Find Greatest Common Divisor of Array LeetCode solution in C++

1
class Solution {
2
public:
3
int findGCD(vector<int> &nums) {
4
int mn = nums[0], mx = nums[0];
5
for (auto n : nums) {
6
// finding maximum, minimum values of the array.
7
if (n > mx) mx = n;
8
if (n < mn) mn = n;
9
}
10

11
for (int i = mn; i >= 1; i--) {
12
// finding greatest common divisor (GCD) of max, min.
13
if ((mn % i == 0) && (mx % i == 0)) return i;
14
}
15
return 1;
16
}
17
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0