Smallest indexed array element required to be flipped to make sum of array equal to 0
Given an array arr[] of size N, the task is to find the smallest indexed array element whose sign needs to be flipped such that the sum of the given array becomes 0. If it is not possible to make the sum of the array equal to 0, then print -1.
Examples:
Input: arr[] = {1, 3, -5, 3, 4}
Output: 1
Explanation:
Flipping the sign of arr[1] modifies arr[] to {1, -3, -5, 3, 4}
Since the sum of array modifies to 0, the required output is 1.
Input: arr[] = {1, 2, 4}
Output: -1
Naive Approach: The simplest approach is to solve this problem is to traverse the array and for each array element, flip the sign of the array element and check if the sum of the array changes to 0 or not. If found to be true, then print the index of the current element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallestIndexArrayElementsFlip(
int arr[], int N)
{
int pos = -1;
for ( int i = 0; i < N; i++) {
arr[i] *= -1;
int sum = 0;
for ( int j = 0; j < N; j++) {
sum += arr[j];
}
if (sum == 0) {
pos = i;
break ;
}
else {
arr[i] *= -1;
}
}
return pos;
}
int main()
{
int arr[] = { 1, 3, -5, 3, 4 };
int N = sizeof (arr)
/ sizeof (arr[0]);
cout << smallestIndexArrayElementsFlip(
arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int smallestIndexArrayElementsFlip( int arr[], int N)
{
int pos = - 1 ;
for ( int i = 0 ; i < N; i++)
{
arr[i] *= - 1 ;
int sum = 0 ;
for ( int j = 0 ; j < N; j++)
{
sum += arr[j];
}
if (sum == 0 ) {
pos = i;
break ;
}
else {
arr[i] *= - 1 ;
}
}
return pos;
}
public static void main (String[] args)
{
int arr[] = { 1 , 3 , - 5 , 3 , 4 };
int N = arr.length;
System.out.println(smallestIndexArrayElementsFlip(arr, N));
}
}
|
Python3
def smallestIndexArrayElementsFlip(arr, N):
pos = - 1
for i in range (N):
arr[i] * = - 1
sum = 0
for j in range (N):
sum + = arr[j]
if ( sum = = 0 ):
pos = i
break
else :
arr[i] * = - 1
return pos
if __name__ = = '__main__' :
arr = [ 1 , 3 , - 5 , 3 , 4 ]
N = len (arr)
print (smallestIndexArrayElementsFlip(arr, N))
|
C#
using System;
class GFG
{
static int smallestIndexArrayElementsFlip( int []arr, int N)
{
int pos = -1;
for ( int i = 0; i < N; i++)
{
arr[i] *= -1;
int sum = 0;
for ( int j = 0; j < N; j++)
{
sum += arr[j];
}
if (sum == 0)
{
pos = i;
break ;
}
else
{
arr[i] *= -1;
}
}
return pos;
}
public static void Main(String[] args)
{
int []arr = { 1, 3, -5, 3, 4 };
int N = arr.Length;
Console.WriteLine(smallestIndexArrayElementsFlip(arr, N));
}
}
|
Javascript
<script>
function smallestIndexArrayElementsFlip(arr, N)
{
var pos = -1;
var i,j;
for (i = 0; i < N; i++) {
arr[i] *= -1;
var sum = 0;
for (j = 0; j < N; j++) {
sum += arr[j];
}
if (sum == 0) {
pos = i;
break ;
}
else {
arr[i] *= -1;
}
}
return pos;
}
var arr = [1, 3, -5, 3, 4];
var N = arr.length;
document.write(smallestIndexArrayElementsFlip(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the following observations:
Let the sum of the given array be ArrSum
Sum of the array after flipping the sign of any array element UpdatedSum = ArrSum – 2 * arr[i]
If UpdatedSum = 0, then arr[i] must be ArrSum / 2.
Follow the steps below to solve the problem:
- Initialize a variable, say ArrSum, to store the sum of the given array.
- Traverse the array and for each array element, check if the value of (2 * arr[i] == ArrSum) is true or not. If found to be true, then print the index of the current element.
- Otherwise, print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallestIndexArrayElementsFlip(
int arr[], int N)
{
int pos = -1;
int ArrSum = 0;
for ( int i = 0; i < N; i++) {
ArrSum += arr[i];
}
for ( int i = 0; i < N; i++) {
if (2 * arr[i] == ArrSum) {
pos = i;
break ;
}
}
return pos;
}
int main()
{
int arr[] = { 1, 3, -5, 3, 4 };
int N = sizeof (arr)
/ sizeof (arr[0]);
cout << smallestIndexArrayElementsFlip(
arr, N);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static int smallestIndexArrayElementsFlip(
int arr[], int N)
{
int pos = - 1 ;
int ArrSum = 0 ;
for ( int i = 0 ; i < N; i++)
{
ArrSum += arr[i];
}
for ( int i = 0 ; i < N; i++)
{
if ( 2 * arr[i] == ArrSum)
{
pos = i;
break ;
}
}
return pos;
}
public static void main (String[] args)
{
int arr[] = { 1 , 3 , - 5 , 3 , 4 };
int N = arr.length;
System.out.println(smallestIndexArrayElementsFlip(
arr, N));
}
}
|
Python3
def smallestIndexArrayElementsFlip(arr, N):
pos = - 1
ArrSum = 0
for i in range (N):
ArrSum + = arr[i]
for i in range (N):
if ( 2 * arr[i] = = ArrSum):
pos = i
break
return pos
arr = [ 1 , 3 , - 5 , 3 , 4 ]
N = len (arr)
print (smallestIndexArrayElementsFlip(
arr, N))
|
C#
using System;
class GFG {
static int smallestIndexArrayElementsFlip( int [] arr,
int N)
{
int pos = -1;
int ArrSum = 0;
for ( int i = 0; i < N; i++) {
ArrSum += arr[i];
}
for ( int i = 0; i < N; i++) {
if (2 * arr[i] == ArrSum) {
pos = i;
break ;
}
}
return pos;
}
static public void Main()
{
int [] arr = new int [] { 1, 3, -5, 3, 4 };
int N = arr.Length;
Console.WriteLine(
smallestIndexArrayElementsFlip(arr, N));
}
}
|
Javascript
<script>
function smallestIndexArrayElementsFlip(
arr, N)
{
let pos = -1;
let ArrSum = 0;
for (let i = 0; i < N; i++)
{
ArrSum += arr[i];
}
for (let i = 0; i < N; i++)
{
if (2 * arr[i] == ArrSum)
{
pos = i;
break ;
}
}
return pos;
}
let arr = [ 1, 3, -5, 3, 4 ];
let N = arr.length;
document.write(smallestIndexArrayElementsFlip(
arr, N));
</script>
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
03 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...