Check if sum of array can be reduced to zero by repetitively reducing array element by their index value
Last Updated :
27 Aug, 2021
Given an array arr[] consisting of N integers, the task is to determine if the sum of array elements can be reduced to 0 by performing the following operations any number of times:
- Choose an element A[i] and reduce A[i] by i(1-based indexing), any number of times, possibly 0.
- If the sum can be reduced to 0, print “Yes“. Otherwise, print “No“.
Examples:
Input: arr[] = {2, 3, 1}
Output: Yes
Explanation:
Select A[2] = 3
Perform given operation 3 times to obtain the following result:
3 -> (3 -2) -> (3 – 2 – 2) -> (3 – 2 – 2 – 2)
Sum of the modified array = 2 + (-3) + 1 = 0.
Therefore, the required answer is 0.
Input: arr[] = {-5, 3}
Output: No
Approach: This problem can be solved based on the following observations:
- If a positive number is repetitively subtracted from another positive number, then eventually, 0 can be obtained. But, repetitively subtracting a positive number from a negative number, 0 can never be obtained as it keeps on decreasing negatively.
- The task to reduce the sum to 0 by subtracting i from A[i].
- Therefore, on choosing an element and reducing the value of the element by i, the sum is being reduced by i.
Let the sum of the array be S.
S = A[1] + A[2] + …. + A[N]
After performing given operations, sum of the array modifies to
S2 = (A[i] – (i)) + (A[i+1] – (i+1)) ….
S2 = A[i] + A[i+1]…. – (i + i+1….)
S2 = S – (i + i + 1…..)
Therefore, after every operation, the original sum is reduced.
- Therefore, the task reduces to checking if the initial sum of the array is positive or 0. If found to be true, print “Yes“. Otherwise, print “No“.
Below is the implementation for the above approach:
C++
#include <iostream>
using namespace std;
bool isPossible( int arr[], int n)
{
int S = 0;
for ( int i = 0; i < n; i++) {
S = S + arr[i];
}
if (S >= 0) {
return true ;
}
else {
return false ;
}
}
int main()
{
int arr[] = { -5, 3 };
int n = sizeof (arr) / sizeof ( int );
if (isPossible(arr, n)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
|
Java
import java.io.*;
class GFG {
static boolean isPossible( int [] arr, int n)
{
int S = 0 ;
for ( int i = 0 ; i < n; i++) {
S = S + arr[i];
}
if (S >= 0 )
return true ;
else
return false ;
}
public static void main(String[] args)
{
int arr[] = { - 5 , 3 };
int n = arr.length;
if (isPossible(arr, n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isPossible(arr, n):
S = sum (arr)
if (S > = 0 ):
return true
else :
return false
if __name__ = = '__main__' :
arr = [ - 5 , 3 ]
n = len (arr)
if (isPossible(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isPossible( int [] arr,
int n)
{
int S = 0;
for ( int i = 0; i < n; i++)
{
S = S + arr[i];
}
if (S >= 0)
return true ;
else
return false ;
}
public static void Main()
{
int [] arr = {-5, 3};
int n = arr.Length;
if (isPossible(arr, n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isPossible(arr , n)
{
var S = 0;
for (i = 0; i < n; i++) {
S = S + arr[i];
}
if (S >= 0)
return true ;
else
return false ;
}
var arr = [ -5, 3 ];
var n = arr.length;
if (isPossible(arr, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...