Skip to content

Commit d372e5e

Browse files
solves find greatest common divisor of array
1 parent 2c72231 commit d372e5e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@
474474
| 1967 | [Number of Strings That Appear as Substrings in Word](https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word) | [![Java](assets/java.png)](src/NumberOfStringsThatAppearAsSubstringsInWord.java) | |
475475
| 1971 | [Find if Path Exists in Graph](https://leetcode.com/problems/find-if-path-exists-in-graph) | [![Java](assets/java.png)](src/FindIfPathExistsInGraph.java) | |
476476
| 1974 | [Minimum Time to Type Word Using Special Typewriter](https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter) | [![Java](assets/java.png)](src/MinimumTimeToTypeWordUsingSpecialTypewriter.java) | |
477-
| 1979 | [Find Greatest Common Divisor of Array](https://leetcode.com/problems/find-greatest-common-divisor-of-array) | | |
477+
| 1979 | [Find Greatest Common Divisor of Array](https://leetcode.com/problems/find-greatest-common-divisor-of-array) | [![Java](assets/java.png)](src/FindGreatestCommonDivisorOfArray.java) | |
478478
| 1984 | [Minimum Difference Between Highest and Lowest of K Scores](https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores) | | |
479479
| 1991 | [Find the Middle Index in Array](https://leetcode.com/problems/find-the-middle-index-in-array) | | |
480480
| 1995 | [Count Special Quadruplets](https://leetcode.com/problems/count-special-quadruplets) | | |

Diff for: src/FindGreatestCommonDivisorOfArray.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://leetcode.com/problems/find-greatest-common-divisor-of-array
2+
// T: O(N)
3+
// S: O(1)
4+
5+
import java.util.Arrays;
6+
7+
public class FindGreatestCommonDivisorOfArray {
8+
public int findGCD(int[] nums) {
9+
return gcd(
10+
Arrays.stream(nums).max().getAsInt(),
11+
Arrays.stream(nums).min().getAsInt()
12+
);
13+
}
14+
15+
private int gcd(int a, int b) {
16+
return b == 0 ? a : gcd(b, a % b);
17+
}
18+
}

0 commit comments

Comments
 (0)