HackerRank 'Birthday Chocolate' Solution

Martin Kysel · May 21, 2020

Short Problem Definition:

The member states of the UN are planning to send 2 people to the moon. They want them to be from different countries. You will be given a list of pairs of astronaut ID’s. Each pair is made of astronauts from the same country. Determine how many pairs of astronauts from different countries they can choose from.

Birthday Chocolate


time complexity is O(N)

space complexity is O(1)


Keep track of _M_elements at a time. Basically a simplified prefix sum.


import sys

def getWays(squares, d, m):
    cnt = 0
    q = squares[:m-1]
    for ele in squares[m-1:]:
        if (sum(q) == d):
            cnt += 1
    return cnt

n = int(raw_input().strip())
s = map(int, raw_input().strip().split(' '))
d,m = raw_input().strip().split(' ')
d,m = [int(d),int(m)]
result = getWays(s, d, m)

Twitter, Facebook