Smallest Even Multiple - Problem

Given a positive integer n, return the smallest positive integer that is a multiple of both 2 and n.

In other words, find the smallest number that is divisible by both 2 and n.

Input & Output

Example 1 — Odd Number
$ Input: n = 5
Output: 10
💡 Note: Since 5 is odd, the smallest multiple of both 2 and 5 is 2×5 = 10. We can verify: 10÷2 = 5 and 10÷5 = 2.
Example 2 — Even Number
$ Input: n = 6
Output: 6
💡 Note: Since 6 is even, it's already divisible by 2. The smallest multiple of both 2 and 6 is 6 itself. We can verify: 6÷2 = 3 and 6÷6 = 1.
Example 3 — Small Odd
$ Input: n = 1
Output: 2
💡 Note: Since 1 is odd, the smallest multiple of both 2 and 1 is 2×1 = 2. We can verify: 2÷2 = 1 and 2÷1 = 2.

Constraints

  • 1 ≤ n ≤ 150

Visualization

Tap to expand
Smallest Even Multiple INPUT Positive Integer n = 5 Find smallest number divisible by both: 2 n n is odd (5 % 2 != 0) Input: n = 5 ALGORITHM STEPS 1 Check if n is even Is n % 2 == 0? 2 If n is even Return n (already even) 3 If n is odd Return n * 2 4 For n = 5 5 * 2 = 10 n % 2 == 0 ? NO n*2 YES n FINAL RESULT Smallest Even Multiple 10 Verification: 10 % 2 = 0 [OK] 10 % 5 = 0 [OK] 10 is divisible by both 2 and 5 Output: 10 Key Insight: The smallest even multiple of n is the LCM(2, n). If n is already even, then LCM(2, n) = n. If n is odd, then LCM(2, n) = 2*n. Time Complexity: O(1), Space Complexity: O(1). TutorialsPoint - Smallest Even Multiple | Optimal Solution
Asked in
Amazon 15 Apple 8
31.5K Views
Low Frequency
~5 min Avg. Time
850 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