19. Subarray with given sum
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To solve this I am using a two-pointer approach to find the subarray with the given sum.
Initialize two pointers, i
and j
, for starting and ending pointers respectively. Initially, both point to the beginning of the array.
Initialize a variable sum
to keep track of the current sum of elements between i
and j
.
Iterate through the array with ending pointer j
, adding each element to sum
.
If sum
becomes greater than the target sum s
, increment i
and subtract arr[i]
from sum
until sum
is less than or equal to s
.
If sum
becomes equal to s
, we have found a subarray with the given sum. Record the indices i+1
and j+1
(1-based indices) and return them.
If the loop completes without finding a subarray, return [-1]
to indicate no such subarray exists.
Suppose we have an array arr = [1, 2, 3, 7, 5]
and the target sum s = 12
.
so the answer for the above example is 2, 4
.
Time Complexity: The algorithm uses a single pass through the array, so the time complexity is O(n)
, where n
is the size of the input array.
Auxiliary Space Complexity: The algorithm uses a constant amount of extra space for variables, so the auxiliary space complexity is O(1)
.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.