Making zero array by decrementing pairs of adjacent


The problem statement includes making an array zero array by decrementing pairs of adjacent.

The array will be given in the input and we can perform the operation on the array i.e. subtract 1 from ith and (i+1)th index where 0<=i<(array size−1). We can perform the given operation as many times as we need to make the elements of the array zero. The array will only contain positive integers.

In this problem, we will be given an array in the input and we need to check if performing the above operation any number of times, we can convert all the array’s elements into zero or not. If all the elements of the array can be converted into zero performing any number of operations, we need to print “Yes” else we need to print “No”.

Let’s try to understand the problem with the below examples.

Input

a[]={1,2,5,4}

Output

Yes

Explanation − Applying the operations on the array, it becomes

{1,2,5,4}={0,1,5,4}={0,0,4,4}

Applying the operation on a[2] and a[3] 4 times, we get {0,0,0,0}.

Since, the array can be converted into a zero array by applying the specified operation any number of times, the output is yes.

Input

a[]={3, 2, 2, 6}

Output

No

Explanation − Applying the operation on the array,

{3,2,2,6}={2,1,2,6}={1,0,2,6}={1,0,1,5}={1,0,0,4}

Since we can’t perform the operation further as we can only subtract 1 from a[i] and a[i+1] and the array can’t be converted into a zero array. Hence, the output is No.

There are various approaches to solve the problem to check if the array can be converted into a zero array by applying the specified operation any number of times. Let’s see and understand the efficient approaches to solve the problem.

Approach 1

The array can be converted into zero arrays if the sum of the elements in the array at odd positions is equal to the sum of the elements in the array at even positions. Since we can only subtract 1 from i and (i+1) position therefore to make the array zero array by subtracting 1 from odd position and even position, this would be only possible if the sum of elements at odd position is equal to sum of elements at even position.

For example, {1,2,5,4} can be converted into a zero array.

The sum of elements at odd position i.e. a[1]+a[3]=2+4=6

The sum of elements at even position i.e. a[0]+a[2]=1+5=6

Therefore, it can be converted into zero array.

We use this logic to check if the array can be converted into zero array by decrementing pairs adjacent in our approach.

The steps to follow to implement the approach in C++:

  • We will make a function to check if the array can be converted into a zero array or not by calculating the sum of even position and odd position.

  • Initialise two variables to store sum of numbers at even position and odd position.

  • Iterate in a for loop from i=0 to i<array’s size.

  • While iterating in a for loop, we will check if i is even, we will add the corresponding element at ith position in the variable which we will use to store sum of numbers at even position or else we will add the corresponding number at the ith position in another variable to store sum of odd positions if i is not even.

  • After iterating in the whole array, we will check if the sum of even positions is equal to sum of odd positions by comparing the value stored in the variables.

  • Return true if they both are equal else return false.

  • If the function returns true, we will print “Yes” in the output else we will print “N0”.

Example

//C++ code to check if the array can be converted to zero array by decrementing adjacent pairs by 1

#include<bits/stdc++.h>

using namespace std;

//function to check if the array can be converted to a zero array
bool check(int a[],int N){
    int even_sum=0; //to store sum of elements at even position
    int odd_sum=0; //to store sum of elements at odd positions
    
    //to find the sum of even and odd positions
    for(int i=0;i<N;i++){
        
        //if i is even
        if(i%2==0){
            even_sum += a[i];
        }
        else{
            odd_sum += a[i];
        }
    }
    
    if(even_sum==odd_sum){ //return true if sum of even positions is equal to odd positions
        return true;
    } else{
        return false;
    }
}

int main()
{
    int a[]={2,8,3,12,5,9,17,8,8,11};
    int N; //to store size of the array
    N=sizeof(a)/sizeof(a[0]);
    //calling the function
    if(check(a,N)==true){
        cout<<"Yes"<<endl;
    } else{
        cout<<"No"<<endl;
    }

    return 0;
}

Output

No

Time complexity− O(N) , because we iterate in the array to store sum of numbers at even position and odd position.

Space complexity − O(1) , because no extra space is taken in the approach.

Approach 2

In this approach, we will check the number formed by the given array. The array can be converted into a zero array by subtracting 1 from a[i] and a[i+1] any number of times if the number formed by the array will be multiple of 11. If the number formed by the given array is not multiple of 11, the array can’t be converted into a zero array.

For example, the array is {3,2,2,6}.

The number formed by the array will be 3226. Since this number is not multiple of 11, the array can’t be converted into a zero array.

If the given array is {1,2,5,4}.

The number formed by the array will be 1254. This number is multiple of 11 as 11*114 gives 1254. Therefore, the array can be converted to a zero array by performing the operation.

The steps to follow to implement the approach in C++:

  • We will make a function to check if the number formed by the given array is the multiple of 11.

  • Initialise a variable to store the number formed by the array.

  • We will iterate in a for loop from i=0 to i<array’s size and keep updating the number by multiplying it by 10 and adding ith number in it.

  • Once we get the number formed by the array, we will check if it is divisible by 11 or not. If it is divisible by 11, we will print Yes as the number formed is multiple of 11 else we will print No.

Note:This approach will only apply to the arrays of size less than or equal to 18 as the number formed by the array may overflow the data type.

Example

// C++ code to check if the array can be converted to a zero array
// by decrementing the pairs of adjacent

#include <bits/stdc++.h>

using namespace std;

//function to check if the given array can be converted to a zero array
bool check(int a[], int N)
{
	// to store the number formed by the given array
	long long number = 0;
	for (int i = 0; i < N; i++){
	    //update the number
		number = number * 10 + a[i];
	}
    
    //if number is multiple of 11, it will be divisible by 11
	if(number%11==0){
	    return true;
	} else{
	    return false;
	}
}


int main()
{
	int a[] = {2,5,1,3,6,1 };
	int N = sizeof(a) / sizeof(a[0]); //calculating size of the array
	
	//calling the function
	if (check(a, N)){
		cout << "Yes"<<endl;
	} else{
		cout << "No"<<endl;
	}
}

Output

Yes

Time complexity− O(N) , as we iterate in the given array to get the number formed by the array

Space complexity − O(1) , because no extra space is taken in the approach.

Conclusion

The problem of checking if the given array can be converted to a zero array by decrementing pairs of adjacent pairs was discussed in the article. We tried to solve the problem with two different approaches in C++ using simple logic within O(N) runtime and constant space.

I hope you understand the problem and the approach towards solving the problem after reading this article.

Updated on: 28-Aug-2023

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements