给定一个由N个整数组成的数组。我们的任务是找到元素的最小索引,以便将其乘以-1时,整个数组的总和将变为0。如果没有这样的索引,则返回-1。
例子:
Input : arr[] = {1, 3, -5, 3, 4}
Output : 2
Input : arr[] = {5, 3, 6, -7, -4}
Output : -1
天真的方法:
一个简单的解决方案是将每个元素乘以-1,然后检查新的和是否为0。此算法适用于O(N 2 ) 。
高效的方法:
如果我们将S作为数组的初始总和,然后将当前元素A i乘以-1,则新总和将变为S – 2 * A i,并且该总和应等于0。因此,当第一次S = 2时* A i,则当前索引为我们的要求,如果没有元素满足条件,则我们的答案为-1。该算法的时间复杂度为O(N) 。
以下是上述想法的实现:
C++
// C++ program to find minimum index
// such that sum becomes 0 when the
// element is multiplied by -1
#include
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
// 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
# 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#
// 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));
}
}
PHP
输出:
2