Ctrl K

Shrink a List With Remainders

Medium
You are given a list of positive integers values. In one move you may: 1. pick two elements at different positions, both greater than zero, call them x and y (order matters); 2. remove both from the list; 3. append the remainder x mod y to the end. Each move shortens the list by one. You may make any number of moves, including none. Return the smallest length the list can be shrunk to.
Example 1
values=[6, 4, 6] → 1

Take 6 and 4: 6 mod 4 = 2, giving [6, 2]. Then 2 and 6: 2 mod 6 = 2, giving [2]. One element is left.

Example 2
values=[3, 3, 3, 9] → 2

Every value is a multiple of 3, and nothing positive smaller than 3 can appear. The pair (3, 9) gives 3, then two pairs of 3s give two zeros: [0, 0]. That is the best possible.

Example 3
values=[11] → 1
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsChunks With a Common Divisor MediumShave Primes Into Order MediumEqual Window Sums on a Ring MediumDivide Down to Sorted Order MediumPrimes Below a Bound Medium
loading Python…
Input
values=[6, 4, 6]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 137 tests on the server, hidden ones included.