Minimize Length of Array Using Operations - Problem
Challenge: You're given an array of positive integers and need to minimize its length using a special operation. In each operation, you can:

1. Pick any two distinct elements nums[i] and nums[j]
2. Replace both elements with their modulo: nums[i] % nums[j]
3. This reduces the array size by 1

The goal is to find the minimum possible length after performing this operation optimally. This problem combines number theory with greedy algorithms - you'll need to understand when the modulo operation helps reduce numbers and how to apply it strategically.

Key insight: The modulo operation is essentially the remainder after division, which can help us find patterns similar to the Euclidean algorithm for GCD.

Input & Output

example_1.py โ€” Basic Case
$ Input: [1, 4, 3, 1]
โ€บ Output: 1
๐Ÿ’ก Note: We can perform operations: (1,4) โ†’ 1%4=1, (3,1) โ†’ 3%1=0. Array becomes [1,0], then (1,0) โ†’ 1%0 is undefined, but 0%1=0, so we get [0]. Final length is 1.
example_2.py โ€” Two Groups
$ Input: [5, 5, 5, 10, 5]
โ€บ Output: 2
๐Ÿ’ก Note: All 5s can be reduced together (5%5=0), and 10%5=0. However, we need to be strategic about the operations to achieve minimum length of 2.
example_3.py โ€” Single Element
$ Input: [2]
โ€บ Output: 1
๐Ÿ’ก Note: With only one element, no operations can be performed. The minimum length remains 1.

Visualization

Tap to expand
Array Reduction ProcessInitial Array[6, 4, 8, 2]Operation 16%4=2 โ†’ [2,8,2]Operation 28%2=0 โ†’ [2,0]Operation 32%0โ†’skip, 0%2=0Final ResultLength = 1Key Insights:โ€ข Modulo operation a%b gives remainder when a is divided by bโ€ข Strategic pairing can reduce array length significantlyโ€ข Numbers tend to reduce toward their GCD relationshipsโ€ข Goal: Find minimum possible length through optimal operations
Understanding the Visualization
1
Initial Setup
Start with array [6, 4, 8, 2] - we need to find the minimum possible length
2
Find Optimal Pairs
Look for pairs where modulo operation reduces the numbers effectively
3
Apply Operations
Perform modulo operations: 6%4=2, 8%2=0, continuing the reduction
4
Iterate Until Minimal
Continue until no more beneficial operations possible
Key Takeaway
๐ŸŽฏ Key Insight: The modulo operation behaves like the Euclidean GCD algorithm, creating equivalence classes that determine the final minimum length.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n)

Each operation can lead to multiple new states, creating an exponential search tree

n
2n
โš  Quadratic Growth
Space Complexity
O(2^n)

Memoization table stores exponential number of different array states

n
2n
โš  Quadratic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 20
  • 1 โ‰ค nums[i] โ‰ค 103
  • All elements in nums are positive integers
  • You can perform the operation any number of times (including zero)
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.0K Views
Medium Frequency
~25 min Avg. Time
850 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