Pankaj Tanwar
Published on

Find Greatest Common Divisor of Array.

––– views

It's day 10.

Problem of the day - Find Greatest Common Divisor of Array

Tag - Easy

Problem is to find the greatest common divisor of maximum and minimum element of given array.

Input: nums = [2,5,6,9,10] Output: 2 Explanation: The smallest number in nums is 2. The largest number in nums is 10. The greatest common divisor of 2 and 10 is 2.

I didn't want to put much efforts in this.

Here is my code -

class Solution {
public:
int findGCD(vector<int>& nums) {
int sm = nums[0];
int mx = nums[0];
for(auto x: nums) {
sm = min(sm, x);
mx = max(mx, x);
}
return __gcd(sm,mx);
}
};

I know, this code can be improved. I am not in the right state of modify this, may be tomorrow!


You might like previous editions of my coding diary