GCD of Odd and Even Sums

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

  • sumOdd: the sum of the smallest n positive odd numbers.
  • sumEven: the sum of the smallest n positive even numbers.

Return the GCD of sumOdd and sumEven.

Example 1
Inputn = 4
Output4
The first 4 odd numbers sum to 16 and the first 4 even numbers sum to 20, so GCD(16, 20) = 4.
Example 2
Inputn = 5
Output5
The first 5 odd numbers sum to 25 and the first 5 even numbers sum to 30, so GCD(25, 30) = 5.

Constraints

  • 1 <= n <= 1000

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate