给定一个整数数组arr []和一个整数k ,任务是找到需要从数组中删除的最小整数数,以使其余元素的总和等于k 。如果我们无法获得所需的总和,请打印-1 。
例子:
Input: arr[] = {1, 2, 3}, k = 3
Output: 1
Either remove 1 and 2 to reduce the array to {3}
or remove 3 to get the array {1, 2}. Both have equal sum i.e. 3
But removing 3 requires only a single removal.
Input: arr[] = {1, 3, 2, 5, 6}, k = 5
Output: 3
方法:这个想法是使用滑动窗口,并且变量j将其初始化为0, min_num用来存储答案,而sum用来存储当前的和。继续将数组的元素添加到变量sum中,直到它变得大于或等于k,如果等于k,则将min_num更新为min_num和(n-(i + 1)+ j)的最小值其中n是数组中的整数数,i是当前索引,否则,如果它大于k,则通过从sum中删除数组的值开始减小和,直到和小于或等于k,并且增加j的值,如果sum等于k,则再次更新min_num 。重复此整个过程,直到数组结束。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum number of
// integers that need to be removed from the
// array to form a sub-array with sum k
int FindMinNumber(int arr[], int n, int k)
{
int i = 0;
int j = 0;
// Stores the minimum number of
// integers that need to be removed
// from the array
int min_num = INT_MAX;
bool found = false;
int sum = 0;
while (i < n) {
sum = sum + arr[i];
// If current sum is equal to
// k, update min_num
if (sum == k) {
min_num = min(min_num, ((n - (i + 1)) + j));
found = true;
}
// If current sum is greater than k
else if (sum > k) {
// Decrement the sum until it
// becomes less than or equal to k
while (sum > k) {
sum = sum - arr[j];
j++;
}
if (sum == k) {
min_num = min(min_num, ((n - (i + 1)) + j));
found = true;
}
}
i++;
}
if (found)
return min_num;
return -1;
}
// Driver code
int main()
{
int arr[] = { 1, 3, 2, 5, 6 };
int n = sizeof(arr) / sizeof(int);
int k = 5;
cout << FindMinNumber(arr, n, k);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the minimum number of
// integers that need to be removed from the
// array to form a sub-array with sum k
static int FindMinNumber(int arr[], int n, int k)
{
int i = 0;
int j = 0;
// Stores the minimum number of
// integers that need to be removed
// from the array
int min_num = Integer.MAX_VALUE;
boolean found = false;
int sum = 0;
while (i < n)
{
sum = sum + arr[i];
// If current sum is equal to
// k, update min_num
if (sum == k)
{
min_num = Math.min(min_num,
((n - (i + 1)) + j));
found = true;
}
// If current sum is greater than k
else if (sum > k)
{
// Decrement the sum until it
// becomes less than or equal to k
while (sum > k)
{
sum = sum - arr[j];
j++;
}
if (sum == k)
{
min_num = Math.min(min_num,
((n - (i + 1)) + j));
found = true;
}
}
i++;
}
if (found)
return min_num;
return -1;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 3, 2, 5, 6 };
int n = arr.length;
int k = 5;
System.out.println(FindMinNumber(arr, n, k));
}
}
// This code is contributed by Code_Mech
Python3
# Python3 implementation of the approach
# Function to return the minimum number of
# integers that need to be removed from the
# array to form a sub-array with Sum k
def FindMinNumber(arr, n, k):
i = 0
j = 0
# Stores the minimum number of
# integers that need to be removed
# from the array
min_num = 10**9
found = False
Sum = 0
while (i < n):
Sum = Sum + arr[i]
# If current Sum is equal to
# k, update min_num
if (Sum == k):
min_num = min(min_num,
((n - (i + 1)) + j))
found = True
# If current Sum is greater than k
elif (Sum > k):
# Decrement the Sum until it
# becomes less than or equal to k
while (Sum > k):
Sum = Sum - arr[j]
j += 1
if (Sum == k):
min_num = min(min_num,
((n - (i + 1)) + j))
found = True
i += 1
if (found):
return min_num
return -1
# Driver code
arr = [1, 3, 2, 5, 6]
n = len(arr)
k = 5
print(FindMinNumber(arr, n, k))
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the minimum number of
// integers that need to be removed from the
// array to form a sub-array with sum k
static int FindMinNumber(int[] arr, int n, int k)
{
int i = 0;
int j = 0;
// Stores the minimum number of
// integers that need to be removed
// from the array
int min_num = int.MaxValue;
bool found = false;
int sum = 0;
while (i < n)
{
sum = sum + arr[i];
// If current sum is equal to
// k, update min_num
if (sum == k)
{
min_num = Math.Min(min_num,
((n - (i + 1)) + j));
found = true;
}
// If current sum is greater than k
else if (sum > k)
{
// Decrement the sum until it
// becomes less than or equal to k
while (sum > k)
{
sum = sum - arr[j];
j++;
}
if (sum == k)
{
min_num = Math.Min(min_num,
((n - (i + 1)) + j));
found = true;
}
}
i++;
}
if (found)
return min_num;
return -1;
}
// Driver code
public static void Main()
{
int[] arr = { 1, 3, 2, 5, 6 };
int n = arr.Length;
int k = 5;
Console.WriteLine(FindMinNumber(arr, n, k));
}
}
// This code is contributed by Code_Mech
PHP
$k)
{
// Decrement the sum until it
// becomes less than or equal to k
while ($sum > $k)
{
$sum = $sum - $arr[$j];
$j++;
}
if ($sum == $k)
{
$min_num =min($min_num,
(($n - ($i + 1)) + $j));
$found = true;
}
}
$i++;
}
if ($found)
return $min_num;
return -1;
}
// Driver code
$arr = array( 1, 3, 2, 5, 6 );
$n = sizeof($arr);
$k = 5;
echo(FindMinNumber($arr, $n, $k));
// This code is contributed by Code_Mech
输出:
3