给定一个大小为N的数组arr[] ,任务是找到最小的索引数组元素,其符号需要翻转以使给定数组的总和变为0 。如果无法使数组的总和等于0 ,则打印-1 。
例子:
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
朴素的方法:解决这个问题最简单的方法是遍历数组,对于每个数组元素,翻转数组元素的符号并检查数组的总和是否变为0 。如果发现为真,则打印当前元素的索引。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the smallest indexed
// array element required to be flipped to
// make sum of the given array equal to 0
int smallestIndexArrayElementsFlip(
int arr[], int N)
{
// Stores the required index
int pos = -1;
// Traverse the given array
for (int i = 0; i < N; i++) {
// Flip the sign of current element
arr[i] *= -1;
// Stores the sum of array elements
int sum = 0;
// Find the sum of the array
for (int j = 0; j < N; j++) {
// Update sum
sum += arr[j];
}
// If sum is equal to 0
if (sum == 0) {
// Update pos
pos = i;
break;
}
// Reset the current element
// to its original value
else {
// Reset the value of arr[i]
arr[i] *= -1;
}
}
return pos;
}
// Driver Code
int main()
{
int arr[] = { 1, 3, -5, 3, 4 };
int N = sizeof(arr)
/ sizeof(arr[0]);
cout << smallestIndexArrayElementsFlip(
arr, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG {
// Function to find the smallest indexed
// array element required to be flipped to
// make sum of the given array equal to 0
static int smallestIndexArrayElementsFlip(int arr[], int N)
{
// Stores the required index
int pos = -1;
// Traverse the given array
for (int i = 0; i < N; i++)
{
// Flip the sign of current element
arr[i] *= -1;
// Stores the sum of array elements
int sum = 0;
// Find the sum of the array
for (int j = 0; j < N; j++)
{
// Update sum
sum += arr[j];
}
// If sum is equal to 0
if (sum == 0) {
// Update pos
pos = i;
break;
}
// Reset the current element
// to its original value
else {
// Reset the value of arr[i]
arr[i] *= -1;
}
}
return pos;
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 1, 3, -5, 3, 4 };
int N = arr.length;
System.out.println(smallestIndexArrayElementsFlip(arr, N));
}
}
// This code is contributed by AnkThon
Python3
# Python3 program to implement
# the above approach
# Function to find the smallest indexed
# array element required to be flipped to
# make sum of the given array equal to 0
def smallestIndexArrayElementsFlip(arr, N):
# Stores the required index
pos = -1
# Traverse the given array
for i in range(N):
# Flip the sign of current element
arr[i] *= -1
# Stores the sum of array elements
sum = 0
# Find the sum of the array
for j in range(N):
# Update sum
sum += arr[j]
# If sum is equal to 0
if (sum == 0):
# Update pos
pos = i
break
# Reset the current element
# to its original value
else:
# Reset the value of arr[i]
arr[i] *= -1
return pos
# Driver Code
if __name__ == '__main__':
arr = [ 1, 3, -5, 3, 4 ]
N = len(arr)
print(smallestIndexArrayElementsFlip(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find the smallest indexed
// array element required to be flipped to
// make sum of the given array equal to 0
static int smallestIndexArrayElementsFlip(int []arr, int N)
{
// Stores the required index
int pos = -1;
// Traverse the given array
for (int i = 0; i < N; i++)
{
// Flip the sign of current element
arr[i] *= -1;
// Stores the sum of array elements
int sum = 0;
// Find the sum of the array
for (int j = 0; j < N; j++)
{
// Update sum
sum += arr[j];
}
// If sum is equal to 0
if (sum == 0)
{
// Update pos
pos = i;
break;
}
// Reset the current element
// to its original value
else
{
// Reset the value of arr[i]
arr[i] *= -1;
}
}
return pos;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 1, 3, -5, 3, 4 };
int N = arr.Length;
Console.WriteLine(smallestIndexArrayElementsFlip(arr, N));
}
}
// This code is contributed by shikhasingrajput
Javascript
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the smallest indexed
// array element required to be flipped to
// make sum of the given array equal to 0
int smallestIndexArrayElementsFlip(
int arr[], int N)
{
// Stores the required index
int pos = -1;
// Stores the sum of the array
int ArrSum = 0;
// Traverse the given array
for (int i = 0; i < N; i++) {
// Update ArrSum
ArrSum += arr[i];
}
// Traverse the given array
for (int i = 0; i < N; i++) {
// If sum of array elements double
// the value of the current element
if (2 * arr[i] == ArrSum) {
// Update pos
pos = i;
break;
}
}
return pos;
}
// Driver Code
int main()
{
int arr[] = { 1, 3, -5, 3, 4 };
int N = sizeof(arr)
/ sizeof(arr[0]);
cout << smallestIndexArrayElementsFlip(
arr, N);
return 0;
}
Java
// Java program for above approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
// Function to find the smallest indexed
// array element required to be flipped to
// make sum of the given array equal to 0
static int smallestIndexArrayElementsFlip(
int arr[], int N)
{
// Stores the required index
int pos = -1;
// Stores the sum of the array
int ArrSum = 0;
// Traverse the given array
for (int i = 0; i < N; i++)
{
// Update ArrSum
ArrSum += arr[i];
}
// Traverse the given array
for (int i = 0; i < N; i++)
{
// If sum of array elements double
// the value of the current element
if (2 * arr[i] == ArrSum)
{
// Update pos
pos = i;
break;
}
}
return pos;
}
// Driver function
public static void main (String[] args)
{
int arr[] = { 1, 3, -5, 3, 4 };
int N = arr.length;
System.out.println(smallestIndexArrayElementsFlip(
arr, N));
}
}
// This code is contributed by offbeat
Python3
# Python program to implement
# the above approach
# Function to find the smallest indexed
# array element required to be flipped to
# make sum of the given array equal to 0
def smallestIndexArrayElementsFlip(arr, N):
# Stores the required index
pos = -1
# Stores the sum of the array
ArrSum = 0
# Traverse the given array
for i in range(N):
# Update ArrSum
ArrSum += arr[i]
# Traverse the given array
for i in range(N):
# If sum of array elements double
# the value of the current element
if (2 * arr[i] == ArrSum):
# Update pos
pos = i
break
return pos
# Driver Code
arr = [1, 3, -5, 3, 4]
N = len(arr)
print(smallestIndexArrayElementsFlip(
arr, N))
# This code is contributed by Dharanendra L V
C#
// C# program for above approach
using System;
class GFG {
// Function to find the smallest indexed
// array element required to be flipped to
// make sum of the given array equal to 0
static int smallestIndexArrayElementsFlip(int[] arr,
int N)
{
// Stores the required index
int pos = -1;
// Stores the sum of the array
int ArrSum = 0;
// Traverse the given array
for (int i = 0; i < N; i++) {
// Update ArrSum
ArrSum += arr[i];
}
// Traverse the given array
for (int i = 0; i < N; i++) {
// If sum of array elements double
// the value of the current element
if (2 * arr[i] == ArrSum) {
// Update pos
pos = i;
break;
}
}
return pos;
}
// Driver function
static public void Main()
{
int[] arr = new int[] { 1, 3, -5, 3, 4 };
int N = arr.Length;
Console.WriteLine(
smallestIndexArrayElementsFlip(arr, N));
}
}
// This code is contributed by Dharanendra L V
Javascript
1
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:为了优化上述方法,该想法基于以下观察:
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.
请按照以下步骤解决问题:
- 初始化一个变量,比如ArrSum ,以存储给定数组的总和。
- 遍历数组,对于每个数组元素,检查(2 * arr[i] == ArrSum) 的值是否为真。如果发现为真,则打印当前元素的索引。
- 否则,打印-1 。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the smallest indexed
// array element required to be flipped to
// make sum of the given array equal to 0
int smallestIndexArrayElementsFlip(
int arr[], int N)
{
// Stores the required index
int pos = -1;
// Stores the sum of the array
int ArrSum = 0;
// Traverse the given array
for (int i = 0; i < N; i++) {
// Update ArrSum
ArrSum += arr[i];
}
// Traverse the given array
for (int i = 0; i < N; i++) {
// If sum of array elements double
// the value of the current element
if (2 * arr[i] == ArrSum) {
// Update pos
pos = i;
break;
}
}
return pos;
}
// Driver Code
int main()
{
int arr[] = { 1, 3, -5, 3, 4 };
int N = sizeof(arr)
/ sizeof(arr[0]);
cout << smallestIndexArrayElementsFlip(
arr, N);
return 0;
}
Java
// Java program for above approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
// Function to find the smallest indexed
// array element required to be flipped to
// make sum of the given array equal to 0
static int smallestIndexArrayElementsFlip(
int arr[], int N)
{
// Stores the required index
int pos = -1;
// Stores the sum of the array
int ArrSum = 0;
// Traverse the given array
for (int i = 0; i < N; i++)
{
// Update ArrSum
ArrSum += arr[i];
}
// Traverse the given array
for (int i = 0; i < N; i++)
{
// If sum of array elements double
// the value of the current element
if (2 * arr[i] == ArrSum)
{
// Update pos
pos = i;
break;
}
}
return pos;
}
// Driver function
public static void main (String[] args)
{
int arr[] = { 1, 3, -5, 3, 4 };
int N = arr.length;
System.out.println(smallestIndexArrayElementsFlip(
arr, N));
}
}
// This code is contributed by offbeat
蟒蛇3
# Python program to implement
# the above approach
# Function to find the smallest indexed
# array element required to be flipped to
# make sum of the given array equal to 0
def smallestIndexArrayElementsFlip(arr, N):
# Stores the required index
pos = -1
# Stores the sum of the array
ArrSum = 0
# Traverse the given array
for i in range(N):
# Update ArrSum
ArrSum += arr[i]
# Traverse the given array
for i in range(N):
# If sum of array elements double
# the value of the current element
if (2 * arr[i] == ArrSum):
# Update pos
pos = i
break
return pos
# Driver Code
arr = [1, 3, -5, 3, 4]
N = len(arr)
print(smallestIndexArrayElementsFlip(
arr, N))
# This code is contributed by Dharanendra L V
C#
// C# program for above approach
using System;
class GFG {
// Function to find the smallest indexed
// array element required to be flipped to
// make sum of the given array equal to 0
static int smallestIndexArrayElementsFlip(int[] arr,
int N)
{
// Stores the required index
int pos = -1;
// Stores the sum of the array
int ArrSum = 0;
// Traverse the given array
for (int i = 0; i < N; i++) {
// Update ArrSum
ArrSum += arr[i];
}
// Traverse the given array
for (int i = 0; i < N; i++) {
// If sum of array elements double
// the value of the current element
if (2 * arr[i] == ArrSum) {
// Update pos
pos = i;
break;
}
}
return pos;
}
// Driver function
static public void Main()
{
int[] arr = new int[] { 1, 3, -5, 3, 4 };
int N = arr.Length;
Console.WriteLine(
smallestIndexArrayElementsFlip(arr, N));
}
}
// This code is contributed by Dharanendra L V
Javascript
1
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。