Minimum operations to make an array zero by subtracting from every non-zero element

Quick Overview

Find the minimum number of operations that turn an array of non-negative integers into all zeros, where each operation subtracts the same positive amount, no larger than the smallest non-zero element, from every non-zero element. It tests reasoning about what an operation preserves and a simple hashing pass.

Minimum operations to make an array zero by subtracting from every non-zero element

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an integer array `nums` of non-negative integers. In one operation you choose a positive integer `x` that is less than or equal to the smallest non-zero element of `nums`, and subtract `x` from every non-zero element of `nums`. Return the minimum number of operations required to reduce all elements of `nums` to zero. ### Function Signature ```python def minimum_operations(nums: list[int]) -> int: ``` ### Rules - Elements equal to `0` are left unchanged by every operation. - The subtraction applies to all non-zero elements at once. Because `x` never exceeds the smallest non-zero element, no element becomes negative. - If every element is already `0`, return `0`. ### Constraints - `1 <= len(nums) <= 10^5` - `0 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [1,5,0,3,5] Output: 3 ``` Choose `x = 1` to get `[0,4,0,2,4]`, then `x = 2` to get `[0,2,0,0,2]`, then `x = 2` again to get `[0,0,0,0,0]`. ```text Input: nums = [0] Output: 0 ``` The array is already all zeros, so no operation is needed. ```text Input: nums = [4,4,4] Output: 1 ``` Choosing `x = 4` turns every element into 0 in a single operation.

Overview: Find the minimum number of operations that turn an array of non-negative integers into all zeros, where each operation subtracts the same positive amount, no larger than the smallest non-zero element, from every non-zero element. It tests reasoning about what an operation preserves and a simple hashing pass.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array nums of non-negative integers. In one operation you choose a positive integer x that is less than or equal to the smallest non-zero element of nums, and subtract x from every non-zero element of nums.

Return the minimum number of operations required to reduce all elements of nums to zero.

Function Signature

def minimum_operations(nums: list[int]) -> int:

Rules

  • Elements equal to 0 are left unchanged by every operation.
  • The subtraction applies to all non-zero elements at once. Because x never exceeds the smallest non-zero element, no element becomes negative.
  • If every element is already 0 , return 0 .

Constraints

  • 1 <= len(nums) <= 10^5
  • 0 <= nums[i] <= 10^9

Examples

Input: nums = [1,5,0,3,5]
Output: 3

Choose x = 1 to get [0,4,0,2,4], then x = 2 to get [0,2,0,0,2], then x = 2 again to get [0,0,0,0,0].

Input: nums = [0]
Output: 0

The array is already all zeros, so no operation is needed.

Input: nums = [4,4,4]
Output: 1

Choosing x = 4 turns every element into 0 in a single operation.