Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0f1f79e

Browse files
Create GCD of Odd and Even Sums.java
1 parent 61e396b commit 0f1f79e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

‎Easy/GCD of Odd and Even Sums.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int gcdOfOddEvenSums(int n) {
3+
int sumOdd = 0;
4+
int sumEven = 0;
5+
for (int i = 1; i <= 2 * n; i++) {
6+
sumOdd += i % 2 != 0 ? i : 0;
7+
sumEven += i % 2 == 0 ? i : 0;
8+
}
9+
return gcd(sumOdd, sumEven);
10+
}
11+
12+
private static int gcd(int a, int b) {
13+
if (b == 0) {
14+
return a;
15+
}
16+
return gcd(b, a % b);
17+
}
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /