Sort by

recency

|

2475 Discussions

|

  • + 0 comments

    For Python3 Platform

    I wrote the code from scratch just to get more practice and time complexity of my code is O(n+k) and not O(n^2) which is way more efficient

    def divisibleSumPairs(n, k, ar):
        freq = [0] * k
        count = 0
        
        for ele in ar:
            remainder = ele % k
            needed = (k-remainder) % k
            
            count += freq[needed]
            freq[remainder] += 1
        
        return count
    
    n, k = map(int, input().split())
    ar = list(map(int, input().split()))
    
    result = divisibleSumPairs(n, k, ar)
    
    print(result)
    
  • + 0 comments

    def divisibleSumPairs(n, k, ar): count = 0

    for i in range(n):
        for j in range(i+1, n):
            if (ar[i] + ar[j]) % k == 0:
                count += 1
    
    return count
    

    Input

    n, k = map(int, input().split()) ar = list(map(int, input().split()))

    Output

    print(divisibleSumPairs(n, k, ar))

  • + 0 comments

    Mi solución en Python 3:

    def divisibleSumPairs(n, k, ar):
        # Write your code here
        contador= 0
        for i in range(n):
            for j in range(i+1,n):
                if(ar[i]+ ar[j])%k ==0:
                    contador +=1
        return contador
    
  • + 0 comments

    //Java solution` public static int divisibleSumPairs(int n, int k, List ar) { int pairs = 0; // include constraints if (k > 0 && 2 <= n && n <= 100) { for (int i = 0; i < n; i++) { for (int j = 1+i; j < n; j++) { int sum = ar.get(i) + ar.get(j); if (sum % k == 0) { pairs++; } } } }

        return pairs;
    
    }
    

    `

  • + 0 comments

    //Java solution` public static int divisibleSumPairs(int n, int k, List ar) { int pairs = 0; // include constraints if (k > 0 && 2 <= n && n <= 100) { for (int i = 0; i < n; i++) { for (int j = 1+i; j < n; j++) { int sum = ar.get(i) + ar.get(j); if (sum % k == 0) { pairs++; } } } }

        return pairs;
    
    }
    

    `