Open In App

Check if given array can be made 0 with given operations performed any number of times

Last Updated : 30 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] containing N integers, the task is to find whether all the elements of the given array can be made 0 by following operations: 
 

  • Increment any element by 2.
  • Subtract the minimum element of the array from all elements in the array.
  • The above operations can be performed any number times.

If all the elements of the given array can become zero then print Yes else print No.
Examples: 
 

Input: arr[] = {1, 1, 3} 
Output: Yes 
Explanation: 
1st round: Choose the first element in the array and increase it by 2 i.e arr[] = {3, 1, 3}. 
Then decrease all the elements by 1(which is minimum in the current array) i.e arr[] = {2, 0, 2}. 
2nd round: Choose the second element in the array and increase it by 2 i.e arr[] = {2, 2, 2}. 
Then decrease all the elements by 2(which is minimum in the current array) i.e arr[] = {0, 0, 0}. 
Therefore, with the given operations performing on the elements of the array, all the elements of the given array can be reduced to 0.
Input: arr[] = {2, 1, 4, 2} 
Output: No 
Explanation: 
We cannot make all the element of the array 0 by performing the given operations. 
 

 

Approach: The problem can be solved with the help of Parity
 

  • Since, by incrementing the element of the array by 2 in each operation, the parity of the element is not changed i.e., odd remains odd or even remains even.
  • And after subtracting each element of the array with the minimum element in the array, the parity of even integers becomes odd and the parity of odd integers becomes even.
  • Therefore to make all the elements of the array 0, the parity of all the elements must be same otherwise we can’t make all the elements of the array 0 by the given operations.

Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to whether the array
// can be made zero or not
bool check(int arr[], int N)
{
    // Count for even elements
    int even = 0;
 
    // Count for odd elements
    int odd = 0;
 
    // Traverse the array to
    // count the even and odd
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is odd
        if (arr[i] & 1) {
            odd++;
        }
 
        // If arr[i] is even
        else {
            even++;
        }
    }
 
    // Check if count of even
    // is zero or count of odd
    // is zero
    if (even == N || odd == N)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver's Code
