Minimum Operations to Make Numbers Non-positive - Problem

You are given a 0-indexed integer array nums and two integers x and y.

In one operation, you must choose an index i such that 0 <= i < nums.length and perform the following:

  • Decrement nums[i] by x
  • Decrement values by y at all indices except the i-th one

Return the minimum number of operations to make all the integers in nums less than or equal to zero.

Input & Output

Example 1 — Basic Case
$ Input: nums = [3,4,1], x = 3, y = 1
Output: 3
💡 Note: Operation 1: Choose i=0, nums becomes [0,3,0]. Operation 2: Choose i=1, nums becomes [-1,0,-1]. Operation 3: Choose i=1, nums becomes [-2,-3,-2]. All numbers ≤ 0 after 3 operations.
Example 2 — Equal x and y
$ Input: nums = [1,2,1], x = 2, y = 2
Output: 1
💡 Note: Since x = y = 2, each operation decreases all numbers by 2. After 1 operation: nums becomes [-1,0,-1]. All numbers ≤ 0.
Example 3 — Large Difference
$ Input: nums = [10], x = 5, y = 1
Output: 2
💡 Note: Only one element. Operation 1: nums[0] becomes 5. Operation 2: nums[0] becomes 0. Need 2 operations to make 10 ≤ 0.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ x, y ≤ 109

Visualization

Tap to expand
Minimum Operations to Make Numbers Non-positiveInput:341x = 3, y = 1Operation (select index 0):030-3 (selected)-1 (others)-1 (others)After 3 operations:≤0≤0≤0Answer: 3 operations
Understanding the Visualization
1
Input
Array with positive numbers and operation parameters x, y
2
Operations
Each operation: decrease selected element by x, others by y
3
Goal
Make all numbers ≤ 0 with minimum operations
Key Takeaway
🎯 Key Insight: Binary search on the answer space and greedily verify feasibility for each candidate
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
23.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen