![]() |
VOOZH | about |
Given an array A[] of length N. You can apply the below operation:
The task is to find the minimum number of operations required to make all array elements positive. If not possible output -1.
Examples:
Input: N = 3, A[] = {-3, 2, 1}
Output: 2
Explanation: The performed operations are as below:
- First operation: Choose i = 2 and X = -1. Then update A2 and A3 as 2-(-1)=3 and 1+(-1)=0 respectively. Then, updated A[] is {-3, 3, 0}
- Second operation: Choose i = 1 and X = -3. Then update A1 and A2 as -3-(-3) = 0 and 3+(-3)=0 respectively. Then, updated A[] is {0, 0, 0}. Now all the elements are >= 0. Therefore, minimum number of operations are 2.
Input: N = 2, A[] = {1, -2}
Output: -1
Explanation: It can be verified that using given operation it is not possible to make all Ai >=0.
Approach: Implement the idea below to solve the problem
The problem can be solved using Prefix Sums and LIS (Longest Increasing Subsequence) Concept. Let us see the significance of both. The main approach for solving the problem is to calculate
N - LIS[N-1],whereLIS[N-1]is the length of the longest increasing subsequence of the Prefix sum array, andN - LIS[N-1]is the minimum number of elements that need to be removed to make the sum of the remaining elements non-negative.
- Prefix Sums: Prefix Sums is an array which gives the sum of input array till the ith index.
- Check for Negative Sums: If the total sum of the array (which is the last element of the prefix sum array
P) is negative, Output-1.Because in such cases, It will be impossible to make all elements non-negative, If the total sum is already negative.- Longest Increasing Subsequence (LIS): Two empty arrays let say Re
sandLIS[]of sizeN. Iterates over the prefix sum arrayPre[]and for each elementlet say X, We need to check ifXis negative. Ifitis negative, We need to setLIS[i]to0and continues to the next iteration. Otherwise, in approach we uses the upper_bound() function to find the first element in Resthat is greater thanXand replaces it withX. If no such element is found, We need to appendXto the end ofRes. TheLISarray stores the length of the longest increasing subsequence ending at each index.- Output: Finally, we need to output
N - LIS[N-1]as required answer, Which is the minimum number of operations required to make all elements of the array non-negative.
Steps taken to solve the problem:
P[] of size N for the prefix sums of the array A. Which gives the sum of A[] till index i.P[]) is negative, it’s impossible to make all elements non-negative. In this case, output -1.Res[] and LIS[]. The res array will store the longest increasing subsequence (LIS) of the prefix sum array P, and the LIS array will store the length of the LIS ending at each index.P. For each element X, if X is negative, set the corresponding element in LIS to 0 and continue to the next iteration. Otherwise, find the first element in res that is greater than X and replace it with X. If no such element is found, append X to the end of Res.N - LIS[N-1].Code to implement the approach:
4
Time Complexity: N*Log(N), As upper_bound() function is used.
Auxiliary Space: O(N), As vectors such as Lis[] and Res[] are used.