Minimize Length of Array Using Operations

You are given a 0-indexed integer array nums containing positive integers.

Your task is to minimize the length of nums by performing the following operations any number of times, including zero:

  • Select two distinct indices i and j from nums, such that nums[i] > 0 and nums[j] > 0.
  • Insert the result of nums[i] % nums[j] at the end of nums.
  • Delete the elements at indices i and j from nums.

Return an integer denoting the minimum length of nums after performing the operation any number of times.

Example 1
Inputnums = [1,4,3,1]
Output1
The array can be reduced through the described operations to [0], and it can be shown that length 1 is the minimum achievable length.
Example 2
Inputnums = [5,5,5,10,5]
Output2
The array can be reduced through the described operations to [0,0], and it can be shown that length 2 is the minimum achievable length.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate