Approach Summary
Equivalent to finding the longest subarray with sum = totalSum - x. Use variable sliding window; maximise subarray length.
How to Recognize This Pattern
- Remove from either end to reach target → find longest middle subarray
- Transform "ends" problem to a centre sliding window
Complexity Analysis
Time Complexity
O(n)
Space Complexity
O(1)
Tags
Array Hash Table Binary Search Sliding Window Prefix Sum