Sort by

recency

|

2457 Discussions

|

  • + 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()
    
  • + 0 comments
    def divisibleSumPairs(n, k, ar):
        count = 0
        for index, i in enumerate(ar):
            for j in ar[index+1:]:
                if (i + j) % k == 0:
                    count+=1        
        return count
    
  • + 0 comments

    result=0 for num in range(n): for num1 in range(n): if num!=num1 and (ar[num]+ar[num1])%k==0: result=result+1 if result%2==0: return result//2 elif result%2==1: return result//2+1

  • + 0 comments

    Simplest solution

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