Check if all elements of the given array can be made 0 by decrementing value in pairs
Last Updated :
19 Jan, 2022
Given an array arr[] consisting of positive integers, the task is to check if all elements of the given array can be made 0 by performing the following operation:
- Choose two indices i and j such that i != j and subtract 1 from both arr[i] and arr[j]
- The above operation can be performed any number of times
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: Yes
Explanation:
First, choose values 2 and 4 and perform the above operation 2 times. Then the array becomes 1 0 3 2.
Now choose 1 and 3 and apply above operation once to get 0 0 2 2.
Now pick two 2s and perform the above operation twice.
Finally array becomes 0 0 0 0.
Input: arr[] = {5, 5, 5, 5, 5}
Output: No
Approach: On observing the problem carefully, it can be observed that if there is only 1 element or the sum of all the elements is odd, then it is not possible to make all elements 0. Since at every iteration, 2 is being subtracted from the sum of all elements, therefore, the array can become 0 only if the sum of all elements of the array is even. And also, it is possible to make the array 0 when the largest number in the array is less than or equal to the sum of remaining elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void canMake( int n, int ar[])
{
int sum = 0, maxx = -1;
for ( int i = 0; i < n; i++) {
sum += ar[i];
maxx = max(maxx, ar[i]);
}
if (n == 1 || sum % 2 == 1
|| sum - maxx < maxx) {
cout << "No\n" ;
}
else {
cout << "Yes\n" ;
}
}
int main()
{
int n = 6;
int arr[] = { 1, 1, 2, 3, 6, 11 };
canMake(n, arr);
return 0;
}
|
Java
class GFG
{
static void canMake( int n, int ar[])
{
int sum = 0 , maxx = - 1 ;
for ( int i = 0 ; i < n; i++)
{
sum += ar[i];
maxx = Math.max(maxx, ar[i]);
}
if (n == 1 || sum % 2 == 1
|| sum - maxx < maxx)
{
System.out.print( "No\n" );
}
else
{
System.out.print( "Yes\n" );
}
}
public static void main(String[] args)
{
int n = 6 ;
int arr[] = { 1 , 1 , 2 , 3 , 6 , 11 };
canMake(n, arr);
}
}
|
Python3
def canMake(n, ar) :
sum = 0 ; maxx = - 1 ;
for i in range (n) :
sum + = ar[i];
maxx = max (maxx, ar[i]);
if (n = = 1 or sum % 2 = = 1
or sum - maxx < maxx) :
print ( "No" );
else :
print ( "Yes" );
if __name__ = = "__main__" :
n = 6 ;
arr = [ 1 , 1 , 2 , 3 , 6 , 11 ];
canMake(n, arr);
|
C#
using System;
class GFG
{
static void canMake( int n, int []ar)
{
int sum = 0, maxx = -1;
for ( int i = 0; i < n; i++)
{
sum += ar[i];
maxx = Math.Max(maxx, ar[i]);
}
if (n == 1 || sum % 2 == 1
|| sum - maxx < maxx)
{
Console.Write( "No\n" );
}
else
{
Console.Write( "Yes\n" );
}
}
public static void Main(String[] args)
{
int n = 6;
int []arr = { 1, 1, 2, 3, 6, 11 };
canMake(n, arr);
}
}
|
Javascript
<script>
function canMake(n, ar)
{
var sum = 0, maxx = -1;
for ( var i = 0; i < n; i++) {
sum += ar[i];
maxx = Math.max(maxx, ar[i]);
}
if (n == 1 || sum % 2 == 1
|| sum - maxx < maxx) {
document.write( "No" );
}
else {
document.write( "Yes" );
}
}
var n = 6;
var arr = [1, 1, 2, 3, 6, 11];
canMake(n, arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...