Skip to content

Ice Cream Parlor

Moustafa Attia edited this page Dec 6, 2017 · 1 revision

This wiki to show my solution for HackerRank problem Ice Cream Parlor

Solution steps:

  • create a dictionary where: key = price, value = list of indices of this price
  • loop over each key (price) inside dictionary and check if (m - key) exist, this means that exist two elements inside dictionary that can add up to (m)
  • return these two elements sorted

Solution in Python 3:

def Solve(m,n,c) :
    d = dict()
    i = 1
    # key = price, value = list of indices
    for e in c :
        if e not in d :
            d[e] = [i]
        else :
            d[e].append(i)
        i += 1

    for e in d :
        if m-e in d :
            # just check if price exist in more than one position then select two different indices 
            if len(d[e]) == 1 and len(d[m-e]) == 1 :
                return (sorted([d[e][0] , d[m-e][0]]))
            elif len(d[e]) >= 2 and len(d[m-e]) >= 2 :
                return (sorted([d[e][0] , d[m-e][1]]))

t = int(input())
for _ in range(t) :
    m = int(input())
    n = int(input())
    c = [int(x) for x in input().split(' ')]
    print(' '.join([str(x) for x in Solve(m,n,c)]))

Clone this wiki locally