Divisible Sum Pairs

Sort by

recency

|

207 Discussions

|

  • + 0 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.

    function divisibleSumPairs(n: number, k: number, ar: number[]): number {
      // Write your code here
      const freq: { [key: number]: number } = {};
      let count = 0;
      ar.forEach((int) => {
        const remainder = int % k;
        const complement = (k - remainder) % k;
        if (complement in freq) {
          count += freq[complement];
        }
        if (remainder in freq) {
          freq[remainder]++;
        } else {
          freq[remainder] = 1;
        }
      });
      return count;
    }
    
  • + 0 comments

    C++:

    int divisibleSumPairs(int n, int k, vector<int> ar) {
        int count = 0;
        
        for (int i = 0; i < n; ++i)
        {
            for(int j = i; j < n; ++j)
            {   
                if(j != i)
                {
                    if(IsDivisiblyByK(ar[i] + ar[j], k))
                    {
                        count++;
                    }
                }
            }
        }
        return count;
    }
    
  • + 0 comments
    function divisibleSumPairs(n, k, ar) {
        
        let sum=0,count=0,i,j;
        
        for(i=0; i<n; i++){
            for(j=i+1; j<n; j++){
                sum=ar[i]+ar[j];
                if(sum%k==0){
                    count++;
                }
            }
        }
     return count;
    }
    
  • + 0 comments

    I belive so far this is the most simple to understand PYTHON solution without the use of any fancy functions but just if else and loops.

    sets=[]
    
    for i in range(len(ar)):
        for j in range(len(ar)):
            if i<j:
                if (ar[i]+ar[j])%k==0:
                    sets.append((ar[i],ar[j]))
    return len(sets)
    
  • + 0 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

    def divisible_sum_pairs(n: int, k: int, ar: list[int]):
        #Time complexity: O(n+k)
        #Space complexity (ignoring input): O(k)
        possible_remainders = {}
        total_pairs = 0
    
        for number in ar:
            remainder = number % k
            if remainder in possible_remainders:
                possible_remainders[remainder] += 1
            else:
                possible_remainders[remainder] = 1
    
        # For remainder 0 or k/2, the total pairs will be n choose 2
        if 0 in possible_remainders:
            total_pairs += int(possible_remainders[0] * (possible_remainders[0] - 1) / 2)
        k_is_pair = k % 2 == 0
        half_k = int(k / 2)
        if k_is_pair and (half_k in possible_remainders):
            total_pairs += int(
                possible_remainders[half_k] * (possible_remainders[half_k] - 1) / 2
            )
    
        # For the rest of the remainders, just need to multiply
        for remainder in range(1, int((k + 1) / 2)):
            if (remainder in possible_remainders) and (
                (k - remainder) in possible_remainders
            ):
                total_pairs += int(
                    possible_remainders[remainder] * (possible_remainders[k - remainder])
                )
    
        return total_pairs