#2413

Smallest Even Multiple

Easy
MathNumber TheoryMathematical PropertiesConditionals
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(1)
Space
O(1)
O(1)
💡

Intuition

Time O(1)Space O(1)

The optimal solution leverages the fact that the smallest even multiple of n can be derived directly. If n is even, the answer is n; if n is odd, the answer is n multiplied by 2. This avoids unnecessary iterations.

⚙️

Algorithm

3 steps
  1. 1Step 1: Check if n is even.
  2. 2Step 2: If n is even, return n.
  3. 3Step 3: If n is odd, return n multiplied by 2.
solution.py4 lines
1# Full working Python code
2
3def smallest_even_multiple(n):
4    return n if n % 2 == 0 else n * 2

Complexity note: This is constant time complexity since we only perform a couple of arithmetic operations and a modulus check, regardless of the size of n.

  • 1The smallest even multiple of a number can be derived directly based on whether the number is even or odd.
  • 2Understanding the properties of numbers (like even and odd) can lead to significant optimizations.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.