Smallest Even Multiple solution and explanation
9 more videos on this problem
Leetcode 2413 Smallest Even Multiple | Coding Decoded SDE sheetCoding Decoded · 1:41 · 1K views
Smallest Even Multiple - LeetCode 2413 Solution in C++ (LeetCode Math)nexTRIE · 1:30 · 840 views
2413. Smallest Even Multiple (Leetcode Easy)Programming Live with Larry · 1:55 · 823 views
Smallest Even Multiple | LeetCode 2413 | CppCodeClips with Abhishek Ranjan · 1:24 · 612 views
2413. Smallest Even Multiple with Python | Leetcode with Python #youtube #leetcode #technologyArsalan Ahmed · 6:55 · 375 views
Leetcode Problem 2413. Smallest Even Multiple. Leetcode Solution 2413 EASY.CodingForBeginners · 2:11 · 372 views
Leetcode 2413. Smallest Even MultipleElite Code · 5:54 · 255 views
LeetCode 2413 Smallest Even Multiple in Python | Easy Coding Tutorial for BeginnersJR: Educational Channel · 1:20 · 83 views
Leetcode | Easy | #2413 Smallest Even Multiple | Coding Practice | Javaa_normal_coder · 3:14 · 71 views
LeetCode 2413 Smallest Even Multiple | C++ Solution | Loop vs Conditional 💡 Beginner Friendly TipsDhruvi_Patel_32 · 5:01 · 53 views
The problem
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
Approach overview
Problem 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 1: Multiply and Check
This approach involves calculating the smallest number that results from multiplying n such that it is also even. Since the problem requires a number that's a multiple of both n and 2, you either take n itself if it's even, or multiply n by 2 if it's odd.
def smallest_even_multiple(n):
return n if n % 2 == 0 else n * 2
n = 6
print(smallest_even_multiple(n))public class SmallestEvenMultiple {
public static int smallestEvenMultiple(int n) {
return (n % 2 == 0) ? n : n * 2;
}
public static void main(String[] args) {
int n = 5;
System.out.println(smallestEvenMultiple(n));
}
}#include <iostream>
int smallestEvenMultiple(int n) {
return n % 2 == 0 ? n : n * 2;
}
int main() {
int n = 6;
std::cout << smallestEvenMultiple(n) << std::endl;
return 0;
}function smallestEvenMultiple(n) {
return n % 2 === 0 ? n : n * 2;
}
let n = 6;
console.log(smallestEvenMultiple(n));#include <stdio.h>
int smallestEvenMultiple(int n) {
return n % 2 == 0 ? n : n * 2;
}
int main() {
int n = 5;
printf("%d\n", smallestEvenMultiple(n));
return 0;
}using System;
class Program {
static int SmallestEvenMultiple(int n) {
return (n % 2 == 0) ? n : n * 2;
}
static void Main() {
int n = 5;
Console.WriteLine(SmallestEvenMultiple(n));
}
}The Python solution leverages Python's succinct syntax to check if n is even and return it, or double it if odd, using a conditional expression.
Time Complexity: O(1)
Space Complexity: O(1)
Approach 2: Bit Manipulation
Leverage the properties of bits to determine evenness. If n is even, return n; otherwise, return n << 1 which is equivalent to multiplying n by 2.
def smallest_even_multiple(n):
return n if (n & 1) == 0 else n << 1
n = 6
print(smallest_even_multiple(n))public class SmallestEvenMultiple {
public static int smallestEvenMultiple(int n) {
return (n & 1) == 0 ? n : n << 1;
}
public static void main(String[] args) {
int n = 5;
System.out.println(smallestEvenMultiple(n));
}
}#include <iostream>
int smallestEvenMultiple(int n) {
return (n & 1) == 0 ? n : n << 1;
}
int main() {
int n = 6;
std::cout << smallestEvenMultiple(n) << std::endl;
return 0;
}function smallestEvenMultiple(n) {
return (n & 1) === 0 ? n : n << 1;
}
let n = 6;
console.log(smallestEvenMultiple(n));#include <stdio.h>
int smallestEvenMultiple(int n) {
return (n & 1) == 0 ? n : n << 1;
}
int main() {
int n = 5;
printf("%d\n", smallestEvenMultiple(n));
return 0;
}using System;
class Program {
static int SmallestEvenMultiple(int n) {
return (n & 1) == 0 ? n : n << 1;
}
static void Main() {
int n = 5;
Console.WriteLine(SmallestEvenMultiple(n));
}
}Python allows using bitwise operations similarly. This simplifies checking of the least significant bit to determine evenness and adjust n accordingly.
Time Complexity: O(1)
Space Complexity: O(1)
Approach 3: 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.
The time complexity is O(1).
function smallestEvenMultiple(n: number): number {
return n % 2 === 0 ? n : n * 2;
}func smallestEvenMultiple(n int) int {
if n%2 == 0 {
return n
}
return n * 2
}impl Solution {
pub fn smallest_even_multiple(n: i32) -> i32 {
if n % 2 == 0 {
return n;
}
n * 2
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Multiply and Check | Time Complexity: O(1) |
| Bit Manipulation | Time Complexity: O(1) |
| Mathematics | – |
Complexity analysis
| 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 |
Frequently asked questions
Is Smallest Even Multiple easy or hard?
Smallest Even Multiple is an Easy-level problem with an acceptance rate around 88%. The challenge is recognizing that the result equals LCM(n, 2), which reduces the solution to a simple even/odd check.
Smallest Even Multiple Python/Java solution
The core logic is identical across languages. In Python or Java, check if n % 2 == 0 and return n; otherwise return n * 2. Some implementations use (n & 1) to detect odd numbers with a bitwise operation.
How to solve Smallest Even Multiple in O(1)?
Check whether the number is even. If n is even, it already contains the factor 2 so the answer is n. If n is odd, multiply it by 2 to get the smallest even multiple. This constant-time check avoids loops or extra computations.
What is the best approach for Smallest Even Multiple?
The optimal approach checks whether n is even. If n % 2 == 0, return n; otherwise return 2 * n. This works because the problem is equivalent to computing LCM(n, 2). The algorithm runs in O(1) time and O(1) space.
Is Smallest Even Multiple asked at Google/Amazon/Meta?
This problem is categorized as an easy math and number theory question commonly used in coding platforms to test basic reasoning and edge-case handling. It is more typical in screening rounds or practice sets rather than advanced on-site interviews at companies like Google or Meta.
What data structure is used in Smallest Even Multiple?
No data structures are required. The solution relies purely on arithmetic and parity checks using modulo or bit manipulation, making it a straightforward math-based problem.
What is the time complexity of Smallest Even Multiple?
The solution runs in O(1) time because it performs only a constant number of operations: a parity check and possibly one multiplication. Space complexity is also O(1) since no additional data structures are required.
