GCD of Odd and Even Sums

You are given an integer `n`. Let `sumOdd` be the sum of the first `n` odd numbers (1 + 3 + 5 + ...) and `sumEven` be the sum of the first `n` even numbers (2 + 4 + 6 + ...). Return the greatest common divisor of `sumOdd` and `sumEven`.

30:00

GCD of Odd and Even Sums
easy
Free written solution
Topics
Companies

You are given an integer n. Let sumOdd be the sum of the first n odd numbers (1 + 3 + 5 + ...) and sumEven be the sum of the first n even numbers (2 + 4 + 6 + ...). Return the greatest common divisor of sumOdd and sumEven.

Example 1:
Input: {"n":4}
Output: 4
Constraints:
  • 1≤n≤10001 \leq n \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":4}

n = 4. Lay the first 4 odd numbers down as L-shaped layers of a square, and the first 4 even numbers as layers of a rectangle.

each layer is one number; stripes alternate so the layers stay visible

odds 1..7: 4 x 4

sum = 0

evens 2..8: 4 x 5

sum = 0

Euclid: a = q x b + r

waiting for both sums

Variables
VariableValue
n4
DepthFunction Call
Stack empty
0/14