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++
// C++ implementation of the approach #include <bits/stdc++.h> 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
<?php // PHP 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 function FindMinNumber($arr,$n,$k) { $i = 0; $j = 0; // Stores the minimum number of // integers that need to be removed // from the array $min_num = PHP_INT_MAX; $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 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 $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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum Circles needed to be removed so that all remaining circles are non intersecting
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Minimum elements to be removed such that sum of adjacent elements is always even
- Minimum elements to be removed to make count of 0s and 1s equal
- Check if the array has an element which is equal to sum of all the remaining elements
- Element equal to the sum of all the remaining elements
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Find the minimum number of elements that should be removed to make an array good
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Count the subarray with sum strictly greater than the sum of remaining elements
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Check if the array has an element which is equal to product of remaining elements
- Check if the array has an element which is equal to XOR of remaining elements
- Minimum elements to be removed from the ends to make the array sorted
- Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Remove k corner elements to maximize remaining sum
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Average of remaining elements after removing K largest and K smallest elements from array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