int main()
{
    int arr[] = { 1, 1, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    check(arr, N);
 
    return 0;
}


Java




// Java implementation of the approach
import java.util.*;
 
class GFG{
  
// Function to whether the array
// can be made zero or not
static void check(int arr[], int N)
{
    // Count for even elements
    int even = 0;
  
    // Count for odd elements
    int odd = 0;
  
    // Traverse the array to
    // count the even and odd
    for (int i = 0; i < N; i++) {
  
        // If arr[i] is odd
        if (arr[i] % 2 == 1) {
            odd++;
        }
  
        // If arr[i] is even
        else {
            even++;
        }
    }
  
    // Check if count of even
    // is zero or count of odd
    // is zero
    if (even == N || odd == N)
        System.out.print("Yes");
    else
        System.out.print("No");
}
  
// Driver's Code
public static void main(String[] args)
{
    int arr[] = { 1, 1, 3 };
    int N = arr.length;
  
    check(arr, N);
}
}
 
// This code is contributed by Rajput-Ji


Python3




# Python3 implementation of the approach
 
# Function to whether the array
# can be made zero or not
def check(arr, N):
     
    # Count for even elements
    even = 0;
 
    # Count for odd elements
    odd = 0;
 
    # Traverse the array to
    # count the even and odd
    for i in range(N):
 
        # If arr[i] is odd
        if (arr[i] % 2 == 1):
            odd += 1;
     
        # If arr[i] is even
        else:
            even += 1;
 
    # Check if count of even
    # is zero or count of odd
    # is zero
    if (even == N or odd == N):
        print("Yes");
    else:
        print("No");
 
# Driver's Code
if __name__ == '__main__':
    arr = [ 1, 1, 3];
    N = len(arr);
 
    check(arr, N);
 
# This code is contributed by 29AjayKumar


C#




// C# implementation of the approach
using System;
 
class GFG{
   
// Function to whether the array
// can be made zero or not
static void check(int []arr, int N)
{
    // Count for even elements
    int even = 0;
   
    // Count for odd elements
    int odd = 0;
   
    // Traverse the array to
    // count the even and odd
    for (int i = 0; i < N; i++) {
   
        // If arr[i] is odd
        if (arr[i] % 2 == 1) {
            odd++;
        }
   
        // If arr[i] is even
        else {
            even++;
        }
    }
   
    // Check if count of even
    // is zero or count of odd
    // is zero
    if (even == N || odd == N)
        Console.Write("Yes");
    else
        Console.Write("No");
}
   
// Driver's Code
public static void Main(String[] args)
{
    int []arr = { 1, 1, 3 };
    int N = arr.Length;
   
    check(arr, N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
// Javascript implementation of the approach
 
 
// Function to whether the array
// can be made zero or not
function check(arr, N)
{
    // Count for even elements
    let even = 0;
 
    // Count for odd elements
    let odd = 0;
 
    // Traverse the array to
    // count the even and odd
    for (let i = 0; i < N; i++) {
 
        // If arr[i] is odd
        if (arr[i] & 1) {
            odd++;
        }
 
        // If arr[i] is even
        else {
            even++;
        }
    }
 
    // Check if count of even
    // is zero or count of odd
    // is zero
    if (even == N || odd == N)
        document.write("Yes");
    else
        document.write("No");
}
 
// Driver's Code
 
let arr = [ 1, 1, 3 ];
let N = arr.length;
 
check(arr, N);
 
 
// This code is contributed by gfgking
</script>


Output: 

Yes

 

Time Complexity: O(N), where N is the length of the given array.

Auxiliary Space: O(1)
 



Similar Reads

Check if the given array can be reduced to zeros with the given operation performed given number of times
Given an array arr[] of N integers and an integer K, the task is to find whether the given array elements can be made 0 if the given operation is applied exactly K times. In a single operation, the smallest element from the array will be subtracted from all the non-zero elements of the array.Examples: Input: arr[] = {1, 1, 2, 3}, K = 3 Output: Yes
6 min read
Check if X and Y can be made zero by using given operation any number of times
Given two integers X and Y, the task is to check if these two integers can be made equal to 0 by using the given operation any number of times. An operation is described as follows - Choose an arbitrary integer Z.Update the values with either of the following values: X = X - 2*Z and Y = Y - 3*ZX = X - 3*Z and Y = Y - 2*Z Examples: Input: X = 6, Y =
5 min read
Minimum number of given operations required to be performed to reduce N to 0
Given an integer N, the task is to reduce N to 0 in the minimum number of operations using the following operations any number of times: Change the rightmost (0th) bit in the binary representation of N.Change the ith bit in the binary representation of N if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0. Examples: Input:
5 min read
Minimize count of given operations required to be performed to make all array elements equal to 1
Given an array, arr[] consisting of N positive integers, the task is to make all array elements equal to 1 by performing the following operations minimum number of times: Increment the value of all elements of a subarray by any positive integers.If all the array elements are even then divide all the array elements by 2. Print the count of minimum o
7 min read
Minimize arithmetic operations to be performed on adjacent elements of given Array to reduce it
Given an array arr[], the task is to perform arithmetic operations (+, -, *, /) on arr[] in sequence. These operations are performed on adjacent elements until array size reduces to 1. Finally, return the reduced value and number of operations required for the same. Refer to examples for more clarity. Examples: Input: arr = {12, 10, 2, 9, 1, 2}Outp
11 min read
Check if any value of x can be made equal to k after some operations
Given a number k, the task is to choose a positive number x(any) initially, such that the value of x can be made equal to k after some operations. The operation is to add x to the current score and double the value of x. Note that you need to perform this operation at least twice to change from k to x. Find this positive number x or return -1, if s
4 min read
Check if Array can be made strictly increasing by merging three consecutive elements M times
Given an array arr[] of length N and an integer M, the task is to determine if a strictly increasing sequence can be formed by merging three consecutive elements exactly M times. Note: Merging three elements means removing all three of them and inserting a single element having value same as the sum of the three at the same position. Examples: Inpu
11 min read
Check if Array elements can be made consecutive by performing given operations
Given an array arr[] of size N, the task is to check if it is possible to make all array elements consecutive by performing any of the following operations: Leave the element unchanged (arr[i] = arr[i])Increment the element by 1Decrement the element by 1 Note: For every array element, one of the above operations can be applied only once. Examples:
11 min read
Maximize array product by changing any array element arr[i] to (-1)*arr[i] - 1 any number of times
Given an array arr[] consisting of N integers, the task is to modify the array elements by changing each array element arr[i] to (-1)*arr[i] - 1 any number of times such that the product of the array is maximized. Examples: Input: arr[] = {-3, 0, 1}Output: 2 -1 -2Explanation:Performing the following operations on the array elements maximizes the pr
7 min read
Check if array sum can be made K by three operations on it
Given an array A of N integers and a positive integer K. Only three operations can be performed on this array: Replace an integer with the negative value of the integer, Add index number (1-based indexing) of the element to the element itself and Subtract index number of the element from the element itself. The task is to check if the given array c
9 min read
Article Tags :
Practice Tags :