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.
functiondivisibleSumPairs(k,ar){// Write your code hereconstmap=newMap();letnoOfPair=0;for(leti=0;i<ar.length;i++){constremainder=ar[i]%k;letdiff=k-remainder;if(diff===k){// means ar[i] is a divisiblediff=0;}if(map.has(diff)){noOfPair+=map.get(diff);}if(map.has(remainder)){letcount=map.get(remainder);map.set(remainder,count+1);}else{map.set(remainder,1);}}returnnoOfPair;}
Here is my solution
i tried lots of iterations before arriving at this solution, some of the problems this question poses includes
1) the array could contain multiples of k, for example imagine ar=[1, 3, 6, 9, 8] and k=3 , 3, 6, ,9 are all divisible by 3, meaning there is 3 pairs(n(n-1)/2) i.e (3,6), (3, 9), (6,9)whose sum are divisible by k(3)
2) For integers less than k, how much more do they need to become divisible by k i.e 1 in ar needs k-1 to become divisible by k, k-1 = 3 - 1 = 2 meaning if we find 2 in this array and pair it with 1, sum of the pair will be divisible.
3) Now, for items greater than k but not divisible by k, how much more do they need to become divisible by k , we get that using this formula a = bq + r where a is the integer in the array, b is the multiple, q is the divisible(k) and r is the remainder. for example, 8 in ar is greater than k(3) , using the above formulae 8 = 2 * 3 + 2 i.e integer division of 8 // 3 = 2 , we can only find 2 3’s in 8 with remainder 2 . Now, that we know this, this becomes easier for us, we now know that k-2 is the integer needed to pair with 8 in order for 8 to become a divisible of k
Now we had an array with items [23, 21, 27 28] and k=3, if we analyse the array, we find out that, i) all integers in array are greater than k ii) only 21 and 27 are divisible by k. However, when we break this elements down to their remainders using a = bq + r we transform the array to [2, 0, 0, 1], now it’s easy for us to identify the pairs, (0, 0), (2, 1) .
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 →
O(N) - time complexity
Here is my solution
i tried lots of iterations before arriving at this solution, some of the problems this question poses includes
1) the array could contain multiples of k, for example imagine
ar=[1, 3, 6, 9, 8]andk=3,3, 6, ,9are all divisible by 3, meaning there is 3 pairs(n(n-1)/2) i.e(3,6), (3, 9), (6,9)whose sum are divisible by k(3)2) For integers less than
k, how much more do they need to become divisible byki.e1inarneedsk-1to become divisible by k,k-1 = 3 - 1 = 2meaning if we find 2 in this array and pair it with 1, sum of the pair will be divisible.3) Now, for items greater than
kbut not divisible byk, how much more do they need to become divisible byk, we get that using this formulaa = bq + rwhereais the integer in the array,bis the multiple,qis the divisible(k) andris the remainder. for example,8inaris greater thank(3), using the above formulae8 = 2 * 3 + 2i.e integer division of8 // 3 = 2, we can only find 2 3’s in8with remainder2. Now, that we know this, this becomes easier for us, we now know thatk-2is the integer needed to pair with 8 in order for 8 to become a divisible ofkNow we had an array with items
[23, 21, 27 28]andk=3, if we analyse the array, we find out that, i) all integers in array are greater than k ii) only21and27are divisible by k. However, when we break this elements down to their remainders usinga = bq + rwe transform the array to[2, 0, 0, 1], now it’s easy for us to identify the pairs,(0, 0), (2, 1).