Sort by

recency

|

2461 Discussions

|

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

  • + 0 comments

    Do not need the n in the function and using itertools to get all unique pairs:

    import os
    from itertools import combinations
    
    def divisibleSumPairs(n, k, ar):
        valid_pair = 0
        all_pairs = combinations(ar, 2)
        for pair in all_pairs:
            pair_sum = sum(pair)
            if pair_sum % k == 0:
                valid_pair += 1
        return valid_pair
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
        
        first_multiple_input = input().rstrip().split()
        n = int(first_multiple_input[0])
        k = int(first_multiple_input[1])
        ar = list(map(int, input().rstrip().split()))
        result = divisibleSumPairs(n, k, ar)
    
        fptr.write(str(result) + '\n')
        fptr.close()