Longest Subarray with Sum at most S II in O(n) Time Complexity using Python


Given an array of positive integers and a number S, find the longest contiguous subarray having the sum at most S.

Return the start and end indices denoting this subarray.

Example

Input: nums = [3, 2, 5, 2, 2, 1, 1, 3, 1 , 2], S = 11
Output: [3, 8]
Explanation:the subarray nums[3...8] of sum 10

Note:

Your algorithm should run in O(n) time and use O(1) extra space.


Problem Definition

The problem requires finding the longest contiguous subarray within a given array of positive integers such that the sum of the subarray is at most a given number S. The output should be the start and end indices of this subarray.

Input

Output

Constraints

Example

Input: nums = [3, 2, 5, 2, 2, 1, 1, 3, 1, 2], S = 11
Output: [3, 8]
Explanation: The subarray nums[3...8] has a sum of 10, which is the longest subarray with sum at most 11.

Understanding the Problem

The core challenge is to find the longest subarray whose sum does not exceed a given value S. This problem is significant in scenarios where resource constraints are critical, such as memory management, network bandwidth allocation, and more.

Potential pitfalls include misunderstanding the requirement for the subarray to be contiguous and not considering the need for an efficient solution that runs in linear time.

Approach

To solve this problem, we can use the sliding window technique. This approach allows us to maintain a window of elements that we expand and contract to find the longest subarray with the desired sum property.

Naive Solution

A naive solution would involve checking all possible subarrays and their sums, which would result in a time complexity of O(n^2). This is not optimal for large arrays.

Optimized Solution

The sliding window technique provides an optimized solution. The idea is to use two pointers to represent the current window of elements. We expand the window by moving the end pointer and contract it by moving the start pointer to ensure the sum remains within the limit S.

Algorithm

Here is a step-by-step breakdown of the sliding window algorithm:

  1. Initialize two pointers, start and end, both set to 0.
  2. Initialize variables to keep track of the current sum and the maximum length of the subarray found.
  3. Iterate through the array using the end pointer.
  4. Add the current element to the current sum.
  5. While the current sum exceeds S, increment the start pointer and subtract the element at the start pointer from the current sum.
  6. Update the maximum length and the corresponding start and end indices if the current window is longer than the previously found window.
  7. Return the start and end indices of the longest subarray.

Code Implementation

def longest_subarray_with_sum_at_most_s(nums, S):
    # Initialize pointers and variables
    start = 0
    current_sum = 0
    max_length = 0
    result = [-1, -1]
    
    for end in range(len(nums)):
        # Add the current element to the current sum
        current_sum += nums[end]
        
        # While the current sum exceeds S, move the start pointer to the right
        while current_sum > S:
            current_sum -= nums[start]
            start += 1
        
        # Update the maximum length and result indices if a longer subarray is found
        if end - start + 1 > max_length:
            max_length = end - start + 1
            result = [start, end]
    
    return result

# Example usage
nums = [3, 2, 5, 2, 2, 1, 1, 3, 1, 2]
S = 11
print(longest_subarray_with_sum_at_most_s(nums, S))  # Output: [3, 8]

Complexity Analysis

The time complexity of the sliding window approach is O(n) because each element is processed at most twice (once by the end pointer and once by the start pointer). The space complexity is O(1) as we only use a few extra variables.

Edge Cases

Consider the following edge cases:

Testing

To test the solution comprehensively, consider the following test cases:

def test_longest_subarray_with_sum_at_most_s():
    assert longest_subarray_with_sum_at_most_s([3, 2, 5, 2, 2, 1, 1, 3, 1, 2], 11) == [3, 8]
    assert longest_subarray_with_sum_at_most_s([], 5) == [-1, -1]
    assert longest_subarray_with_sum_at_most_s([10, 20, 30], 5) == [-1, -1]
    assert longest_subarray_with_sum_at_most_s([1, 2, 3, 4, 5], 15) == [0, 4]
    assert longest_subarray_with_sum_at_most_s([1, 2, 3, 4, 5], 10) == [1, 4]
    print("All test cases pass")

test_longest_subarray_with_sum_at_most_s()

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to find the longest subarray with a sum at most S using the sliding window technique. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for improving algorithmic thinking and coding skills.

Additional Resources

For further reading and practice, consider the following resources: