Sort by

recency

|

2465 Discussions

|

  • + 0 comments

    condition should be i <= j not i < j inshallah, correct ?

  • + 0 comments

    O(n) solution in JavaScript

    function divisibleSumPairs(n, k, ar) {
        let pairCount = 0;
        let remainders = new Array(k).fill(0);
        
        for(let i = 0; i < n; i++){
            let num = ar[i];
            let remainder = num % k;
            let compliment = (k - remainder)% k;
            
            pairCount += remainders[compliment];
            remainders[remainder]++;
        }
        return pairCount;
    }
    
  • + 0 comments

    Is there any solution but O(n^2) ?

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