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.
If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below:
my solutions
defdivisible_sum_pairs(n:int,k:int,ar:list[int]):#Time complexity: O(n+k)#Space complexity (ignoring input): O(k)possible_remainders={}total_pairs=0fornumberinar:remainder=number%kifremainderinpossible_remainders:possible_remainders[remainder]+=1else:possible_remainders[remainder]=1# For remainder 0 or k/2, the total pairs will be n choose 2if0inpossible_remainders:total_pairs+=int(possible_remainders[0]*(possible_remainders[0]-1)/2)k_is_pair=k%2==0half_k=int(k/2)ifk_is_pairand(half_kinpossible_remainders):total_pairs+=int(possible_remainders[half_k]*(possible_remainders[half_k]-1)/2)# For the rest of the remainders, just need to multiplyforremainderinrange(1,int((k+1)/2)):if(remainderinpossible_remainders)and((k-remainder)inpossible_remainders):total_pairs+=int(possible_remainders[remainder]*(possible_remainders[k-remainder]))returntotal_pairs
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 →
Python best solution
If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions