Smallest Even Multiple - Problem

Given a positive integer n, you need to find the smallest positive integer that is a multiple of both 2 and n.

In other words, find the Least Common Multiple (LCM) of 2 and n.

Example: If n = 5, the multiples of 5 are [5, 10, 15, 20, ...] and the multiples of 2 are [2, 4, 6, 8, 10, 12, ...]. The smallest number that appears in both sequences is 10.

This is a fundamental number theory problem that helps you understand the relationship between LCM and GCD.

Input & Output

example_1.py โ€” Basic Even Number
$ Input: n = 6
โ€บ Output: 6
๐Ÿ’ก Note: Since 6 is already even, it's divisible by both 2 and 6. The LCM of 2 and 6 is 6.
example_2.py โ€” Basic Odd Number
$ Input: n = 5
โ€บ Output: 10
๐Ÿ’ก Note: 5 is odd, so we need the first multiple of 5 that's also even. That would be 5ร—2 = 10.
example_3.py โ€” Edge Case Minimum
$ Input: n = 1
โ€บ Output: 2
๐Ÿ’ก Note: 1 is odd, so the smallest multiple of both 1 and 2 is 1ร—2 = 2.

Constraints

  • 1 โ‰ค n โ‰ค 150
  • n is a positive integer
  • The result will always fit in a 32-bit integer

Visualization

Tap to expand
Even Numbers (n % 2 == 0)Examples: 2, 4, 6, 8, 10...LCM(2, n) = nAlready divisible by 2!Odd Numbers (n % 2 == 1)Examples: 1, 3, 5, 7, 9...LCM(2, n) = 2 ร— nNeed to multiply by 2!Mathematical FormulaLCM(a,b) = (a ร— b) / GCD(a,b)For n even: GCD(2,n) = 2, so LCM = (2ร—n)/2 = nFor n odd: GCD(2,n) = 1, so LCM = (2ร—n)/1 = 2n
Understanding the Visualization
1
Even Numbers
If n is even, it already contains factor 2, so LCM(2,n) = n
2
Odd Numbers
If n is odd, gcd(2,n) = 1, so LCM(2,n) = 2ร—n
3
Pattern
This creates a simple conditional: return n if even, 2n if odd
Key Takeaway
๐ŸŽฏ Key Insight: The parity (even/odd nature) of n directly determines the LCM with 2, making this an O(1) solution.
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
73.5K Views
Medium Frequency
~5 min Avg. Time
2.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen