The Wayback Machine - https://web.archive.org/web/20210304202717/https://www.geeksforgeeks.org/minimum-number-of-array-elements-from-either-ends-required-to-be-subtracted-from-x-to-reduce-x-to-0/
Skip to content
Related Articles

Related Articles

Minimum number of array elements from either ends required to be subtracted from X to reduce X to 0
  • Last Updated : 04 Mar, 2021

Given an array nums[] and an integer X, the task is to reduce X to 0 by removing either the leftmost or the rightmost array elements and subtracting its value from X, minimum number of times. If it’s possible to reduce X to 0, print the count of operations required. Otherwise, return -1.

Examples:

Input: nums[] = {3,2,20,1,1,3}, X = 10
Output: 5
Explanation: X (= 10) – 3 – 1 – 1 – 3 – 2 = 0. Therefore, the number of operations required is 5. 

Input: nums[] = {1, 1, 4, 2, 3}, X = 5
Output: 2
Explanation: X (= 5) – 3 – 2 = 0. Therefore, the number of operations required is 2. 

Approach: The given problem can be solved using Two Pointers technique. Follow the steps below to solve the problem. 

  • Maintain two pointers left and right, pointing to the ends of the left and right subarrays excluded from X.
  • Initialize left to consider the entire array, and right to include nothing.
  • Therefore, reduce X by the sum of the array.
  • Now, iterate until left reaches the front of the array. 
    • If the sum of the left and the right subarrays exceeds X (i.e. X < 0), shift left by an index to the left and increase X that element.
    • If the sum of the left and the right subarrays is less than X (i.e. X > 0), shift right pointer by an index to the left and reduce X by that element.
    • If X is found to be 0 at any point, update the minimum number of operations required.
  • Print the minimum number of operations required.
  • Below is the implementation of the above approach:

Python3




# Python Program to implement
# the above approach
  
import math
  
# Function to count the minimum
# number of operations required
# to reduce x to 0
def minOperations(nums, x):
  
    # If sum of the array
    # is less than x
    if sum(nums) < x:
        return -1
  
    # Stores the count
    # of operations
    ans = math.inf
  
    # Two pointers to traverse the array
    l, r = len(nums)-1, len(nums)
  
    # Reduce x by the sum
    # of the entire array
    x -= sum(nums)
  
    # Iterate until l reaches
    # the front of the array
    while l >= 0:
  
        # If sum of elements from
        # the front exceeds x
        if x <= 0:
  
            # Shift towards left
            x += nums[l]
            l -= 1
  
        # If sum exceeds 0
        if x > 0:
  
            # Reduce x by elements
            # from the right
            r -= 1
            x -= nums[r]
  
        # If x is reduced to 0
        if x == 0:
  
            # Update the minimum count
            # of operations required
            ans = min(ans, (l+1) + (len(nums)-r))
  
    return ans if ans < math.inf else -1
  
  
# Driver Code
nums = [1, 1, 4, 2, 3]
x = 5
print(minOperations(nums, x))


Output:

2

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :