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.
Java Solution
Using a complementary Hashtable.
Time Complexity: O(n), Space Complexity: O(k)
publicstaticintdivisibleSumPairs(intn,intk,List<Integer>ar){intpairs=0;// The remainder counts hastable has the size of the kint[]remainderCounts=newint[k];for(intnum:ar){// we have kth remainder positionsintremainder=num%k;// Find the complement intcomplement=(k-remainder)%k;// Add the count of complements found so farpairs+=remainderCounts[complement];// Increment the count of remaindersremainderCounts[remainder]++;}returnpairs;}
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 →
Java Solution Using a complementary Hashtable. Time Complexity: O(n), Space Complexity: O(k)