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:
#include <iostream> using namespace std;
int minIndex( int arr[], int N) {
// Iterate through the array and check for each index
for ( int i = 0; i < N; i++) {
// Multiply the element by -1
arr[i] *= -1;
// Calculate the new sum
int sum = 0;
for ( int j = 0; j < N; j++) {
sum += arr[j];
}
// Check if the new sum is 0
if (sum == 0) {
return (i+1);
}
// Reset the element to its original value
arr[i] *= -1;
}
// If no such index is found, return -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 code implementation import java.io.*;
public class Main {
public static int minIndex( int [] arr, int N) {
// Iterate through the array and check for each index
for ( int i = 0 ; i < N; i++) {
// Multiply the element by -1
arr[i] *= - 1 ;
// Calculate the new sum
int sum = 0 ;
for ( int j = 0 ; j < N; j++) {
sum += arr[j];
}
// Check if the new sum is 0
if (sum == 0 ) {
return (i + 1 );
}
// Reset the element to its original value
arr[i] *= - 1 ;
}
// If no such index is found, return -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));
}
} |
def minIndex(arr, N):
# Iterate through the array and check for each index
for i in range (N):
# Multiply the element by -1
arr[i] * = - 1
# Calculate the new sum
sum = 0
for j in range (N):
sum + = arr[j]
# Check if the new sum is 0
if sum = = 0 :
return (i + 1 )
# Reset the element to its original value
arr[i] * = - 1
# If no such index is found, return -1
return - 1
arr = [ 1 , 3 , - 5 , 3 , 4 ]
n = len (arr)
print (minIndex(arr, n))
|
using System;
public class GFG
{ public static int MinIndex( int [] arr, int N)
{
// Iterate through the array and check for each index
for ( int i = 0; i < N; i++)
{
// Multiply the element by -1
arr[i] *= -1;
// Calculate the new sum
int sum = 0;
for ( int j = 0; j < N; j++)
{
sum += arr[j];
}
// Check if the new sum is 0
if (sum == 0)
{
// Since the array is 0-indexed in C#, return (i + 1) to get the 1-based index
return (i + 1);
}
// Reset the element to its original value
arr[i] *= -1;
}
// If no such index is found, return -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));
}
} |
function minIndex(arr) {
const N = arr.length;
// Iterate through the array and check for each index
for (let i = 0; i < N; i++) {
// Multiply the element by -1
arr[i] *= -1;
// Calculate the new sum
let sum = 0;
for (let j = 0; j < N; j++) {
sum += arr[j];
}
// Check if the new sum is 0
if (sum === 0) {
return i + 1;
}
// Reset the element to its original value
arr[i] *= -1;
}
// If no such index is found, return -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++ program to find minimum index // such that sum becomes 0 when the // element is multiplied by -1 #include <bits/stdc++.h> using namespace std;
// Function to find minimum index // such that sum becomes 0 when the // element is multiplied by -1 int minIndex( int arr[], int n)
{ // Find array sum
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
// Find element with value equal to sum/2
for ( int i = 0; i < n; i++) {
// when sum is equal to 2*element
// then this is our required element
if (2 * arr[i] == sum)
return (i + 1);
}
return -1;
} // Driver code int main()
{ int arr[] = { 1, 3, -5, 3, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minIndex(arr, n) << endl;
return 0;
} |
// Java program to find minimum index // such that sum becomes 0 when the // element is multiplied by -1 import java.io.*;
class GFG {
// Function to find minimum index // such that sum becomes 0 when the // element is multiplied by -1 static int minIndex( int arr[], int n)
{ // Find array sum
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += arr[i];
// Find element with value equal to sum/2
for ( int i = 0 ; i < n; i++) {
// when sum is equal to 2*element
// then this is our required element
if ( 2 * arr[i] == sum)
return (i + 1 );
}
return - 1 ;
} // Driver code public static void main (String[] args) {
int []arr = { 1 , 3 , - 5 , 3 , 4 };
int n =arr.length;
System.out.print( minIndex(arr, n));
}
} // This code is contributed by anuj_67.. |
# Python 3 program to find minimum index # such that sum becomes 0 when the # element is multiplied by -1 # Function to find minimum index # such that sum becomes 0 when the # element is multiplied by -1 def minIndex(arr, n):
# Find array sum
sum = 0
for i in range ( 0 , n):
sum + = arr[i]
# Find element with value
# equal to sum/2
for i in range ( 0 , n):
# when sum is equal to 2*element
# then this is our required element
if ( 2 * arr[i] = = sum ):
return (i + 1 )
return - 1
# Driver code arr = [ 1 , 3 , - 5 , 3 , 4 ];
n = len (arr);
print (minIndex(arr, n))
# This code is contributed # by Akanksha Rai |
// C# program to find minimum index // such that sum becomes 0 when the // element is multiplied by -1 using System;
class GFG {
// Function to find minimum index // such that sum becomes 0 when the // element is multiplied by -1 static int minIndex( int [] arr, int n)
{ // Find array sum
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
// Find element with value equal to sum/2
for ( int i = 0; i < n; i++) {
// when sum is equal to 2*element
// then this is our required element
if (2 * arr[i] == sum)
return (i + 1);
}
return -1;
} // Driver code public static void Main () {
int [] arr = { 1, 3, -5, 3, 4 };
int n =arr.Length;
Console.Write( minIndex(arr, n));
}
} |
<script> // Javascript program to find minimum index // such that sum becomes 0 when the // element is multiplied by -1 // Function to find minimum index // such that sum becomes 0 when the // element is multiplied by -1 function minIndex(arr , n)
{
// Find array sum
var sum = 0;
for (i = 0; i < n; i++)
sum += arr[i];
// Find element with value equal to sum/2
for (i = 0; i < n; i++) {
// when sum is equal to 2*element
// then this is our required element
if (2 * arr[i] == sum)
return (i + 1);
}
return -1;
}
// Driver code
var arr = [ 1, 3, -5, 3, 4 ];
var n = arr.length;
document.write(minIndex(arr, n));
// This code contributed by Rajput-Ji </script> |
<?php // PHP program to find minimum index // such that sum becomes 0 when the // element is multiplied by -1 // Function to find minimum index // such that sum becomes 0 when the // element is multiplied by -1 function minIndex(& $arr , $n )
{ // Find array sum
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += $arr [ $i ];
// Find element with value equal to sum/2
for ( $i = 0; $i < $n ; $i ++)
{
// when sum is equal to 2*element
// then this is our required element
if (2 * $arr [ $i ] == $sum )
return ( $i + 1);
}
return -1;
} // Driver code $arr = array (1, 3, -5, 3, 4 );
$n = sizeof( $arr );
echo (minIndex( $arr , $n ));
// This code is contributed // by Shivi_Aggarwal ?> |
2
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.