Skip to content

Practice GCD Sort of an Array

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1998
LeetCode Problem

GCD Sort of an Array

Problem Statement

You may swap two current entries of nums only when their greatest common divisor is greater than 1. Repeat any number of times. Return whether these swaps can put the array in nondecreasing order.

Source: GCD Sort of an Array on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [7,21,3]
Output
true

# Example 2

Input
nums = [5,2,6,2]
Output
false

# Example 3

Input
nums = [10,5,9,3,15]
Output
true We can sort [10,5,9,3,15] by performing the following operations: - Swap 10 and 15 because gcd(10,15) = 5. nums = [15,5,9,3,10] - Swap 15 and 3 because gcd(15,3) = 3. nums = [3,5,9,15,10] - Swap 10 and 15 because gcd(10,15) = 5. nums = [3,5,9,10,15]

Constraints

1 <= nums.length <= 3 * 10^4
2 <= nums[i] <= 10^5

Related LeetCode Problems

Practice more problems similar to GCD Sort of an Array.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.