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++
#include <bits/stdc++.h>
using namespace std;
bool check( int arr[], int N)
{
int even = 0;
int odd = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] & 1) {
odd++;
}
else {
even++;
}
}
if (even == N || odd == N)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int arr[] = { 1, 1, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
check(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void check( int arr[], int N)
{
int even = 0 ;
int odd = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] % 2 == 1 ) {
odd++;
}
else {
even++;
}
}
if (even == N || odd == N)
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 3 };
int N = arr.length;
check(arr, N);
}
}
|
Python3
def check(arr, N):
even = 0 ;
odd = 0 ;
for i in range (N):
if (arr[i] % 2 = = 1 ):
odd + = 1 ;
else :
even + = 1 ;
if (even = = N or odd = = N):
print ( "Yes" );
else :
print ( "No" );
if __name__ = = '__main__' :
arr = [ 1 , 1 , 3 ];
N = len (arr);
check(arr, N);
|
C#
using System;
class GFG{
static void check( int []arr, int N)
{
int even = 0;
int odd = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] % 2 == 1) {
odd++;
}
else {
even++;
}
}
if (even == N || odd == N)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main(String[] args)
{
int []arr = { 1, 1, 3 };
int N = arr.Length;
check(arr, N);
}
}
|
Javascript
<script>
function check(arr, N)
{
let even = 0;
let odd = 0;
for (let i = 0; i < N; i++) {
if (arr[i] & 1) {
odd++;
}
else {
even++;
}
}
if (even == N || odd == N)
document.write( "Yes" );
else
document.write( "No" );
}
let arr = [ 1, 1, 3 ];
let N = arr.length;
check(arr, N);
</script>
|
Time Complexity: O(N), where N is the length of the given array.
Auxiliary Space: O(1)