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.
Typescript solution:
I have made the algorithm to run o(n) operation using the hash map, and much better compared to o(n^2) if we use brut force method such as nested for loop.
functiondivisibleSumPairs(n:number,k:number,ar:number[]):number{// Write your code hereconstfreq:{[key:number]:number}={};letcount=0;ar.forEach((int)=>{constremainder=int%k;constcomplement=(k-remainder)%k;if(complementinfreq){count+=freq[complement];}if(remainderinfreq){freq[remainder]++;}else{freq[remainder]=1;}});returncount;}
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 →
Typescript solution: I have made the algorithm to run o(n) operation using the hash map, and much better compared to o(n^2) if we use brut force method such as nested for loop.