📜  平均拆分数组所需的最小正整数

📅  最后修改于: 2022-05-13 01:57:50.966000             🧑  作者: Mango

平均拆分数组所需的最小正整数

给定一个由 N 个正整数组成的数组,任务是找到可以放在数组的任意两个元素之间的最小正整数,使得在它之前出现的子数组中的元素之和等于出现的元素之和在它之后的子数组中,新放置的整数包含在两个子数组中的任何一个中。
例子:

Input : arr = { 3, 2, 1, 5, 7, 8 }
Output : 4
Explanation
The smallest possible number that can be inserted is 4 between elements 5 and 7 
as part of the first subarray so that the sum of the two subarrays becomes 
equal i.e, 3 + 2 + 1 + 5 + 4 = 15 and 7 + 8 = 15.

Input : arr = { 3, 2, 2, 3 }
Output : No Extra Element required
Explanation
Equal sum of 5 is obtained by adding the first two elements and last two elements 
as separate subarrays without inserting any extra number.

方法:让整个数组的总和为S。这个想法是找到左和直到索引i(包括它)。设这个总和为 L。现在子数组 arr i + 1 的总和……。 NS – L 。设这个和为R。由于假设两个子数组的和相等,所以上面得到的两个和L和R中较大的一个,应该减少到这两个和中较小的值和较大的差值sum 和较小的和,将是所需的正整数的值,需要最小化。
遍历时会有两个条件:

  1. L > R :使左右子数组之和相等所需的元素值将是L - R ,如果该值小于先前计算的最小元素的值,则这成为所需的最小元素。显然,该元素将是具有较小和的子数组的一部分,即右子数组就是这种情况
  2. R > L :使左右子数组之和相等所需的元素的值将是R - L ,如果该值小于先前计算的最小元素的值,则这成为所需的最小元素。显然,该元素将是具有较小和的子数组的一部分,即左子数组就是这种情况

下面是上述方法的实现:

C++
// C++ program to find the minimum non-negative
// element required to split the array
// into two subarrays with equal sum
#include 
using namespace std;
 
// Function to return the minimum positive integer
// required to split array into two subarrays with equal sums
int findMinimumSplit(int arr[], int n)
{
 
    // Find the sum of whole array
    int totalSum = 0;
    for (int i = 0; i < n; i++) {
        totalSum += arr[i];
    }
 
    // leftSubarraySum stores the sum of arr[0....i] and
    // rightSubarraySum stores the sum of arr[i + 1....n]
    int leftSubarraySum = 0;
    int rightSubarraySum = 0;
    int minimumElement = INT_MAX;
 
    for (int i = 0; i < n - 1; i++) {
        // Find the left subarray sum and
        // corresponding right subarray sum
        leftSubarraySum += arr[i];
        rightSubarraySum = totalSum - leftSubarraySum;
 
        // if left subarray has larger sum, find the
        // element to be included in the right subarray
        // to make their sums equal
        if (leftSubarraySum > rightSubarraySum) {
            int element = leftSubarraySum - rightSubarraySum;
            if (element < minimumElement) {
                minimumElement = element;
            }
        }
        // the Right subarray has larger sum,
        // find the element to be included in
        // the left subarray to make their sums equal
        else {
            int element = rightSubarraySum - leftSubarraySum;
            if (element < minimumElement) {
                minimumElement = element;
            }
        }
    }
 
    return minimumElement;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 3, 2, 1, 5, 7, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int minimumElement = findMinimumSplit(arr, n);
 
    // If 0 then no insertion is required
    if (minimumElement == 0) {
        cout << "No Extra Element Required" << endl;
    }
    else {
        cout << minimumElement << endl;
    }
 
    return 0;
}


Java
// Java program to find the minimum non-negative
// element required to split the array
// into two subarrays with equal sum
import java.util.*;
 
class solution
{
 
// Function to return the minimum positive integer
// required to split array into two subarrays with equal sums
static int findMinimumSplit(int arr[], int n)
{
 
    // Find the sum of whole array
    int totalSum = 0;
    for (int i = 0; i < n; i++) {
        totalSum += arr[i];
    }
 
    // leftSubarraySum stores the sum of arr[0....i] and
    // rightSubarraySum stores the sum of arr[i + 1....n]
    int leftSubarraySum = 0;
    int rightSubarraySum = 0;
    int minimumElement = Integer.MAX_VALUE;
 
    for (int i = 0; i < n - 1; i++) {
        // Find the left subarray sum and
        // corresponding right subarray sum
        leftSubarraySum += arr[i];
        rightSubarraySum = totalSum - leftSubarraySum;
 
        // if left subarray has larger sum, find the
        // element to be included in the right subarray
        // to make their sums equal
        if (leftSubarraySum > rightSubarraySum) {
            int element = leftSubarraySum - rightSubarraySum;
            if (element < minimumElement) {
                minimumElement = element;
            }
        }
        // the Right subarray has larger sum,
        // find the element to be included in
        // the left subarray to make their sums equal
        else {
            int element = rightSubarraySum - leftSubarraySum;
            if (element < minimumElement) {
                minimumElement = element;
            }
        }
    }
 
    return minimumElement;
}
 
// Driver Code
public static void main(String args[])
{
 
    int arr[] = { 3, 2, 1, 5, 7, 8 };
    int n = arr.length;
 
    int minimumElement = findMinimumSplit(arr, n);
 
    // If 0 then no insertion is required
    if (minimumElement == 0) {
        System.out.println("No Extra Element Required");
    }
    else {
        System.out.println(minimumElement);
    }
 
}
 
}
// This code is contributed by
// Sanjit_Prasad


