📌  相关文章
📜  需要翻转以使数组之和等于0的最小索引数组元素

📅  最后修改于: 2021-04-21 21:29:44             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是找到最小的索引数组元素,其符号需要翻转,以使给定数组的总和变为0 。如果不可能使数组的总和等于0 ,则打印-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


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


输出:
1

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:为了优化上述方法,该思想基于以下观察结果:

请按照以下步骤解决问题:

  • 初始化一个变量,例如ArrSum ,以存储给定数组的总和。
  • 遍历数组,对于每个数组元素,检查(2 * arr [i] == ArrSum)的值是否为true。如果发现为真,则打印当前元素的索引。
  • 否则,打印-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

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
输出:
1

时间复杂度: O(N)
辅助空间: O(1)