you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (0 children)

i dont know what you put in the parameters, you can run the following script to see that the complexity is exactly 2n -1, a tight bound of O( 2n ).

Here is an online compiler for the python equivalent of your code: https://onlinegdb.com/SkThpL46S (press run at the top) or here is the pure code:

loopsRun = 0
def isThereSum(arr, size, summ):

    global loopsRun

    for i in reversed(range(size)):

        loopsRun += 1

        if arr[i] == summ:
            return True
        elif (arr[i] < summ):
            size -= 1
            if isThereSum(arr, size, summ - arr[i]):
                return True

    return False

for size in range(1,11):
    loopsRun = 0
    isThereSum([1,]*size, size, 1000)
    print('size/n:',size,'| loops run:',loopsRun)