VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-removals-for-target-sum/

⇱ Minimum Removals for Target Sum - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum Removals for Target Sum

Last Updated : 4 Aug, 2025

Given an array of positive integers arr[] and an integer k, you can remove either the leftmost or rightmost element from the array in one operation. After each operation, the size of arr[] is reduced by one. find the minimum number of operations required to make the total sum of the removed elements exactly equal to k. If it is impossible to achieve the sum k, return -1.

Examples :

Input: arr[] = [3, 4, 1, 3, 2], k = 5
Output: 2
Explanation: Removing 3 from left and 2 from right gives a sum of 5 in 2 operations.

Input: arr[] = [5, 3, 4, 6, 2], k = 6
Output: -1
Explanation: It is impossible to achieve the sum of removed elements as 6.

Input: arr[] = [1, 1, 3, 1, 2], k = 4
Output: 3
Explanation: Removing 1, 1 from left and 2 from right gives a sum of 4 in 3 operations.

[Naive Approach] Recursive Approach - O(2 ^ n) Time and O(n) Space

The idea is to use recursion to explore all combinations of removing elements from the left or right. In each recursive call, we have two choices: either to remove the left most element or remove the right most element. After exploring both the choices, return the minimum of the two. If all elements are exhausted, return -1.


Output
2

[Better Approach] Using Prefix Array - O(n) Time and O(n) Space

If we observe carefully, we can say that after removing the elements whose sum = k, the sum of the remaining elements will be (total sum - k). Since we need to minimize the removals, the problem can be reduced to finding the longest subarray whose sum = (total sum - k).

We can find the longest subarray having sum = (total sum - k) by using a hash map or dictionary to store the first occurrence of each prefix sum. For each index i, we find the prefix sum till index i, say prefSum and find the first occurrence of (prefSum - (total sum - k)) in the hash map, if present. The difference between the first occurrence and the current index i will be the length of the subarray.

To know more about the algorithm, please refer Longest Subarray Having Sum K.


Output
2

[Expected Approach] Using Sliding Window Technique - O(n) Time and O(1) Space

In the previous approach, we observed that in order to find the minimum removals with sum k, we need to find the longest subarray with sum (total sum - k). Since the input array contains only positive numbers, we can further optimize our previous approach by using Sliding Window Technique to find the longest subarray having sum = (total sum - k).

  • Start with a window of size 1 having only the first element.
  • Extend the window until the sum of elements exceeds (total sum - k).
  • Shrink the window from the left until sum of elements <= (total sum - k).
  • If sum of elements == (total sum - k), find the length of window (right - left + 1) and update the maximum length.

Finally, minimum removals = n - maximum length of window having sum as (total sum - k).


Output
2
Comment
Article Tags: