The Wayback Machine - https://web.archive.org/web/20220219001228/https://www.geeksforgeeks.org/minimize-dissolve-operations-to-make-all-elements-0-except-first-and-last/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimize dissolve operations to make all elements 0 except first and last

  • Last Updated : 18 Feb, 2022

Given an array arr of length N, the task is to find the minimum number of operations required to make all array elements 0, except  first and last, using the given operation any number of times: 

  • In each operation choose 3 indices 1 ≤ i < j < k ≤ n, such that  arr[j]>=2, and
  • Subtract 2 from arr[j] and add 1 to both arr[i] and arr[k].

Examples 

Input : arr = {1, 2,  2,  3,  6}
Output: 4
Explanation: 
Select (i,j,k)=(1,2,5), array = {2,0,2,3,7}.
Select (i,j,k)=(1,3,4), array = [3,0,0,4,7].
Select (i,j,k)=(1,4,5) twice.
array = [5,0,0,0,9].
All elements except the first and last element  becomes equal to zero.

Input : arr = { 3, 1, 1, 2}
Output: -1
Explanation : It’s impossible to perform any operation,since it is not possible to  select j because all middle elements are 1

 

Approach: Consider the below observations:

There are only two cases when the answer is not possible

  • Case 1 : When all elements except first and last are 1
  • Case 2 : When the array size is 3 and there is an odd number on middle position.

Now consider other cases:

  • Number on jth position, can convert two odd numbers ( on ith and kth indices)  into even by transferring 1
  • and even number let’ s say x will require x/2 operations to convert into zero.
  • If there are odd numbers , they will be first converted into even and then they are converted into zero ,
  • If there are no odd  numbers left always select first and last indices as i and j.

Therefore this problem can be solves using below steps with the help of above observation:

  • For even number (let’s say x), minimum operations required will be x/2
  • For odd number (let’s say y), minimum operations required will be (y+1)/2

Below is the implementation of the above approach: 

Java




// Java implementation of above approach
  
import java.io.*;
  
class GFG {
    public static void solver(int n, int arr[])
    {
        int ans = 0;
        int cnt = 0;
        int i = 0;
  
        // Check case 1
        if (n == 3 && arr[1] % 2 == 1) {
            System.out.println(-1);
            return;
        }
  
        // Check case 2
        for (i = 1; i < n - 1; i++) {
            if (arr[i] == 1)
                cnt++;
        }
  
        if (cnt == n - 2)
            System.out.println(-1);
        else {
            for (i = 1; i < n - 1; i++) {
  
                // if number is odd
                // we have to first make it even
                // and then increased by one
                ans += (arr[i] + 1) / 2;
            }
            System.out.println(ans);
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        int N = 5;
        int arr[] = { 1, 2, 2, 3, 6 };
        solver(N, arr);
    }
}
Output
4

Time Complexity:  O(N)  
Auxiliary Space: O(1 )


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!