Input is an array of integers, and a "move" is an operation removing a single element and placing it at the end of the array.
Find the minimum number of moves so the sum of any contiguous subsequence starting at the first element is non-negative
I would like to find the time-complexity optimal solution.
It is guaranteed that it's possible to find a sequence of moves such that after it the sum of every subsequence of the array(from the start of it) is non-negative.
e.g.:
for input:
[1,3,-4,1]
result is 0, since any subsequence starting at the first element is non-negative. {1}=1,{1,3}=4,{1,3,-4}=0,{1,3,-4,1}=1
for input:
[1,3,-5,1]
result is 1, since the sum of subsequence {1,3,-5}
is negative, so a single move -5
is performed to create [1,3,1,-5]
. Then every subsequence sum is non-negative. {1}=1,{1,3}=4,{1,3,1}=5,{1,3,1,-5}=0
for input:
[15,-10,-11,1,-7,12]
result is 2, moving -11
and then -10
, which will look like that:
[15,-10,1,-7,12,-11]
- {15,-10,1,-7}=-1
- negative!
[15,1,-7,12,-11,-10]
- {15}=15,{15,1}=16,{15,1,-7}=9,{15,1,-7,12}=21,{15,1,-7,12,-11}=10,{15,1,-7,12,-11,-10}=0