Python 3
# Python 3 program to find the minimum
# non-negative element required to split
# the array into two subarrays with equal sum
import sys
 
# Function to return the minimum positive
# integer required to split array into two
# subarrays with equal sums
def findMinimumSplit(arr, n):
 
    # Find the sum of whole array
    totalSum = 0
    for i in range(n):
        totalSum += arr[i]
 
    # leftSubarraySum stores the sum of
    # arr[0....i] and rightSubarraySum
    # stores the sum of arr[i + 1....n]
    leftSubarraySum = 0
    rightSubarraySum = 0
    minimumElement = sys.maxsize
 
    for i in range(n - 1):
         
        # Find the left subarray sum and
        # corresponding right subarray sum
        leftSubarraySum += arr[i]
        rightSubarraySum = totalSum - leftSubarraySum
 
        # if left subarray has larger sum, find the
        # element to be included in the right
        # subarray to make their sums equal
        if (leftSubarraySum > rightSubarraySum):
            element = leftSubarraySum - rightSubarraySum
            if (element < minimumElement) :
                minimumElement = element
     
        # the Right subarray has larger sum,
        # find the element to be included in
        # the left subarray to make their sums equal
        else :
            element = rightSubarraySum - leftSubarraySum
            if (element < minimumElement) :
                minimumElement = element
 
    return minimumElement
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 3, 2, 1, 5, 7, 8 ]
    n = len(arr)
 
    minimumElement = findMinimumSplit(arr, n)
 
    # If 0 then no insertion is required
    if (minimumElement == 0):
        print( "No Extra Element Required" )
     
    else :
        print(minimumElement)
 
# This code is contributed by ita_c


C#
// C# program to find the
// minimum non-negative
// element required to split
// the array into two
// subarrays with equal sum
using System;
 
class GFG
{
 
// Function to return the
// minimum positive integer
// required to split array
// into two subarrays with
// equal sums
static int findMinimumSplit(int []arr,
                            int n)
{
 
    // Find the sum
    // of whole array
    int totalSum = 0;
    for (int i = 0; i < n; i++)
    {
        totalSum += arr[i];
    }
 
    // leftSubarraySum stores
    // the sum of arr[0....i]
    // and rightSubarraySum
    // stores the sum of
    // arr[i + 1....n]
    int leftSubarraySum = 0;
    int rightSubarraySum = 0;
    int minimumElement = int.MaxValue;
 
    for (int i = 0; i < n - 1; i++)
    {
        // Find the left subarray
        // sum and corresponding
        // right subarray sum
        leftSubarraySum += arr[i];
        rightSubarraySum = totalSum -
                           leftSubarraySum;
 
        // if left subarray has
        // larger sum, find the
        // element to be included
        // in the right subarray
        // to make their sums equal
        if (leftSubarraySum >
            rightSubarraySum)
        {
            int element = leftSubarraySum -
                          rightSubarraySum;
            if (element < minimumElement)
            {
                minimumElement = element;
            }
        }
         
        // the Right subarray has
        // larger sum, find the
        // element to be included
        // in the left subarray to
        // make their sums equal
        else
        {
            int element = rightSubarraySum -
                          leftSubarraySum;
            if (element < minimumElement)
            {
                minimumElement = element;
            }
        }
    }
 
    return minimumElement;
}
 
// Driver Code
public static void Main ()
{
    int []arr = {3, 2, 1, 5, 7, 8};
    int n = arr.Length;
 
    int minimumElement =
        findMinimumSplit(arr, n);
 
    // If 0 then no
    // insertion is required
    if (minimumElement == 0)
    {
        Console.WriteLine("No Extra " +
                   "Element Required");
    }
    else
    {
        Console.WriteLine(minimumElement);
    }
}
}
 
// This code is contributed
// by anuj_67.


Javascript


输出:
4

时间复杂度: O(N),其中 N 是数组中元素的数量。