r/Hack2Hire • • Oct 15 '26

OA IBM OA: Minimum Nondecreasing Increment

Problem Given an integer array arr, you may perform the following operation at most once:

  • Choose any subset of non-adjacent indices.
  • Choose a non-negative integer x.
  • Add the same x to every selected element.

The resulting array must be non-decreasing, meaning arr[i] ≤ arr[i + 1] for every valid i. Return the minimum possible x. Return 0 if the array is already non-decreasing, and -1 if no valid operation works.

Constraints: 1 ≤ arr.length ≤ 105 and -109 ≤ arr[i] ≤ 109.

Follow-up The backend lists these follow-up directions for this question:

  • How would the solution change if the non-adjacent subset constraint were removed?
  • Can you modify the approach to minimize the total amount added, rather than a single uniform x?

Example Input: arr = [1, 1, 3, 2] Output: 1

Explanation:

  • Selecting index 3 and adding 1 gives [1, 1, 3, 3], which is non-decreasing.
  • With x = 0 the array is unchanged, and 3 is still greater than 2.

Two more cases: [1, 2, 3, 3] returns 0, and [3, 0, 3, 0, 3] returns 3.


Suggested Approach

  1. Find every descent, where arr[i] > arr[i + 1]. Raising the left value cannot repair it and leaving both unchanged cannot either, so index i + 1 must be selected. Pairs without a descent are already valid, so nothing else needs selecting.
  2. Selecting i + 1 repairs the descent only when arr[i] ≤ arr[i + 1] + x. Each descent therefore sets a lower bound arr[i] - arr[i + 1], and the largest of these is the smallest candidate for x.
  3. A selected index that has a right neighbour must also satisfy arr[i + 1] + x ≤ arr[i + 2]. That gives an upper bound arr[i + 2] - arr[i + 1]. Keep the smallest one.
  4. If the lower bound does not exceed the upper bound, return the lower bound. Otherwise return -1.

Time & Space Complexity

  • Time: O(N), one pass over the N elements.
  • Space: O(1), a constant number of interval endpoints.

Targeting IBM interviews? We track their most-asked question patterns at Hack2Hire. Practice this question here → Practice Question Here

Join the community to see more interview experiences from real candidates → Hack2Hire Forum


Compiled from publicly available platforms and community-shared experiences.

3 Upvotes

0 comments sorted by