We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This is tough to explain. Let's say we have array {2, 3, 4, 1}. Let's say we have a "lambda" function that is
(a,b)->a*b
When we use reduce, we traverse the array left to right. It takes each adjacent pair and multiplies it together while going left to right.
So we get 2 * 3 = 6. Then it takes the 6 and multiplies it by 4, and we get 24.
Then we do 24 * 1 and get 24.
So basically, it multiplies all the elements in the array together, by going left to right.
Now if instead of multiplication, if we have LCM, it does LCM(2, 3). It takes that result, and does LCM of that result with 4. Then it takes that result, and does LCM of that with 1.
If that made no sense, try reading about "lambda functions" in Java.
Between Two Sets
You are viewing a single comment's thread. Return to all comments →
This is tough to explain. Let's say we have array {2, 3, 4, 1}. Let's say we have a "lambda" function that is
When we use reduce, we traverse the array left to right. It takes each adjacent pair and multiplies it together while going left to right. So we get 2 * 3 = 6. Then it takes the 6 and multiplies it by 4, and we get 24. Then we do 24 * 1 and get 24.
So basically, it multiplies all the elements in the array together, by going left to right.
Now if instead of multiplication, if we have LCM, it does LCM(2, 3). It takes that result, and does LCM of that result with 4. Then it takes that result, and does LCM of that with 1.
If that made no sense, try reading about "lambda functions" in Java.
HackerRank solutions.