LeetCode Problem Workspace
Smallest Even Multiple
Find the smallest even multiple of a given integer using math and number theory concepts efficiently.
2
Topics
7
Code langs
3
Related
Practice Focus
Easy · Math plus Number Theory
Answer-first summary
Find the smallest even multiple of a given integer using math and number theory concepts efficiently.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Math plus Number Theory
The smallest even multiple of an integer n is either n itself if n is even, or 2*n if n is odd. This leverages number theory by checking parity and ensures correctness with minimal computation. Using this approach guarantees an O(1) solution with constant space while avoiding unnecessary loops or factors.
Problem Statement
Given a positive integer n, return the smallest positive integer that is a multiple of both 2 and n. The solution must handle any n within the given constraints efficiently.
For example, if n = 5, the smallest multiple of 5 and 2 is 10. If n = 6, since 6 is already even, the smallest multiple of 6 and 2 is 6 itself. Constraints: 1 <= n <= 150.
Examples
Example 1
Input: n = 5
Output: 10
The smallest multiple of both 5 and 2 is 10.
Example 2
Input: n = 6
Output: 6
The smallest multiple of both 6 and 2 is 6. Note that a number is a multiple of itself.
Constraints
- 1 <= n <= 150
Solution Approach
Check if n is even
If n is even, it is already a multiple of 2, so return n directly. This uses the property that any even number is divisible by 2, minimizing computation.
Multiply by 2 if n is odd
If n is odd, the smallest even multiple cannot be n itself. Multiply n by 2 to guarantee an even multiple that is also divisible by n, using the minimal number theory approach.
Constant time solution
Combine the parity check and conditional multiplication in a single line. This yields an O(1) time and O(1) space solution without loops, ensuring correctness for all 1 <= n <= 150.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(1) because the solution only requires a single parity check and potentially one multiplication. Space complexity is O(1) as no extra storage is needed beyond the input and output.
What Interviewers Usually Probe
- Asks if n is even or odd to guide towards a simple formula.
- Looks for a constant time solution without loops.
- Wants understanding of minimal number theory application for multiples.
Common Pitfalls or Variants
Common pitfalls
- Forgetting that even numbers return n itself, leading to unnecessary multiplication.
- Overcomplicating by trying to find least common multiple manually.
- Not considering constraints, assuming n could be very large and optimizing incorrectly.
Follow-up variants
- Find smallest multiple divisible by 3 and n instead of 2.
- Compute smallest multiple divisible by a given k and n.
- Generalize to find smallest multiple divisible by multiple given numbers.
FAQ
What is the simplest way to find the smallest even multiple of n?
Check if n is even. If yes, return n. If not, return 2*n.
Does this approach work for all n between 1 and 150?
Yes, the parity check covers all values in the constraint range, ensuring correctness.
Why not compute multiples until finding an even one?
Iterating is unnecessary and slower. The parity check provides a direct O(1) solution.
Is there a formula to generalize this to other divisors?
Yes, for divisor k, return n if n is divisible by k, otherwise return n*k divided by gcd(n,k).
Which problem pattern does this follow?
This is a Math plus Number Theory pattern focused on multiples and parity checks.
Solution
Solution 1: Mathematics
If $n$ is even, then the least common multiple (LCM) of $2$ and $n$ is $n$ itself. Otherwise, the LCM of $2$ and $n$ is $n \times 2$.
class Solution:
def smallestEvenMultiple(self, n: int) -> int:
return n if n % 2 == 0 else n * 2Continue Topic
math
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Math plus Number Theory
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward