VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-number-of-operations-required-to-make-all-the-elements-positive/

⇱ Minimum number of operations required to make all the elements positive - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum number of operations required to make all the elements positive

Last Updated : 4 Apr, 2024

Given an array A[] of length N. You can apply the below operation:

  • Choose an index i such that (i+1) element exists. After that choose any number X (positive or negative). Then subtract X from the ith element and add it into (i+1)th element.

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], where LIS[N-1] is the length of the longest increasing subsequence of the Prefix sum array, and N - 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 Res and LIS[] of size N. Iterates over the prefix sum array Pre[] and for each element let say X, We need to check if X is negative. If it is negative, We need to set LIS[i] to 0 and continues to the next iteration. Otherwise, in approach we uses the upper_bound() function to find the first element in Res that is greater than X and replaces it with X. If no such element is found, We need to append X to the end of Res. The LIS array 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:

  • Calculate Prefix Sums: Create a new vector P[] of size N for the prefix sums of the array A. Which gives the sum of A[] till index i.
  • Check for Negative Sum: If the total sum of the array (which is the last element of the prefix sum array P[]) is negative, it’s impossible to make all elements non-negative. In this case, output -1.
  • Initialize LIS Arrays: If the total sum is not negative, initialize two new arrays: 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.
  • Calculate LIS: Iterate over the prefix sum array 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.
  • Result: Finally, output the minimum number of operations required to make all elements of the array non-negative. This is given by N - LIS[N-1].

Code to implement the approach:


Output
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.

Comment