I have N elements in array. I can select the first item max of N times, the second item max of N-1 times and so on.
I have K tokens to use and need to use them so I can have the maximum number of items.
arr = [3, 4, 8] where array elements indicates tokens required for i'th item
n = 10 , represents number of tokens I have
Output:
3
Explanation:
We have 2 options here:
1. option 1: 1st item 2 times for 6 tokens (3*2) and second item once for 4 tokens (4*1)
2. option 2: 1st item 3 times for 9 tokens (3*3)
so maximum we can have 3 items
Code:
def process(arr,n):
count = 0
sum = 0
size = len(arr)+1
for i in range(0, len(arr), 1):
size1 = size-1
size -= 1
while((sum+arr[i] <= n) and (size1 > 0)):
size1 = size1 -1
sum = sum + arr[i]
count += 1
return count;
But it worked for only few test cases, it failed for some hidden test cases. I am not sure where I made a mistake. Can anybody help me?