Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Number Theory
GCD of Odd and Even Sums

Given an array, compute the greatest common divisor of the sum of elements at odd indices and the sum of elements at even indices.

Acceptance 0%
Problem Statement

Problem

You are given an integer array nums. Let oddSum be the sum of elements at odd indices and evenSum be the sum of elements at even indices, using 0-based indexing.

Return the value of gcd(oddSum, evenSum).

Notes

  • Indices 1, 3, 5, ... are odd indices.
  • Indices 0, 2, 4, ... are even indices.
  • The greatest common divisor of two numbers is always non-negative.

Input Format

  • A single integer array nums.

Output Format

  • Return a single integer: gcd(oddSum, evenSum).

Constraints

  • 1 <= nums.length
  • Array values are integers.
  • The exact bounds are not provided here; assume standard interview-style constraints.

Hints

  • Compute the two sums separately.
  • Then apply the Euclidean algorithm for the GCD.
  • Be careful about which indices are considered odd and even when using 0-based indexing.

Input Format

  • An integer array nums.
  • Indices are treated as 0-based.

Output Format

  • A single integer equal to gcd(sum(nums[i] for odd i), sum(nums[i] for even i)).

Constraints

  • nums is non-empty.
  • Use 0-based indexing.
  • Return the non-negative greatest common divisor.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [2, 5, 6, 9, 10]

Output

6

Explanation

Even indices: 2 + 6 + 10 = 18. Odd indices: 5 + 9 = 14. gcd(18, 14) = 2.

If interpreted as 1-based positions, the result would differ; this problem uses 0-based indexing. For the given array, the correct answer is gcd(18, 14) = 2.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.