Given an array arr[] of integers and an integer k, the task is to find the minimum number of integers that need to be removed from the array such that the sum of the remaining elements is equal to k. If we cannot get the required sum the print -1.
Examples:
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
Approach: The idea is to use a sliding window and variables j initialize it to 0, min_num to store the answer and sum to store the current sum. Keep on adding the elements of the array to the variable sum, till it becomes greater than or equal to k, if it is equal to k, then update the min_num as minimum of min_num and (n -(i+1) +j) where n is the number of integers in array and i is the current index, else if it is greater than k, then start decrementing the sum by removing the values of the array from sum till the sum becomes less than or equal to k and also increment the value of j, if sum is equal to k, then once again update min_num. Repeat this whole process till the end of the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int FindMinNumber( int arr[], int n, int k)
{
int i = 0;
int j = 0;
int min_num = INT_MAX;
bool found = false ;
int sum = 0;
while (i < n) {
sum = sum + arr[i];
if (sum == k) {
min_num = min(min_num, ((n - (i + 1)) + j));
found = true ;
}
else if (sum > 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;
}
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
class GFG
{
static int FindMinNumber( int arr[], int n, int k)
{
int i = 0 ;
int j = 0 ;
int min_num = Integer.MAX_VALUE;
boolean found = false ;
int sum = 0 ;
while (i < n)
{
sum = sum + arr[i];
if (sum == k)
{
min_num = Math.min(min_num,
((n - (i + 1 )) + j));
found = true ;
}
else if (sum > 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 ;
}
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));
}
}
|
Python3
def FindMinNumber(arr, n, k):
i = 0
j = 0
min_num = 10 * * 9
found = False
Sum = 0
while (i < n):
Sum = Sum + arr[i]
if ( Sum = = k):
min_num = min (min_num,
((n - (i + 1 )) + j))
found = True
elif ( Sum > 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
arr = [ 1 , 3 , 2 , 5 , 6 ]
n = len (arr)
k = 5
print (FindMinNumber(arr, n, k))
|
C#
using System;
class GFG
{
static int FindMinNumber( int [] arr, int n, int k)
{
int i = 0;
int j = 0;
int min_num = int .MaxValue;
bool found = false ;
int sum = 0;
while (i < n)
{
sum = sum + arr[i];
if (sum == k)
{
min_num = Math.Min(min_num,
((n - (i + 1)) + j));
found = true ;
}
else if (sum > 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;
}
public static void Main()
{
int [] arr = { 1, 3, 2, 5, 6 };
int n = arr.Length;
int k = 5;
Console.WriteLine(FindMinNumber(arr, n, k));
}
}
|
PHP
<?php
function FindMinNumber( $arr , $n , $k )
{
$i = 0;
$j = 0;
$min_num = PHP_INT_MAX;
$found = false;
$sum = 0;
while ( $i < $n )
{
$sum = $sum + $arr [ $i ];
if ( $sum == $k )
{
$min_num = min( $min_num ,
(( $n - ( $i + 1)) + $j ));
$found = true;
}
else if ( $sum > $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;
}
$arr = array ( 1, 3, 2, 5, 6 );
$n = sizeof( $arr );
$k = 5;
echo (FindMinNumber( $arr , $n , $k ));
|
Javascript
<script>
function FindMinNumber(arr,n,k)
{
let i = 0;
let j = 0;
let min_num = Number.MAX_VALUE;
let found = false ;
let sum = 0;
while (i < n)
{
sum = sum + arr[i];
if (sum == k)
{
min_num = Math.min(min_num,
((n - (i + 1)) + j));
found = true ;
}
else if (sum > 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;
}
let arr = [1, 3, 2, 5, 6 ];
let n = arr.length;
let k = 5;
document.write(FindMinNumber(arr, n, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)