20. Minimum Operations to Reduce X to Zero
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
We have to minimixe the left and right element of a array thats meanwe have to maximize the number element of a subarray which present in the array which sum equal to tatolSum-x
To find the max middle subarray element or length
we will use sliding window technique/two pointer approach -
Initially we will set left and right pointer at 0 index
We will move right pointer until CurrSum > totalSum - x
if CurrSum > totalSum - x we will move left pointer and also check the condition that l<=r
Update our maxLength if and only if currSum == totalSum - x
Repeat the above steps until we reach the end of the array.
At last if we did not find any such window we will return -1 as answer
Time Complexity: O(n)
Auxiliary Space Complexity: 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.