Skip to content

Instantly share code, notes, and snippets.

View gauravkr0071's full-sized avatar
❤️

Gaurav gauravkr0071

❤️
View GitHub Profile
@gauravkr0071
gauravkr0071 / Minimum Operations to Reduce X to Zero.cpp
Last active August 5, 2021 14:22
Minimum Operations to Reduce X to Zero
/*
https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/
You are given an integer array nums and an integer x. In one operation, you can either remove the leftmost or the rightmost element from the array nums and subtract its value from x. Note that this modifies the array for future operations.
Return the minimum number of operations to reduce x to exactly 0 if it is possible, otherwise, return -1
BASED ON LONGEST SUBARRAY WITH K SUM
Example 1:
Input: nums = [1,1,4,2,3], x = 5
Output: 2
Explanation: The optimal solution is to remove the last two elements to reduce x to zero.
Example 2: