HackerRank 'Divisible Sum Pairs' Solution

Martin Kysel · July 3, 2016

Short Problem Definition:

You are given an array of n integers and a positive integer, k. Find and print the number of (i,j) pairs where i < j and ai + aj is evenly divisible by k.

Divisible Sum Pairs

Complexity:

time complexity is O(N^2)

space complexity is O(1)

Execution:

Brute force search.

Solution:

n,k = raw_input().strip().split(' ')
n,k = [int(n),int(k)]
a = map(int,raw_input().strip().split(' '))
count=0
for i in xrange(len(a)):
    for j in xrange(i+1,len(a)):
        if (a[i]+a[j]) % k == 0:
            count+=1
        
print count

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.