↓Skip to main content
  1. LeetCode/

LeetCode 2413: Smallest Even Multiple

·1 min· ·
LeetCode Easy Math Number-Theory
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-12-17

  • Submissions: Accepted (Python) → Wrong Answer (C++) → Accepted (C++)

Approach
#

The smallest positive multiple of both 2 and n is lcm(2, n):

  • Even n: n is already a multiple of 2, so the answer is n.
  • Odd n: gcd(2, n) = 1, so the answer is 2n.

My Attempts
#

Python passed first. Practicing in C++, I swapped the ternary branches: (n % 2) ? n : n * 2 returns n when n % 2 is 1 (odd). For n = 5 the answer is 10; it returned 5.

Solution
#

class Solution:
    def smallestEvenMultiple(self, n: int) -> int:
        return n if n % 2 == 0 else 2 * n

Complexity
#

Time O(1), space O(1).

Mistakes to Avoid
#

  • n % 2 is truthy (1) for odd numbers; writing n % 2 == 0 makes the branches harder to swap.

Interview Explanation
#

The answer is the least common multiple of 2 and n: n itself when even, and 2n when odd since 2 and n are then coprime. Time O(1).

Related

LeetCode 258: Add Digits
·1 min
LeetCode Easy Math Simulation Number-Theory
LeetCode 258 note: sum the digits repeatedly until one remains; the O(1) answer is the digital root 1 + (n - 1) % 9
LeetCode 1979: Find Greatest Common Divisor of Array
·1 min
LeetCode Easy Math Number-Theory Gcd Array
LeetCode 1979 note: find the minimum and maximum, then take their GCD with Euclid’s algorithm
LeetCode 2235: Add Two Integers
·1 min
LeetCode Easy Math
LeetCode 2235 note: return the sum; the C++ version once failed to compile from a missing semicolon
LeetCode 2709: Greatest Common Divisor Traversal
·3 mins
LeetCode Hard Math Number-Theory Union-Find Sieve Gcd Array
LeetCode 2709 note: every pair of indices connects ⇔ one component; factor with SPF and union each number with its prime factors in Union-Find, treating 1 specially
LeetCode 1304: Find N Unique Integers Sum up to Zero
·1 min
LeetCode Easy Math Array
LeetCode 1304 note: place 1..n/2 and their negatives so each pair cancels, adding 0 when n is odd
LeetCode 3622: Check Divisibility by Digit Sum and Product
·1 min
LeetCode Easy Math
LeetCode 3622 note: compute the digit sum and product and test n % (sum + product) == 0, keeping the modulo the right way around