Finding a minimal subarray of n integers of sum k in linear time
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Finding a minimal subarray of `n` integers such that the sum is greater than or equal to `k` is a classic problem that can be solved efficiently. This article will provide a thorough exploration of a linear time solution to this problem using a sliding window approach.
Problem Definition
Given an array `A` of `n` integers and an integer `k`, the goal is to find the minimal (i.e., smallest possible length) subarray such that the sum of the subarray is at least `k`.
Sliding Window Technique
The sliding window technique is an efficient way to solve this type of problem in linear time. This technique involves adjusting the window represented by two pointers that define the current subarray. The algorithm dynamically expands and contracts the window while maintaining the sum of its elements.
Algorithm Steps
- Initialize Pointers and Variables:
- Let `start` and `end` both be initialized to 0. These pointers will denote the bounds of the sliding window.
- Initialize `current_sum` to 0 to keep track of the sum of the elements within the window.
- Initialize `min_length` to infinity as a placeholder for the shortest subarray.
- Expand the Window:
- Iterate `end` from 0 to `n - 1` (inclusive), adding `A[end]` to `current_sum`.
- Check if `current_sum` is greater than or equal to `k`.
- Contract the Window:
- When `current_sum` is greater than or equal to `k`, move the `start` pointer to the right as far as possible while maintaining the condition `current_sum >= k`.
- Each time you achieve this condition, calculate the length of the current window and update `min_length` with the smaller value between the current `min_length` and the current window size (`end - start + 1`).
- Output the Result:
- After traversing the array, if `min_length` is still infinity, it means there's no such subarray, hence return 0 or suitable output. Otherwise, return `min_length`.
Complexity
- Time Complexity: The algorithm runs in time, where n is the size of the input array. This is because each element of the array will be visited at most twice by both the `start` and `end` pointers.
- Space Complexity: The algorithm uses additional space since it only requires a constant amount of extra storage for indices and sum tracking.
Example
Consider the array `A = [2, 3, 1, 2, 4, 3]` and `k = 7`.
- Initialize `start` = 0, `end` = 0, `current_sum` = 0, `min_length` = infinity.
- Move `end` to fill the initial window:
- `end` = 0, `current_sum` = 2 (not enough)
- `end` = 1, `current_sum` = 5 (not enough)
- `end` = 2, `current_sum` = 6 (not enough)
- `end` = 3, `current_sum` = 8 (enough)
- Contract at `start`:
- `current_sum` >= `k`, calculate length: `end - start + 1` = 4
- Move `start`, now `start` = 1, `current_sum` = 6 (not enough)
- Expand `end`:
- `end` = 4, `current_sum` = 10 (enough)
- Contract at `start`:
- Calculate length: `end - start + 1` = 4
- Move `start`, now `start` = 2, `current_sum` = 9 (enough)
- Calculate length: `end - start + 1` = 3
- Move `start`, now `start` = 3, `current_sum` = 7 (enough)
- Calculate length: `end - start + 1` = 2 (new min)
- Move `start`, now `start` = 4, `current_sum` = 4 (not enough)
- `end` moves to 5, `current_sum` = 7 (enough)
- Contract at `start`:
- Calculate length: `end - start + 1` = 2
- End of array reached; minimum subarray length found is 2.
The minimal subarray is `[4, 3]` with a sum greater than or equal to `k`.
Tabular Summary of Approach
| Step | Action | Condition Satisfied | Additional Notes |
| Window Expansion | Increase end and add to current\_sum | --- | Expand as long as sum < k |
| Sum Check and Contraction | Check if current\_sum >= k | Yes, continue to contract | Contract to find minimal length |
| Calculate Length | If current\_sum >= k, update min\_length | Yes | Update minimum length if smaller |
| Result | Conclude after complete iteration | Final check on min\_length | Output 0 if not found |
By following this technique, you can efficiently solve the problem in linear time, which is optimal for large datasets. Adjustments or enhancements, such as early breaking when conditions are met, can further optimize performance in practice.

