给定一个正整数数组,请找到可放置在该数组的任何两个元素之间的最小非负整数(即大于或等于零),以使子数组中的元素之和等于该整数之前子数组之后出现的元素的总和,两个子数组中的任何一个都包含新放置的整数。
例子:
Input : arr = {3, 2, 1, 5, 7, 8}
Output : 4
Explanation : The smallest possible number that we can insert is 4, at position 5 (i.e. between 5 and 7) as part of the first subarray so that the sum of the two subarrays becomes equal as, 3+2+1+5+4=15 and 7+8=15.
Input : arr = {3, 2, 2, 3}
Output : 0
Explanation: Equal sum of 5 is obtained by adding first two elements and last two elements as separate subarrays without inserting any extra number. Hence, the smallest integer to be inserted is 0 at position 3.
为了以这样的方式拆分数组,使两个子数组的元素之和相等,一种非常简单明了的方法是从数组的开头一直逐个添加元素,并找出它们之间的差异。总和和其余元素的总和。我们使用迭代循环来做到这一点。对于数组索引0到N-1,我们不断从左到右添加元素,并找到它与剩余总和的差。在第一次迭代期间,为了进行进一步的比较,将获得的差异视为最小。对于其余的迭代,如果获得的差小于早先考虑的最小值,我们将更新最小值。直到循环结束,我们终于获得了可以增加的最小数量。
下面是上述方法的实现:
C++
// C++ program to find the smallest
// number to be added to make the
// sum of left and right subarrays equal
#include
using namespace std;
// Function to find the minimum
// value to be added
int findMinEqualSums(int a[], int N)
{
// Variable to store entire
// array sum
int sum = 0;
for (int i = 0; i < N; i++)
{
sum += a[i];
}
// Variables to store sum of
// subarray1 and subarray 2
int sum1 = 0, sum2 = 0;
// minimum value to be added
int min = INT_MAX;
// Traverse through the array
for (int i = 0; i < N; i++)
{
// Sum of both halves
sum1 += a[i];
sum2 = sum - sum1;
// Calculate minimum number
// to be added
if (abs(sum1 - sum2) < min)
{
min = abs(sum1 - sum2);
}
if (min == 0)
{
break;
}
}
return min;
}
// Driver code
int main()
{
int a[] = { 3, 2, 1, 5, 7, 8 };
// Length of array
int N = sizeof(a) / sizeof(a[0]);
cout << (findMinEqualSums(a, N));
}
// This code is contributed
// by ChitraNayal
Java
// Java program to find the smallest
// number to be added to make the
// sum of left and right subarrays equal
import java.io.*;
import java.util.*;
class GFG
{
// Function to find the minimum
// value to be added
static int findMinEqualSums(int[] a, int N)
{
// Variable to store
// entire array sum
int sum = 0;
for (int i = 0; i < N; i++)
{
sum += a[i];
}
// Variables to store sum of
// subarray1 and subarray 2
int sum1 = 0, sum2 = 0;
// minimum value to be added
int min = Integer.MAX_VALUE;
// Traverse through the array
for (int i = 0; i < N; i++)
{
// Sum of both halves
sum1 += a[i];
sum2 = sum - sum1;
// Calculate minimum number
// to be added
if (Math.abs(sum1 - sum2) < min)
{
min = Math.abs(sum1 - sum2);
}
if (min == 0)
{
break;
}
}
return min;
}
// Driver code
public static void main(String args[])
{
int[] a = { 3, 2, 1, 5, 7, 8 };
// Length of array
int N = a.length;
System.out.println(findMinEqualSums(a, N));
}
}
Python3
import sys
# Python3 program to find the smallest
# number to be added to make the
# sum of left and right subarrays equal
# Function to find the minimum
# value to be added
def findMinEqualSums(a, N):
# Variable to store entire
# array sum
sum = 0
for i in range(0,N):
sum = sum+a[i]
# Variables to store sum of
# subarray1 and subarray 2
sum1 = 0
sum2 = 0
# minimum value to be added
min = sys.maxsize
# Traverse through the array
for i in range(0, N-1):
# Sum of both halves
sum1 += a[i]
sum2 = sum - sum1
# Calculate minimum number
# to be added
if (abs(sum1 - sum2) < min):
min = abs(sum1 - sum2)
if (min == 0) :
break
return min
# Driver code
if __name__=='__main__':
a = [3, 2, 1, 5, 7, 8]
# Length of array
N = len(a)
print(findMinEqualSums(a, N))
# This code is contributed
# by Shivi_Aggarwal
C#
// C# program to find the smallest
// number to be added to make the
// sum of left and right subarrays equal
using System;
class GFG
{
// Function to find the minimum
// value to be added
static int findMinEqualSums(int[] a, int N)
{
// Variable to store
// entire array sum
int sum = 0;
for (int i = 0; i < N; i++)
{
sum += a[i];
}
// Variables to store sum of
// subarray1 and subarray 2
int sum1 = 0, sum2 = 0;
// minimum value to be added
int min = int.MaxValue;
// Traverse through the array
for (int i = 0; i < N; i++)
{
// Sum of both halves
sum1 += a[i];
sum2 = sum - sum1;
// Calculate minimum number
// to be added
if (Math.Abs(sum1 - sum2) < min)
{
min = Math.Abs(sum1 - sum2);
}
if (min == 0)
{
break;
}
}
return min;
}
// Driver code
public static void Main()
{
int[] a = { 3, 2, 1, 5, 7, 8 };
// Length of array
int N = a.Length;
Console.WriteLine(findMinEqualSums(a, N));
}
}
// This code is contributed by shs
PHP
4