Smallest Even Multiple
EasyPrompt
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 <= 150
Approaches
3 approaches with complexity analysis and trade-offs.
This approach simulates a search for the smallest common multiple. We start with the number n and check its multiples one by one (n, 2n, 3n, ...) until we find one that is also a multiple of 2 (i.e., an even number).
Algorithm
-
- Initialize a variable
multipleton.
- Initialize a variable
-
- Start a
whileloop that runs indefinitely.
- Start a
-
- Inside the loop, check if
multipleis even using the modulo operator (multiple % 2 == 0).
- Inside the loop, check if
-
- If it is even, return
multipleas it is the smallest even multiple ofn.
- If it is even, return
-
- If it is odd, update
multipleto the next multiple ofnby addingnto it (multiple += n).
- If it is odd, update
Walkthrough
The algorithm starts by initializing a candidate number, let's call it multiple, to the input n.
It then enters a loop. Inside the loop, it checks if multiple is divisible by 2.
If multiple % 2 == 0, it means we have found the smallest positive integer that is a multiple of both n and 2. The loop terminates, and this value is returned.
If multiple is not divisible by 2, we need to check the next multiple of n. We update multiple by adding n to it (multiple = multiple + n).
The loop continues until an even multiple is found. Since 2 * n is always an even multiple of n, this loop is guaranteed to terminate quickly.
class Solution { public int smallestEvenMultiple(int n) { int multiple = n; while (true) { if (multiple % 2 == 0) { return multiple; } multiple += n; } }}Complexity
Time
O(1). The loop will execute at most twice. If `n` is even, it executes once. If `n` is odd, it executes twice (for `n` and `2n`).
Space
O(1). We only use a single extra variable to store the current multiple.
Trade-offs
Pros
Simple to understand and directly models the process of finding a multiple.
Guaranteed to be correct and terminates quickly for this problem's constraints.
Cons
Involves a loop, which is slightly less direct and efficient than a mathematical or bitwise solution.
For this specific problem, the performance difference is negligible, but as a general approach for finding LCM, it can be slow if the numbers are large.
This approach uses basic number theory. The problem asks for the Least Common Multiple (LCM) of n and 2. We can determine the result by simply checking if n is even or odd.
Algorithm
-
- Check if
nis even by computingn % 2.
- Check if
-
- If the result is 0,
nis even. Returnn.
- If the result is 0,
-
- If the result is not 0,
nis odd. Returnn * 2.
- If the result is not 0,
Walkthrough
The logic is based on two cases:
- Case 1:
nis even. Ifnis already a multiple of 2, thennitself is the smallest positive number that is a multiple of bothnand 2. For example, ifn=6, the smallest even multiple is 6. - Case 2:
nis odd. Ifnis not a multiple of 2, we need to find the smallest multiple ofnthat is even. The multiples ofnaren, 2n, 3n, .... Sincenis odd, the first multiple,n, is odd. The second multiple,2n, is guaranteed to be even. Therefore,2nis the smallest even multiple. For example, ifn=5, the smallest even multiple is2 * 5 = 10. This logic can be implemented with a simpleif-elsestatement or a ternary operator.
class Solution { public int smallestEvenMultiple(int n) { if (n % 2 == 0) { return n; } else { return n * 2; } }} // Using a ternary operator for conciseness:class SolutionTernary { public int smallestEvenMultiple(int n) { return (n % 2 == 0) ? n : n * 2; }}Complexity
Time
O(1). The solution involves a single modulo operation, a comparison, and potentially a multiplication, all of which are constant-time operations.
Space
O(1). No extra space is required beyond the input storage.
Trade-offs
Pros
Very efficient and directly solves the problem based on its mathematical properties.
The code is clean, easy to read, and requires no loops.
Cons
No significant cons for this problem, as it's a direct and optimal solution.
This is the most optimized approach, leveraging bitwise operations to check for even/odd and perform multiplication. It achieves the same result as the conditional check but can be slightly faster at the machine code level and is more concise.
Algorithm
-
- Perform a bitwise AND of
nwith 1 (n & 1). This result is 0 ifnis even and 1 ifnis odd.
- Perform a bitwise AND of
-
- Perform a left bit shift on
nby the result of the previous step.
- Perform a left bit shift on
-
- Return the final value.
Walkthrough
The core idea is the same: if n is even, return n; if n is odd, return 2n. However, we use bitwise operators for the logic.
- Checking for odd/even: A number is odd if its least significant bit (LSB) is 1. We can check this using the bitwise AND operator:
(n & 1). Ifnis even,(n & 1)is 0. Ifnis odd,(n & 1)is 1. - Multiplying by 2: Multiplying an integer by 2 is equivalent to a left bit shift by 1 (
n << 1). We can combine these ideas into a single, elegant expression:n << (n & 1). Let's analyze this expression: - If
nis even,(n & 1)evaluates to 0. The expression becomesn << 0, which isn. - If
nis odd,(n & 1)evaluates to 1. The expression becomesn << 1, which isn * 2. This single line of code correctly handles both cases without any explicit branching (if/elseor ternary).
class Solution { public int smallestEvenMultiple(int n) { // If n is even, (n & 1) is 0, so n << 0 is n. // If n is odd, (n & 1) is 1, so n << 1 is n * 2. return n << (n & 1); }}Complexity
Time
O(1). This involves a couple of bitwise operations, which are extremely fast single-CPU instructions.
Space
O(1). No auxiliary data structures are used.
Trade-offs
Pros
Most concise and potentially the fastest implementation due to the use of efficient bitwise operations.
Avoids conditional branching, which can sometimes lead to better performance on modern CPUs.
Cons
The logic might be slightly less intuitive for developers not familiar with bitwise operations compared to a standard
if-elsecheck.
Solutions
Solution
class Solution {public int smallestEvenMultiple(int n) { return n % 2 == 0 ? n : n * 2; }}class Solution {public: int smallestEvenMultiple(int n) { return n % 2 == 0 ? n : n * 2; }};class Solution: def smallestEvenMultiple( self, n: int) -> int: return n if n % 2 == 0 else n * 2Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.