- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find minimum operations to reduce X to zero in Python

Suppose we have an array called nums and another value x. In one operation, we can either delete the leftmost or the rightmost element from the array and subtract the value from x. We have to find the minimum number of operations required to reduce x to exactly 0. If it is not possible then return -1.

So, if the input is like nums = [4,2,9,1,4,2,3] x = 9, then the output will be 3 because at first we have to delete left most element 4, so array will be [2,9,1,4,2,3] and x will be 5, then remove right most element 3, so array will be [2,9,1,4,2], and x = 2, then again either left from left or from right to make x = 0, and array will be either [2,9,1,4] or [9,1,4,2].

To solve this, we will follow these steps −

- n := size of nums
- leftMap := a new map
- leftMap[0] := -1
- left := 0
- for i in range 0 to n - 1, do
- left := left + nums[i]
- if left is not in leftMap, then
- leftMap[left] := i

- right := 0
- ans := n + 1
- for i in range n to 0, decrease by 1, do
- if i < n, then
- right := right + nums[i]

- left := x - right
- if left is present in leftMap, then
- ans := minimum of ans and leftMap[left] + 1 + n-i

- if i < n, then
- if ans is same as n + 1, then
- return -1

- return ans

## Example

Let us see the following implementation to get better understanding −

def solve(nums, x): n = len(nums) leftMap = dict() leftMap[0] = -1 left = 0 for i in range(n): left += nums[i] if left not in leftMap: leftMap[left] = i right = 0 ans = n + 1 for i in range(n, -1, -1): if i < n: right += nums[i] left = x - right if left in leftMap: ans = min(ans, leftMap[left] + 1 + n - i) if ans == n + 1: return -1 return ans nums = [4,2,9,1,4,2,3] x = 9 print(solve(nums, x))

## Input

[4,2,9,1,4,2,3], 9

## Output

3

- Related Questions & Answers
- Program to find minimum one bit operations to make integers zero in Python
- Find maximum operations to reduce N to 1 in Python
- Program to find minimum operations to make array equal using Python
- Program to find minimum operations to make the array increasing using Python
- Program to find minimum cost to reduce a list into one integer in Python
- Program to find minimum number of operations to make string sorted in Python
- Minimum operations to make XOR of array zero in C++
- Find maximum operations to reduce N to 1 in C++
- Program to find minimum possible maximum value after k operations in python
- Program to find minimum operations needed to make two arrays sum equal in Python
- Program to find equal sum arrays with minimum number of operations in Python
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum number of operations to move all balls to each box in Python
- Program to find minimum number of operations required to make lists strictly Increasing in python
- C++ program to find minimum how many operations needed to make number 0