Find the element whose multiplication with -1 makes array sum 0
Given an array of N integers. The task is to find the smallest index of an element such that when multiplied by -1 the sum of whole array becomes 0. If there is no such index return -1.
Examples:
Input : arr[] = {1, 3, -5, 3, 4}
Output : 2
Input : arr[] = {5, 3, 6, -7, -4}
Output : -1
Naive Approach: The simple solution will be to take each element, multiply it by -1 and check if the new sum is 0. This algorithm works in O(N2).
Approach:
Here’s a step-by-step approach to solve this problem
- Set the variables sum and min_index to their default values. Set sum=0 and min_index=1.
Iterate from left to right through the array.
- Add each element to the sum variable.
- Check to see if sum= 0. If this is the case, set min_index to the current index and exit the loop.
- Iterate from right to left through the array.
- Subtract each element from the sum.
- Check to see if sum= 0. If it is, and min_index is still -1, change it to the current index.
- Return the value min_index.
Implementation:
C++
#include <iostream>
using namespace std;
int minIndex( int arr[], int N) {
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) {
return (i+1);
}
arr[i] *= -1;
}
return -1;
}
int main()
{
int arr[] = { 1, 3, -5, 3, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minIndex(arr, n) << endl;
return 0;
}
|
Java
import java.io.*;
public class Main {
public static int minIndex( int [] arr, int N) {
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 ) {
return (i + 1 );
}
arr[i] *= - 1 ;
}
return - 1 ;
}
public static void main(String[] args) {
int arr[] = { 1 , 3 , - 5 , 3 , 4 };
int n = arr.length;
System.out.println(minIndex(arr, n));
}
}
|
Python3
def minIndex(arr, N):
for i in range (N):
arr[i] * = - 1
sum = 0
for j in range (N):
sum + = arr[j]
if sum = = 0 :
return (i + 1 )
arr[i] * = - 1
return - 1
arr = [ 1 , 3 , - 5 , 3 , 4 ]
n = len (arr)
print (minIndex(arr, n))
|
C#
using System;
public class GFG
{
public static int MinIndex( int [] arr, int N)
{
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)
{
return (i + 1);
}
arr[i] *= -1;
}
return -1;
}
public static void Main( string [] args)
{
int [] arr = { 1, 3, -5, 3, 4 };
int n = arr.Length;
Console.WriteLine(MinIndex(arr, n));
}
}
|
Javascript
function minIndex(arr) {
const N = arr.length;
for (let i = 0; i < N; i++) {
arr[i] *= -1;
let sum = 0;
for (let j = 0; j < N; j++) {
sum += arr[j];
}
if (sum === 0) {
return i + 1;
}
arr[i] *= -1;
}
return -1;
}
const arr = [1, 3, -5, 3, 4];
const result = minIndex(arr);
console.log(result);
|
Output:
2
Time Complexity: O(N^2), where N represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Efficient Approach: If we take S as our initial sum of the array and we multiply current element Ai by -1 then the new sum will become S – 2*Ai and this should be equal to 0. So when for the first time S = 2*Ai then the current index is our required and if no element satisfies the condition then our answer will be -1. The time complexity of this algorithm is O(N).
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minIndex( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
for ( int i = 0; i < n; i++) {
if (2 * arr[i] == sum)
return (i + 1);
}
return -1;
}
int main()
{
int arr[] = { 1, 3, -5, 3, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minIndex(arr, n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int minIndex( int arr[], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += arr[i];
for ( int i = 0 ; i < n; i++) {
if ( 2 * arr[i] == sum)
return (i + 1 );
}
return - 1 ;
}
public static void main (String[] args) {
int []arr = { 1 , 3 , - 5 , 3 , 4 };
int n =arr.length;
System.out.print( minIndex(arr, n));
}
}
|
Python 3
def minIndex(arr, n):
sum = 0
for i in range ( 0 , n):
sum + = arr[i]
for i in range ( 0 , n):
if ( 2 * arr[i] = = sum ):
return (i + 1 )
return - 1
arr = [ 1 , 3 , - 5 , 3 , 4 ];
n = len (arr);
print (minIndex(arr, n))
|
C#
using System;
class GFG {
static int minIndex( int [] arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
for ( int i = 0; i < n; i++) {
if (2 * arr[i] == sum)
return (i + 1);
}
return -1;
}
public static void Main () {
int [] arr = { 1, 3, -5, 3, 4 };
int n =arr.Length;
Console.Write( minIndex(arr, n));
}
}
|
Javascript
<script>
function minIndex(arr , n)
{
var sum = 0;
for (i = 0; i < n; i++)
sum += arr[i];
for (i = 0; i < n; i++) {
if (2 * arr[i] == sum)
return (i + 1);
}
return -1;
}
var arr = [ 1, 3, -5, 3, 4 ];
var n = arr.length;
document.write(minIndex(arr, n));
</script>
|
PHP
<?php
function minIndex(& $arr , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += $arr [ $i ];
for ( $i = 0; $i < $n ; $i ++)
{
if (2 * $arr [ $i ] == $sum )
return ( $i + 1);
}
return -1;
}
$arr = array (1, 3, -5, 3, 4 );
$n = sizeof( $arr );
echo (minIndex( $arr , $n ));
?>
|
Complexity Analysis:
- Time Complexity: O(N), where N represents the size of the given array.
- Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
04 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...