Sort by

recency

|

2467 Discussions

|

  • + 0 comments

    This approach greatly improves performance while keeping the logic simple and clear. It’s a great exercise to learn about optimizing loops and using modular properties in programming challenges. If you want to explore or attempt this problem yourself, click here to open it on HackerRank.

  • + 0 comments

    def divisibleSumPairs(n, k, ar): count=0 for i in range(len(ar)-1): first=ar[i] for j in range(i+1,len(ar)): second=ar[j] if (first+second)%k==0: count +=1 return count

  • + 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;
    }
    
  • + 2 comments

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