Python HackerRank Bonetrousle code times out
<p>I have been working on the Bonetrousle problem in hacker rank using python. After many hours the below code passes all the test cases except one where it times out. Any suggestions on how I can make the code quicker would be appreciated. I believe the problem is the code that deals with the remainder, I put comments below and above it so it is easy to find. Unfortunately I am at a loss on how to refactor it so it works faster.</p>

<p>The code I wrote gets the right answer for all test cases, I have verified this in pycharm. The only problem is that it is to slow for one of the hacker rank test cases. </p>

<p>Here is the link to the problem <a href="https://www.hackerrank.com/challenges/bonetrousle" rel="nofollow">https://www.hackerrank.com/challenges/bonetrousle</a></p>

<p>Here is a link to the test case it fails 
<a href="https://hr-testcases-us-east-1.s3.amazonaws.com/21649/input12.txt?AWSAccessKeyId=AKIAJAMR4KJHHUS76CYQ&amp;Expires=1473987910&amp;Signature=xaHGvYRVmUVJHh4r3on%2BWgoIsjs%3D&amp;response-content-type=text%2Fplain" rel="nofollow">https://hr-testcases-us-east-1.s3.amazonaws.com/21649/input12.txt?AWSAccessKeyId=AKIAJAMR4KJHHUS76CYQ&amp;Expires=1473987910&amp;Signature=xaHGvYRVmUVJHh4r3on%2BWgoIsjs%3D&amp;response-content-type=text%2Fplain</a></p>

<pre><code>firstLine = int(input())

for a in range(0, firstLine):
    nums = input() 
    numsArr = list(map(int, nums.split(" ")))
    n = numsArr[0]
    k = numsArr[1]
    b = numsArr[2]

    num1 = 0 
    rem = 0 

    answer = True
    remAdded = False 
    count = 0 
    boxArr = []
    for i in range(1, b+1): 
        count += i 
        boxArr.append(i)



    num1 = (n - count)//b
    rem = (n - count)%b 

    for j in range(0, len(boxArr)): 
        boxArr[j] = boxArr[j] + num1 
        if boxArr[j] &gt; k:
            answer = False

    # In below code -&gt; if there is a remainder I am adding it to an element in the array that has box numbers
    # I check to see if I can add the remainder to an element in the array
     #without that element exceeding k, the number of sticks. If I can't  then the bool remAdded doesn't get set to True
    # The below code works but it seems inefficient and looks like the problem


    if rem == 0:
        remAdded = True  
    elif answer != False: 
        for r in range(len(boxArr) - 1, 0, -1):
            if boxArr[r] + rem &lt;= k and r == len(boxArr) - 1: 
                boxArr[r] = boxArr[r] + rem
                remAdded = True 
                break
            else:
                if boxArr[r] + rem &lt;= k and (boxArr[r] + rem) not in boxArr:
                    boxArr[r] = boxArr[r] + rem
                    remAdded = True 
                    break


    # above is code for dealing with remainder. Might be the problem

    if answer == False or remAdded == False: 
        print(-1)
    elif 0 in boxArr:
        print(-1)
    else:
        for z in range(0, len(boxArr)):
            if z != len(boxArr) - 1: 
                print(boxArr[z], end =" ")
            else:
                print(boxArr[z])
</code></pre>

<p>Replace the code between your comments by:</p>

<pre><code>if rem == 0:
    remAdded = True
elif boxArr[-1] + 1 &gt; k:
    remAdded = False
elif answer != False:
    l = len(boxArr)-1
    for r in range(l, l-rem, -1):
        boxArr[r] += 1
    remAdded = True
</code></pre>

<p>This gets rid of the expensive <code>(boxArr[r] + rem) not in boxArr</code>, basically. It passes all test cases for me.</p>

python algorithm