Making zero array by decrementing pairs of adjacent
Last Updated :
12 Aug, 2022
Given a sequence of non-negative integers, say a1, a2, …, an. Only following actions can be performed on the given sequence:
- Subtract 1 from a[i] and a[i+1] both.
Find if the series can be modified into all zeros using any required number of above operations.
Examples :
Input : 1 2
Output : NO
Explanation: Only two elements, if we subtract
1 then it will convert into [0, 1].
so answer is NO.
Input : 0 1 1 0
Output : YES
Explanation: Here we can choose both 1 and
subtract 1 from them then array becomes [0, 0, 0,
0].
So answer is YES.
Input : 1 2 3 4
Output : NO
Explanation: if we try to subtract 1 any
number of times then array will be [0, 0, 0, 1].
[1, 2, 3, 4]->[0, 1, 3, 4]->[0, 0, 2, 3]->
[0, 0, 1, 2]->[0, 0, 0, 1].
Approach 1 :
If all adjacent elements(i, i+1) in array are equal and total number of element in array is even then it’s all element can be converted to zero. For example, if array elements are like {1, 1, 2, 2, 3, 3} then its all element is convertible into zero.
Then in this case sum of every odd positioned value are always equal to the sum of even positioned value.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossibleToZero( int a[], int n)
{
int even = 0, odd = 0;
for ( int i = 0; i < n; i++)
{
if (i & 1)
odd += a[i];
else
even += a[i];
}
return (odd == even);
}
int main()
{
int arr[] = { 0, 1, 1, 0 };
int n = sizeof (arr) / sizeof (arr[0]);
if (isPossibleToZero(arr, n))
cout << "YES" ;
else
cout << "NO" ;
}
|
Java
import java.io.*;
class GFG
{
static boolean isPossibleToZero( int a[],
int n)
{
int even = 0 , odd = 0 ;
for ( int i = 0 ; i < n; i++)
{
if ((i & 1 ) == 0 )
odd += a[i];
else
even += a[i];
}
return (odd == even);
}
public static void main(String[] args)
{
int arr[] = { 0 , 1 , 1 , 0 };
int n = arr.length;
if (isPossibleToZero(arr, n))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def isPossibleToZero(a, n):
even = 0 ;
odd = 0 ;
for i in range (n):
if (i & 1 ):
odd + = a[i];
else :
even + = a[i];
return (odd = = even);
arr = [ 0 , 1 , 1 , 0 ];
n = len (arr);
if (isPossibleToZero(arr, n)):
print ( "YES" );
else :
print ( "NO" );
|
C#
using System;
class GFG
{
static bool isPossibleToZero( int []a,
int n)
{
int even = 0, odd = 0;
for ( int i = 0; i < n; i++)
{
if ((i & 1) == 0)
odd += a[i];
else
even += a[i];
}
return (odd == even);
}
static public void Main ()
{
int []arr = {0, 1, 1, 0};
int n = arr.Length;
if (isPossibleToZero(arr, n))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
function isPossibleToZero( $a , $n )
{
$even = 0; $odd = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $i & 1)
$odd += $a [ $i ];
else
$even += $a [ $i ];
}
return ( $odd == $even );
}
$arr = array (0, 1, 1, 0);
$n = sizeof( $arr );
if (isPossibleToZero( $arr , $n ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function isPossibleToZero(a, n)
{
let even = 0, odd = 0;
for (let i = 0; i < n; i++)
{
if ((i & 1) == 0)
odd += a[i];
else
even += a[i];
}
return (odd == even);
}
let arr = [ 0, 1, 1, 0 ];
let n = arr.length;
if (isPossibleToZero(arr, n))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Approach 2: If Number formed by given array element is divisible by 11 then all elements of array also can be convertible to zero.
For ex: given array {0, 1, 1, 0}, number formed by this array is 110 then it is divisible by 11. So all elements can be converted into zero.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossibleToZero( int a[], int n)
{
int num = 0;
for ( int i = 0; i < n; i++)
num = num * 10 + a[i];
return (num % 11 == 0);
}
int main()
{
int arr[] = { 0, 1, 1, 0 };
int n = sizeof (arr) / sizeof (arr[0]);
if (isPossibleToZero(arr, n))
cout << "YES" ;
else
cout << "NO" ;
}
|
Java
import java.io.*;
class GFG {
static boolean isPossibleToZero( int a[], int n)
{
int num = 0 ;
for ( int i = 0 ; i < n; i++)
num = num * 10 + a[i];
return (num % 11 == 0 );
}
public static void main (String[] args)
{
int arr[] = { 0 , 1 , 1 , 0 };
int n = arr.length;
if (isPossibleToZero(arr, n))
System.out.println( "YES" );
else
System.out.println ( "NO" );
}
}
|
Python3
def isPossibleToZero(a, n):
num = 0 ;
for i in range (n):
num = num * 10 + a[i];
return (num % 11 = = 0 );
arr = [ 0 , 1 , 1 , 0 ];
n = len (arr);
if (isPossibleToZero(arr, n)):
print ( "YES" );
else :
print ( "NO" );
|
C#
using System;
class GFG
{
static bool isPossibleToZero( int [] a, int n)
{
int num = 0;
for ( int i = 0; i < n; i++)
num = num * 10 + a[i];
return (num % 11 == 0);
}
public static void Main()
{
int [] arr = {0, 1, 1, 0};
int n = arr.Length;
if (isPossibleToZero(arr, n))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
function isPossibleToZero( $a , $n )
{
$num = 0;
for ( $i = 0; $i < $n ; $i ++)
$num = $num * 10 + $a [ $i ];
return ( $num % 11 == 0);
}
$arr = array ( 0, 1, 1, 0 );
$n = sizeof( $arr );
if (isPossibleToZero( $arr , $n ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function isPossibleToZero(a, n)
{
let num = 0;
for (let i = 0; i < n; i++)
num = num * 10 + a[i];
return (num % 11 == 0);
}
let arr = [ 0, 1, 1, 0 ];
let n = arr.length;
if (isPossibleToZero(arr, n))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
The above implementation causes overflow for slightly bigger arrays. We can use below method to avoid overflow.Check if a large number is divisible by 11 or not
Share your thoughts in the comments
Please Login to comment...