Increasing Array
Explanation
In this problem, we are provided with an array containing n
positive integers. Our task is to make this array non-decreasing by only applying the following move minimum number of times.
Move: Add 1
to any element of the array.
This can be done by comparing arr[i]
and arr[i - 1]
where i
ranges from 1
to n - 1
.
If arr[i]
is less than arr[i - 1]
we will add 1
to arr[i]
arr[i - 1] - arr[i]
times.
We'll add arr[i - 1] - arr[i]
to a moves
variable everytime we perform this.
Code
n = int(input())
arr = list(map(int, input().split()))
moves = 0
for i in range(1, n):
if arr[i] < arr[i - 1]:
moves += arr[i - 1] - arr[i]
arr[i] = arr[i - 1]
print(moves)