Challenges

Add
MEDIUM
Attempted: 0
Completed: 0
Likes: 0

Given an array arr of n integers. In each operation, the system can increase the ith element by 1 (i.e. set arr[i] = arr[i] + 1, where 1 <= i <= n). The task is to calculate the minimum number of operations required such that there is no prefix in the array arr whose sum is less than 0 (i.e. for all i, arr[0] + arr[1] + arr[2] + … + arr[i] = 0).

Example 1:

Input: arr = [2, -3, 1]
Output: 1
Reasoning: The operation can increase the 2nd element of the array by 1.

Example 2:

Input: arr = [5]
Output: 0
Reasoning: No operations are required.

Your task is to write a function called min_ops which takes in an integer array arr and returns the minimum number of operations.