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.
You really should have used an even number to explain the middle bucket and this case better:
if(k%2==0) sum+=(m[k/2]*(m[k/2]-1))/2;
Edit:
Say with k= 4, you have 1 3 2 6 4 5 9 10:
mod 4 == 0 : 4
mod 4 == 1 : 1 5 9
mod 4 == 2 : 2 6 10
mod 4 == 3 : 3
The base case is the same, and mod 1 matches with mod 3, as before. However, we also have
(2, 6) (2, 10), and (6, 10)
This follows the same logic as the bass case, where any number in this list can match any other number in this list, thus we also use n * (n-1) / 2 here as well
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Divisible Sum Pairs
You are viewing a single comment's thread. Return to all comments →
You really should have used an even number to explain the middle bucket and this case better:
Edit:
Say with k= 4, you have 1 3 2 6 4 5 9 10:
The base case is the same, and mod 1 matches with mod 3, as before. However, we also have (2, 6) (2, 10), and (6, 10)
This follows the same logic as the bass case, where any number in this list can match any other number in this list, thus we also use n * (n-1) / 2 here as well