Triple Array Toolkit: Lookup, Increment, Zero Shift
Arrays & HashingAuto-graded · 14 test casesLast updated
Combines a binary search on a sorted list, adding one to a digit-array number with carry, and a stable in-place move of zeros to the back.
Write one function that performs three small, unrelated array tasks and returns all three results together.
Inputs
sorted_values: a list of integers sorted in nondecreasing order.target: an integer to look for insorted_values.digits: a list of decimal digits (each 0 to 9) that spells a nonnegative integer, most significant digit first.values: a list of integers.
Task A: Locate the target
Search sorted_values for target and report its index, or -1 if it is absent. Your search should take O(log n) time.
To keep the output deterministic when duplicates exist, use the classic closed-window binary search. Start with lo = 0 and hi = len(sorted_values) - 1. While lo <= hi, set mid = (lo + hi) // 2.
- If
sorted_values[mid] == target, returnmidimmediately. - If
sorted_values[mid] < target, setlo = mid + 1. - Otherwise, set
hi = mid - 1.
If the window becomes empty, the answer is -1.
Task B: Increment the number
Treat digits as a large decimal number and add 1 to it. Return the new digit list, most significant digit first, with no leading zeros added except where the number itself requires them.
- Do not convert the whole list into an integer. Handle carries digit by digit.
- Do not change the caller's
digitslist. Work on a copy. - If every digit is
9, the result has one more digit than the input (for example[9, 9]becomes[1, 0, 0]). - If
digitsis empty, treat it as the number zero, so the result is[1].
Task C: Push zeros to the back
Rearrange values in place so that all nonzero elements come first, in their original relative order, followed by all the zeros. Use O(1) extra space. The list you return for this task is the same, modified values list.
Output
Return a list of three items, in this order:
- The index from Task A (an
int). - The digit list from Task B.
- The rearranged
valueslist from Task C.
Constraints
0 <= len(sorted_values), len(digits), len(values) <= 10^5- Elements of
sorted_valuesandvalues, andtarget, fit in a 32-bit signed integer. - Every element of
digitsis an integer from 0 to 9. sorted_valuesis sorted in nondecreasing order.
Examples
Example 1
Input: run_array_tasks([1, 3, 5, 7, 9], 7, [1, 2, 3], [0, 1, 0, 3, 12])
Output: [3, [1, 2, 4], [1, 3, 12, 0, 0]]
Example 2
Input: run_array_tasks([1, 3, 5, 7, 9], 4, [9, 9, 9], [0, 0, 0])
Output: [-1, [1, 0, 0, 0], [0, 0, 0]]
Hints
Hint 1
For the search, keep a window [lo, hi] that must contain the target if it exists, and shrink it by comparing against the middle element.
Hint 2
For the increment, scan from the last digit leftward. A 9 turns into 0 and passes the carry on. The first digit below 9 gets +1 and you can stop.
Hint 3
For the zeros, keep a write pointer for the next slot a nonzero element belongs in. Scan once, placing each nonzero at the write pointer, then fill what is left with zeros.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The three tasks are independent, so we solve each in turn and bundle the results.
Task A: Binary search
Maintain a closed window [lo, hi] that contains target whenever target is present. Look at mid = (lo + hi) // 2:
- An equal value means we return
mid. - A smaller value means everything at or left of
midis too small, solo = mid + 1. - A larger value means everything at or right of
midis too large, sohi = mid - 1.
When the window empties, the target is absent and the answer is -1.
Why it is correct: the array is sorted, so each step discards only indices that cannot hold the target. The window shrinks every iteration, so the loop terminates. Using the fixed midpoint rule and returning on the first hit makes the answer deterministic even with duplicates.
Task B: Add one
Adding 1 affects only the trailing run of 9s and the first non-9 digit before it. Walk from the right: every 9 becomes 0 (the carry moves on). When we reach a digit below 9, we add 1 and stop. If we run off the left end, every digit was 9 (or the list was empty), so we put a new leading 1 in front. We work on a copy so the input is untouched.
Why it is correct: this mirrors grade-school addition. A number of all 9s, 10^n - 1, becomes 10^n, which is a 1 followed by n zeros. An empty list is treated as zero, giving [1].
Task C: Stable zero shift
Use a write pointer. Scan the list once and copy each nonzero value to the write position, advancing the pointer. Then fill the remaining positions with zeros.
Why it is correct: nonzeros are written in the order they are met, so their relative order is preserved. The number of zeros written at the end equals the number of positions left over, which equals the number of zeros in the original list.
Complexity
- Task A:
O(log n)time,O(1)space. - Task B:
O(m)time in the worst case, withO(m)space for the copy and result. - Task C:
O(k)time,O(1)extra space.
Here n, m and k are the lengths of sorted_values, digits and values.
Reference solution
from typing import List
def run_array_tasks(sorted_values: List[int], target: int, digits: List[int], values: List[int]) -> List[object]:
# Task A: closed-window binary search
lo, hi = 0, len(sorted_values) - 1
found = -1
while lo <= hi:
mid = (lo + hi) // 2
if sorted_values[mid] == target:
found = mid
break
if sorted_values[mid] < target:
lo = mid + 1
else:
hi = mid - 1
# Task B: add one with carry propagation on a copy
result_digits = digits[:]
i = len(result_digits) - 1
while i >= 0 and result_digits[i] == 9:
result_digits[i] = 0
i -= 1
if i >= 0:
result_digits[i] += 1
else:
result_digits.insert(0, 1)
# Task C: stable in-place zero shift
write = 0
for value in values:
if value != 0:
values[write] = value
write += 1
while write < len(values):
values[write] = 0
write += 1
return [found, result_digits, values]