Skip to content

Commit 7f02f05

Browse files
committed
1998. GCD Sort of an Array: RETRY
1 parent 156dfd6 commit 7f02f05

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,3 +1506,4 @@ mod s1994_the_number_of_good_subsets;
15061506
mod s1995_count_special_quadruplets;
15071507
mod s1996_the_number_of_weak_characters_in_the_game;
15081508
mod s1997_first_day_where_you_have_been_in_all_the_rooms;
1509+
mod s1998_gcd_sort_of_an_array;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* [1998] GCD Sort of an Array
3+
*
4+
* You are given an integer array nums, and you can perform the following operation any number of times on nums:
5+
*
6+
* Swap the positions of two elements nums[i] and nums[j] if gcd(nums[i], nums[j]) > 1 where gcd(nums[i], nums[j]) is the greatest common divisor of nums[i] and nums[j].
7+
*
8+
* Return true if it is possible to sort nums in non-decreasing order using the above swap method, or false otherwise.
9+
*
10+
* Example 1:
11+
*
12+
* Input: nums = [7,21,3]
13+
* Output: true
14+
* Explanation: We can sort [7,21,3] by performing the following operations:
15+
* - Swap 7 and 21 because gcd(7,21) = 7. nums = [<u>21</u>,<u>7</u>,3]
16+
* - Swap 21 and 3 because gcd(21,3) = 3. nums = [<u>3</u>,7,<u>21</u>]
17+
*
18+
* Example 2:
19+
*
20+
* Input: nums = [5,2,6,2]
21+
* Output: false
22+
* Explanation: It is impossible to sort the array because 5 cannot be swapped with any other element.
23+
*
24+
* Example 3:
25+
*
26+
* Input: nums = [10,5,9,3,15]
27+
* Output: true
28+
* We can sort [10,5,9,3,15] by performing the following operations:
29+
* - Swap 10 and 15 because gcd(10,15) = 5. nums = [<u>15</u>,5,9,3,<u>10</u>]
30+
* - Swap 15 and 3 because gcd(15,3) = 3. nums = [<u>3</u>,5,9,<u>15</u>,10]
31+
* - Swap 10 and 15 because gcd(10,15) = 5. nums = [3,5,9,<u>10</u>,<u>15</u>]
32+
*
33+
*
34+
* Constraints:
35+
*
36+
* 1 <= nums.length <= 3 * 10^4
37+
* 2 <= nums[i] <= 10^5
38+
*
39+
*/
40+
pub struct Solution {}
41+
42+
// problem: https://leetcode.com/problems/gcd-sort-of-an-array/
43+
// discuss: https://leetcode.com/problems/gcd-sort-of-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
44+
45+
// submission codes start here
46+
47+
impl Solution {
48+
pub fn gcd_sort(nums: Vec<i32>) -> bool {
49+
false
50+
}
51+
}
52+
53+
// submission codes end
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use super::*;
58+
59+
#[test]
60+
#[ignore]
61+
fn test_1998_example_1() {
62+
let nums = vec![7, 21, 3];
63+
64+
let result = true;
65+
66+
assert_eq!(Solution::gcd_sort(nums), result);
67+
}
68+
69+
#[test]
70+
#[ignore]
71+
fn test_1998_example_2() {
72+
let nums = vec![5, 2, 6, 2];
73+
74+
let result = false;
75+
76+
assert_eq!(Solution::gcd_sort(nums), result);
77+
}
78+
79+
#[test]
80+
#[ignore]
81+
fn test_1998_example_3() {
82+
let nums = vec![10, 5, 9, 3, 15];
83+
84+
let result = true;
85+
86+
assert_eq!(Solution::gcd_sort(nums), result);
87+
}
88+
}

0 commit comments

Comments
 (0)