Given an integer array arr of length n, you can perform the following operation on the array at most once:
x, and increase all the selected elements by x.
Determine the minimum non-negative integer x, required to make the array arr non-decreasing using at most one operation. If it is impossible, return -1.
Complete the function getMinimumIncrement in the editor with the following parameter(s):
int arr[n]: the array
Returns
int: the minimum value of increment so that the array becomes non-decreasing
Examples
01 · Example 1
arr = [1, 1, 3, 2] return = 1
Some of the possible ways are:
- Select the element with index 3 (0-based indexing) and increment = 1 to get the array
arr = [1, 1, 3, 3]. - Select the elements with indices [1, 3] (0-based indexing) and increment = 2 to get the array
arr = [1, 3, 3, 4].
More IBM problems
- Count Descending SubarraysOA · Seen Apr 2026
- Count Power Products in RangeOA · Seen Apr 2026
- Minimum Operations to Make Alternating Binary StringSeen Feb 2026
- Minimum Number of Non-Empty Disjoint SegmentsSeen Feb 2026
- Count Unstable ProcessesOA · Seen Feb 2026
- Longest Balanced Binary SubarrayOA · Seen Feb 2026
- Process Execution TimeOA · Seen Nov 2025
- Service Timeout DetectionOA · Seen Nov 2025
public int getMinimumIncrement(int[] arr) {
// write your code here
}
arr[1, 1, 3, 2]
expected1
sign in to submit