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
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
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))
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
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
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
hii bro can i get ur contact
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
@@thiennguyen1721 can you explain the logic behind i%x and y%i ?
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
can u pls explain
we have to find gcd of three numbers but you have passing only two argument a and b
I am using reduce function, it will take gcd of the whole list, a and b are the lists
Bro how did you get the idea about that gcd, lcm. ?Really genious. Can you suggest me to improve my ideas pls.
Just try out different problems and attend some online contests
do some arthematic and reasoning practice
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
I don't understand, why you took if b == 0 while defining the gcd? Can you explain this?
if one variable (i.e b)reaches 0 other variable 'a' contains the result
In the for loop at the end, why did we use increment in range as "L"? What does this do?
it is the step size for the loop
Didn't understand 😔
What part you didn't understand?
@@HackersRealm u used functions and then reduce. Can u please explain that in detail!!
@@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.
@@HackersRealm thanks!
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))
underrated
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
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
@@HackersRealm Thanks 😀
kuch samagh ni aaya bhai
Please explain properly.. I didnt understand anything.
Sorry, initial videos, i didn't explain much. All the latest videos have clear explanation of logic and code