Menu Close

Calculate the step by step sum of startValue plus elements in array

Given an array of integers nums, you start with an initial positive value startValue. In each iteration, you calculate the step by step sum of startValue plus elements in nums(from left to right). Return the minimum positive value of startValue such that the step by step is never less than 1.
Input: nums = [-3,2,-3,4,2]
Output: 5

def ifcond(x):
    if x>=1:
        return 1
    else:
        return 0
nums=list(map(int,input('Enter an array of integers : ').split(' ')))
for i in range(max(nums)+100):
    count=0
    sum=0
    for j in range(len(nums)):
        if j==0:
            sum+=i+nums[j]
            count+=ifcond(sum)
        else:
            sum+=nums[j]
            count+=ifcond(sum)
    if count == len(nums):
        count=i
        break
if count==0:
    print(nums[0])
else:
    print(count)

Input_1:
Enter an array of integers : -3 2 -3 4 2
Output:
5


Input_2:
Enter an array of integers : 1 2
Output:
1


Morae Q!