Skip to content

Commit 27e2641

Browse files
committed
1979. Find Greatest Common Divisor of Array: AC
1 parent 5f8349c commit 27e2641

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1491,3 +1491,4 @@ mod s1974_minimum_time_to_type_word_using_special_typewriter;
14911491
mod s1975_maximum_matrix_sum;
14921492
mod s1976_number_of_ways_to_arrive_at_destination;
14931493
mod s1977_number_of_ways_to_separate_numbers;
1494+
mod s1979_find_greatest_common_divisor_of_array;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* [1979] Find Greatest Common Divisor of Array
3+
*
4+
* Given an integer array nums, return the greatest common divisor of the smallest number and largest number in nums.
5+
* The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.
6+
*
7+
* Example 1:
8+
*
9+
* Input: nums = [2,5,6,9,10]
10+
* Output: 2
11+
* Explanation:
12+
* The smallest number in nums is 2.
13+
* The largest number in nums is 10.
14+
* The greatest common divisor of 2 and 10 is 2.
15+
*
16+
* Example 2:
17+
*
18+
* Input: nums = [7,5,6,8,3]
19+
* Output: 1
20+
* Explanation:
21+
* The smallest number in nums is 3.
22+
* The largest number in nums is 8.
23+
* The greatest common divisor of 3 and 8 is 1.
24+
*
25+
* Example 3:
26+
*
27+
* Input: nums = [3,3]
28+
* Output: 3
29+
* Explanation:
30+
* The smallest number in nums is 3.
31+
* The largest number in nums is 3.
32+
* The greatest common divisor of 3 and 3 is 3.
33+
*
34+
*
35+
* Constraints:
36+
*
37+
* 2 <= nums.length <= 1000
38+
* 1 <= nums[i] <= 1000
39+
*
40+
*/
41+
pub struct Solution {}
42+
43+
// problem: https://leetcode.com/problems/find-greatest-common-divisor-of-array/
44+
// discuss: https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/?currentPage=1&orderBy=most_votes&query=
45+
46+
// submission codes start here
47+
48+
impl Solution {
49+
pub fn find_gcd(nums: Vec<i32>) -> i32 {
50+
let (mn, ma) = nums.iter().fold((1000, 1), |(mn, ma), &y| {
51+
if (y > ma) {
52+
if (y < mn) {
53+
(y, y)
54+
} else {
55+
(mn, y)
56+
}
57+
} else if (y < mn) {
58+
(y, ma)
59+
} else {
60+
(mn, ma)
61+
}
62+
});
63+
64+
(1..=mn).fold(1, |x, y| if (mn % y == 0) && (ma % y) == 0 { y } else { x })
65+
}
66+
}
67+
68+
// submission codes end
69+
70+
#[cfg(test)]
71+
mod tests {
72+
use super::*;
73+
74+
#[test]
75+
fn test_1979_example_1() {
76+
let nums = vec![2, 5, 6, 9, 10];
77+
78+
let result = 2;
79+
80+
assert_eq!(Solution::find_gcd(nums), result);
81+
}
82+
83+
#[test]
84+
fn test_1979_example_2() {
85+
let nums = vec![7, 5, 6, 8, 3];
86+
87+
let result = 1;
88+
89+
assert_eq!(Solution::find_gcd(nums), result);
90+
}
91+
92+
#[test]
93+
fn test_1979_example_3() {
94+
let nums = vec![3, 3];
95+
96+
let result = 3;
97+
98+
assert_eq!(Solution::find_gcd(nums), result);
99+
}
100+
}

0 commit comments

Comments
 (0)