15 - Between Two Sets | Implementation | Hackerrank Solution | Python

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 31

  • @amacodes7347
    @amacodes7347 Рік тому +5

    def getTotalX(a, b):
    count = 0
    for i in range(1, 101):
    if all(i % x == 0 for x in a) and all(y % i == 0 for y in b):
    count += 1
    return count

    • @69organ
      @69organ 9 місяців тому

      hii bro can i get ur contact

    • @thiennguyen1721
      @thiennguyen1721 5 місяців тому

      My solution is improved from yours:
      def getTotalX(a, b):
      count = 0
      for i in range(max(a), min(b) + 1):
      if all(i % x == 0 for x in a) and all(y % i == 0 for y in b):
      count += 1
      return count

    • @Spring_Boott
      @Spring_Boott 2 місяці тому

      @@thiennguyen1721 can you explain the logic behind i%x and y%i ?

  • @devmishra6317
    @devmishra6317 4 роки тому +5

    i didn't know about reduce function thankyou very much buddy it helped a lot...
    also the program needs a little tweak in the for loop bcz in my case the input
    3 2
    2 3 6
    42 84
    was returning 5 while the answer was 2 so i did a small tweak
    for i in range(l,g+1):
    if g%i == 0 and i%l == 0: # here the tweak
    s += 1
    return s

  • @ashutoshchauhan3608
    @ashutoshchauhan3608 3 роки тому +2

    can u pls explain
    we have to find gcd of three numbers but you have passing only two argument a and b

    • @HackersRealm
      @HackersRealm  3 роки тому +1

      I am using reduce function, it will take gcd of the whole list, a and b are the lists

  • @navyasri5077
    @navyasri5077 3 роки тому +1

    Bro how did you get the idea about that gcd, lcm. ?Really genious. Can you suggest me to improve my ideas pls.

    • @HackersRealm
      @HackersRealm  3 роки тому +2

      Just try out different problems and attend some online contests

    • @saisantosh2180
      @saisantosh2180 Рік тому

      do some arthematic and reasoning practice

    • @AdeniyiAdebowale-yi9fc
      @AdeniyiAdebowale-yi9fc Місяць тому

      Bro, is really smart wtf...I honestly wanted to ask you, how you were going to handle more than 2 inputs, but then you did the magic with reduce

  • @jamirpatel6448
    @jamirpatel6448 4 роки тому +2

    I don't understand, why you took if b == 0 while defining the gcd? Can you explain this?

    • @HackersRealm
      @HackersRealm  4 роки тому +1

      if one variable (i.e b)reaches 0 other variable 'a' contains the result

  • @mmxo2631
    @mmxo2631 2 роки тому +1

    In the for loop at the end, why did we use increment in range as "L"? What does this do?

    • @AjinA-we3jx
      @AjinA-we3jx Рік тому +1

      it is the step size for the loop

  • @roshanvastrad4514
    @roshanvastrad4514 4 роки тому +10

    Didn't understand 😔

    • @HackersRealm
      @HackersRealm  4 роки тому

      What part you didn't understand?

    • @roshanvastrad4514
      @roshanvastrad4514 4 роки тому

      @@HackersRealm u used functions and then reduce. Can u please explain that in detail!!

    • @HackersRealm
      @HackersRealm  4 роки тому +1

      @@roshanvastrad4514 reduce is used when we have a list of values to pass to the function. reduce will compute lcm of all the values in the array.

    • @roshanvastrad4514
      @roshanvastrad4514 4 роки тому +2

      @@HackersRealm thanks!

  • @LucasLeow
    @LucasLeow 2 роки тому +3

    2 conditions provided:
    1: Elements of first array are all factors of integer being considered
    2: Integer being considered is a factor of all elements of second array
    Condition 2 is the initial controlling factor since integer considered must be a factor of all elements of second array.
    Lets tackle condition 2 first by getting all common factors in second array.
    list_of_possible_integers = list()
    for i in range(1, min(b) + 1):
    if all(b_element % i == 0 for b_element in b):
    list_of_possible_integers.append(i)
    Now having the list of possible integers, we must consider condition 1.
    Elements of first array must be a factor of the integer in the list of possible integers
    final_integers = list()
    for integer in list_of_possible_integers:
    if all(integer % a_element == 0 for a_element in a): # if integer can be divided by all a_element
    final_integers.append(integer)
    print(len(final_integers))

  • @nitheshkumar2998
    @nitheshkumar2998 3 роки тому

    hi buddy,
    I got the gcd ,lcm ,reduce functions usage and all ok, but wat exactly this problem statements try to find and steps in loop.
    Still not clear.
    Thanks in Advance

    • @HackersRealm
      @HackersRealm  3 роки тому +1

      In the problem example, you can see, we are getting list of factors first. Using the loop we are getting those factors(eg: 4 8 12 16). The if condition in the loop checks whether it's a factor of elements in b(we found that using gcd). Please check the first two lines of the problem statement to understand the condition

    • @nitheshkumar2998
      @nitheshkumar2998 3 роки тому +1

      @@HackersRealm Thanks 😀

  • @MrTryEverythingInLife
    @MrTryEverythingInLife 4 роки тому +2

    kuch samagh ni aaya bhai

  • @shibourne2000
    @shibourne2000 3 роки тому

    Please explain properly.. I didnt understand anything.

    • @HackersRealm
      @HackersRealm  3 роки тому +1

      Sorry, initial videos, i didn't explain much. All the latest videos have clear explanation of logic and code