Sort by

recency

|

2462 Discussions

|

  • + 0 comments

    This is my solution, with JavaScript:

    function divisibleSumPairs(n, k, ar) {
        // Write your code here
        let count = 0;
        while(ar.length>1){
            let i = ar.shift();
            for(let j=0; j<ar.length;j++){    
            let sum = i + ar[j];
            if(sum%k===0) count++;
            }
        }
        return count;
    }
    
  • + 0 comments

    def divisibleSumPairs(n, k, ar): freq = [0] * k count = 0

    for num in ar:
        mod = num % k
        complement = (k - num) % k
        count += freq[complement]
        freq[mod] += 1
    
    return count
    
  • + 0 comments

    My solution in python:

    def divisibleSumPairs(n, k, arr):

    return sum([1 for i in range(n) for j in range (i + 1 , n) if i < j and (arr[i] + arr[j]) % k == 0])
    
  • + 0 comments

    JAVA Solution

    public static int divisibleSumPairs(int n, int k, List ar) {

        int count = 0;
    
        for(int i = 0; i<n; i++){
    
            for(int j = i+1; j<n; j++){
    
                if((ar.get(i)+ ar.get(j)) % k == 0){
                    count++;
                }
            }
        }
    
        return count;
    
    }
    
  • + 0 comments

    C# var remainderCounts = new int[k]; var count = 0;

    foreach (var num in ar) { var remainder = num % k; var complement = (k - remainder) % k; count += remainderCounts[complement]; remainderCounts[remainder]++; }

    return count;