Amazon SDE-1 interview | DSA

Anonymous User
0
Jul 28, 2024

Question:- An array is given of positive numbers including 0. At each operation you can choose any non empty subarray and decrement 1 from each element of that subarray. Find minimum number of operation required to make all elements of array equal to 0.

for eg : Arr = [3,1,4,2]

  • 1 : subarray[0:3] -> [2,0,3,1]
  • 2: subarray[2:3] -> [2,0,2,0]
  • 3: subarray[0:0] -> [0,0,2,0] (two times)
  • 4 subarray[2:2] - > [0,0,0,0] (two times)

total => 1+1+2+2 = 6.
So ans = 6

Constraint : Size of array can be 1e5.

How to do it in O(N)?
Comments (0)