Back to problems

Repeated Leading-Nonzero Subtraction

Algorithm · Capital One · Medium

Requirements Input: nums, an array containing non-negative integers. Pseudocode: When every value is 0, return the answer accumulated so far. Locate the first position k such that nums[k] > 0, and store nums[k] in x. Examine each position j after k: whenever nums[j] >= x, reduce it by x; if an entry is smaller than x, end this inner traversal immediately without changing that entry. Increase the answer by x. Replace nums[k] with 0, then continue the process. Note: the first…

Checking your access…