Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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 summary: Given a positive integer n, return the smallest positive integer that is a multiple of both 2 and n.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
5
6
greatest-common-divisor-of-strings)three-divisors)find-greatest-common-divisor-of-array)convert-the-temperature)minimum-cuts-to-divide-a-circle)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2413: Smallest Even Multiple
class Solution {
public int smallestEvenMultiple(int n) {
return n % 2 == 0 ? n : n * 2;
}
}
// Accepted solution for LeetCode #2413: Smallest Even Multiple
func smallestEvenMultiple(n int) int {
if n%2 == 0 {
return n
}
return n * 2
}
# Accepted solution for LeetCode #2413: Smallest Even Multiple
class Solution:
def smallestEvenMultiple(self, n: int) -> int:
return n if n % 2 == 0 else n * 2
// Accepted solution for LeetCode #2413: Smallest Even Multiple
impl Solution {
pub fn smallest_even_multiple(n: i32) -> i32 {
if n % 2 == 0 {
return n;
}
n * 2
}
}
// Accepted solution for LeetCode #2413: Smallest Even Multiple
function smallestEvenMultiple(n: number): number {
return n % 2 === 0 ? n : n * 2;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.