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 <= 150The key idea in #2413 Smallest Even Multiple is to recognize the relationship between a number and the requirement that the result must be even. The smallest even multiple of a number can be viewed as the Least Common Multiple (LCM) between the number n and 2. Instead of computing LCM using the general formula, we can simplify the reasoning using basic number theory.
If a number is already even, it already contains the factor 2, meaning its smallest even multiple is simply the number itself. However, if the number is odd, it lacks the factor 2, so multiplying it by 2 introduces the smallest even multiple.
This observation leads to a very efficient constant-time approach based on checking the parity of the number (whether it is even or odd). The solution avoids loops or complex calculations, making it extremely efficient for interview scenarios.
Time Complexity: O(1) since only a constant-time check is required.
Space Complexity: O(1) because no extra data structures are used.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Math / Parity Check using LCM concept | O(1) | O(1) |
Coding Decoded
Use these hints if you're stuck. Try solving on your own first.
A guaranteed way to find a multiple of 2 and n is to multiply them together. When is this the answer, and when is there a smaller answer?
There is a smaller answer when n is even.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
The problem essentially asks for the smallest number that is divisible by both n and 2. This matches the definition of the Least Common Multiple (LCM), which helps simplify the reasoning and leads to a constant-time solution.
Problems like Smallest Even Multiple are common as warm-up or screening questions. While the exact problem may not always appear, the concept of using number theory and parity checks is frequently tested in coding interviews.
No special data structure is required for this problem. It relies purely on a mathematical insight and a simple parity check, making it solvable with basic arithmetic operations.
The optimal approach uses a simple math observation based on the least common multiple of n and 2. By checking whether the number is even or odd, we can determine the smallest even multiple in constant time without loops or extra computation.