Minimize dissolve operations to make all elements 0 except first and last
Last Updated :
29 Aug, 2023
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:
C++
#include <bits/stdc++.h>
using namespace std;
void solver( int n, int arr[])
{
int ans = 0;
int cnt = 0;
int i = 0;
if (n == 3 && arr[1] % 2 == 1) {
cout << -1;
return ;
}
for (i = 1; i < n - 1; i++) {
if (arr[i] == 1)
cnt++;
}
if (cnt == n - 2)
cout << (-1);
else {
for (i = 1; i < n - 1; i++) {
ans += (arr[i] + 1) / 2;
}
cout << (ans);
}
}
int main()
{
int N = 5;
int arr[] = { 1, 2, 2, 3, 6 };
solver(N, arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void solver( int n, int arr[])
{
int ans = 0 ;
int cnt = 0 ;
int i = 0 ;
if (n == 3 && arr[ 1 ] % 2 == 1 ) {
System.out.println(- 1 );
return ;
}
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++) {
ans += (arr[i] + 1 ) / 2 ;
}
System.out.println(ans);
}
}
public static void main(String[] args)
{
int N = 5 ;
int arr[] = { 1 , 2 , 2 , 3 , 6 };
solver(N, arr);
}
}
|
Python
def solver(n, arr):
ans = 0
cnt = 0
i = 0
if (n = = 3 and arr[ 1 ] % 2 = = 1 ):
print ( - 1 )
return
for i in range ( 1 , n - 1 ):
if (arr[i] = = 1 ):
cnt + = 1
if (cnt = = n - 2 ):
print ( - 1 )
else :
for i in range ( 1 , n - 1 ):
ans + = (arr[i] + 1 ) / / 2
print (ans)
N = 5
arr = [ 1 , 2 , 2 , 3 , 6 ]
solver(N, arr)
|
C#
using System;
class GFG {
public static void solver( int n, int []arr)
{
int ans = 0;
int cnt = 0;
int i = 0;
if (n == 3 && arr[1] % 2 == 1) {
Console.WriteLine(-1);
return ;
}
for (i = 1; i < n - 1; i++) {
if (arr[i] == 1)
cnt++;
}
if (cnt == n - 2)
Console.WriteLine(-1);
else {
for (i = 1; i < n - 1; i++) {
ans += (arr[i] + 1) / 2;
}
Console.WriteLine(ans);
}
}
public static void Main()
{
int N = 5;
int []arr = { 1, 2, 2, 3, 6 };
solver(N, arr);
}
}
|
Javascript
<script>
function solver(n, arr)
{
let ans = 0;
let cnt = 0;
let i = 0;
if (n == 3 && arr[1] % 2 == 1) {
document.write(-1);
return ;
}
for (i = 1; i < n - 1; i++) {
if (arr[i] == 1)
cnt++;
}
if (cnt == n - 2)
document.write(-1);
else {
for (i = 1; i < n - 1; i++) {
ans += Math.floor((arr[i] + 1) / 2)
}
document.write(ans);
}
}
var N = 5;
var arr = [ 1, 2, 2, 3, 6 ];
solver(N, arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1 )
Another Approach:
- Define a function named minOperations that takes two arguments: an integer n and an integer array arr.
- Initialize a variable ans to 0.
- Check for case 1: if n is 3 and the second element of arr is odd, return -1.
- Check for case 2: if all elements of arr except the first and last are 1, return -1.
- Loop through the array arr starting from the second element and ending at the second-to-last element:
a. If the current element is odd, add (arr[i] + 1) / 2 to ans.
b. If the current element is even, add arr[i] / 2 to ans.
- Return ans.
- In the main function, define an integer array arr and initialize it with the given values.
- Compute the length of the array arr and store it in an integer variable n.
- Call the minOperations function with arguments n and arr, and store the result in an integer variable ans.
- Print the value of ans.
- Return 0 to indicate successful completion of the program.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int n, int arr[]) {
int ans = 0;
if (n == 3 && arr[1] % 2 == 1) {
return -1;
}
bool allOnes = true ;
for ( int i = 1; i < n - 1; i++) {
if (arr[i] != 1) {
allOnes = false ;
break ;
}
}
if (allOnes) {
return -1;
}
for ( int i = 1; i < n - 1; i++) {
if (arr[i] % 2 == 1) {
ans += (arr[i] + 1) / 2;
}
else {
ans += arr[i] / 2;
}
}
return ans;
}
int main() {
int arr[] = {1, 2, 2, 3, 6};
int n = sizeof (arr) / sizeof (arr[0]);
int ans = minOperations(n, arr);
cout << ans << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int minOperations( int n, int [] arr) {
int ans = 0 ;
if (n == 3 && arr[ 1 ] % 2 == 1 ) {
return - 1 ;
}
boolean allOnes = true ;
for ( int i = 1 ; i < n - 1 ; i++) {
if (arr[i] != 1 ) {
allOnes = false ;
break ;
}
}
if (allOnes) {
return - 1 ;
}
for ( int i = 1 ; i < n - 1 ; i++) {
if (arr[i] % 2 == 1 ) {
ans += (arr[i] + 1 ) / 2 ;
}
else {
ans += arr[i] / 2 ;
}
}
return ans;
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 2 , 3 , 6 };
int n = arr.length;
int ans = minOperations(n, arr);
System.out.println(ans);
}
}
|
Python3
def min_operations(n, arr):
ans = 0
if n = = 3 and arr[ 1 ] % 2 = = 1 :
return - 1
all_ones = all (arr[i] = = 1 for i in range ( 1 , n - 1 ))
if all_ones:
return - 1
for i in range ( 1 , n - 1 ):
if arr[i] % 2 = = 1 :
ans + = (arr[i] + 1 ) / / 2
else :
ans + = arr[i] / / 2
return ans
if __name__ = = "__main__" :
arr = [ 1 , 2 , 2 , 3 , 6 ]
n = len (arr)
ans = min_operations(n, arr)
print (ans)
|
C#
using System;
public class GFG {
public static int MinOperations( int n, int [] arr) {
int ans = 0;
if (n == 3 && arr[1] % 2 == 1)
{
return -1;
}
bool allOnes = true ;
for ( int i = 1; i < n - 1; i++)
{
if (arr[i] != 1)
{
allOnes = false ;
break ;
}
}
if (allOnes)
{
return -1;
}
for ( int i = 1; i < n - 1; i++)
{
if (arr[i] % 2 == 1)
{
ans += (arr[i] + 1) / 2;
}
else
{
ans += arr[i] / 2;
}
}
return ans;
}
public static void Main()
{
int [] arr = {1, 2, 2, 3, 6};
int n = arr.Length;
int ans = MinOperations(n, arr);
Console.WriteLine(ans);
}
}
|
Javascript
function minOperations(n, arr) {
let ans = 0;
if (n === 3 && arr[1] % 2 === 1) {
return -1;
}
let allOnes = true ;
for (let i = 1; i < n - 1; i++) {
if (arr[i] !== 1) {
allOnes = false ;
break ;
}
}
if (allOnes) {
return -1;
}
for (let i = 1; i < n - 1; i++) {
if (arr[i] % 2 === 1) {
ans += Math.floor((arr[i] + 1) / 2);
}
else {
ans += Math.floor(arr[i] / 2);
}
}
return ans;
}
const arr = [1, 2, 2, 3, 6];
const n = arr.length;
const ans = minOperations(n, arr);
console.log(ans);
|
Time complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...