Ice Cream Parlor

  • + 2 comments

    Simple solution in python:

    no need to sort

    t = raw_input()
    t = int(t)
    for i in range(0,t):
        m = raw_input()
        m = int(m)
        n = raw_input()
        n = int(n)
        li = raw_input()  #list of elements from input
        li = [int(i) for i in li.split(' ')]
        for i in range(0,len(li)):
            if (m-li[i]) in li[i+1:len(li)]: #check if the difference of m and element in the list exist, if yes then pick the index of those two elements.
                li1 = li[i+1:len(li)] #if the same element appears twice, for example m=4 and the list has 2 2 4 1, this line helps in picking the next '2' at the position 1(starting from 0)
                x = li1.index(m-li[i]) #position of the next element which forms the sum equal to m.
                print i+1, i+x+2 #Since index starts from 0 add 1 to i, to print the second index, remember we have copied all the elements after position i, so i+x, since the index starts from 0, i+x+1, we left the element at position i while copying to li1, hence i+x+2
                break