LeetCode-in-Java

3658. GCD of Odd and Even Sums

Easy

You are given an integer n. Your task is to compute the GCD (greatest common divisor) of two values:

Return the GCD of sumOdd and sumEven.

Example 1:

Input: n = 4

Output: 4

Explanation:

Hence, GCD(sumOdd, sumEven) = GCD(16, 20) = 4.

Example 2:

Input: n = 5

Output: 5

Explanation:

Hence, GCD(sumOdd, sumEven) = GCD(25, 30) = 5.

Constraints:

Solution

public class Solution {
    public int gcdOfOddEvenSums(int n) {
        return (n < 0) ? -n : n;
    }
}