Watch 10 video solutions for Smallest Even Multiple, a easy level problem involving Math, Number Theory. This walkthrough by Coding Decoded has 1,184 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
n, return the smallest positive integer that is a multiple of both 2 and n.
Example 1:
Input: n = 5 Output: 10 Explanation: The smallest multiple of both 5 and 2 is 10.
Example 2:
Input: n = 6 Output: 6 Explanation: The smallest multiple of both 6 and 2 is 6. Note that a number is a multiple of itself.
Constraints:
1 <= n <= 150Problem Overview: Given a positive integer n, return the smallest positive number that is both a multiple of n and an even number. In other words, compute the smallest even number divisible by n. This is essentially finding LCM(n, 2).
The key observation is simple: if n is already even, then n itself satisfies the condition. If n is odd, multiplying it by 2 produces the smallest even multiple.
Approach 1: Multiply and Check (O(1) time, O(1) space)
This approach relies on a basic property from math and number theory. The smallest even multiple of n is equivalent to the least common multiple of n and 2. Instead of explicitly computing LCM, you check whether n is even using the modulo operation. If n % 2 == 0, return n. Otherwise return 2 * n.
This works because any even number already contains the factor 2, meaning it is automatically divisible by both n and 2. If n is odd, the smallest number that includes both factors is simply 2n. The algorithm performs a single arithmetic check and multiplication, giving constant time complexity O(1) and constant space O(1). This is the most straightforward and readable implementation.
Approach 2: Bit Manipulation (O(1) time, O(1) space)
You can also determine whether n is even using bit manipulation. In binary representation, even numbers always end with 0 while odd numbers end with 1. Using the expression n & 1 extracts the least significant bit.
If (n & 1) == 0, the number is even and you return n. Otherwise return n * 2. This avoids the modulo operator and relies purely on bitwise operations. The runtime remains O(1) because only a single bit check and multiplication occur, and memory usage is also O(1).
This approach is common in performance-sensitive code and demonstrates familiarity with binary representations of integers. While the performance difference is negligible for this problem, bitwise checks are often faster at the machine level.
Recommended for interviews: The modulo-based solution is typically what interviewers expect first because it directly reflects the mathematical insight behind the problem. Mentioning that the result is LCM(n, 2) shows strong understanding of number theory. The bit manipulation version is a good follow-up optimization that demonstrates deeper familiarity with low-level integer operations.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Multiply and Check (Modulo) | O(1) | O(1) | Best general solution; clear mathematical reasoning using LCM(n,2) |
| Bit Manipulation | O(1) | O(1) | When demonstrating knowledge of binary operations or avoiding modulo |