LeetCode 2413: Smallest Even Multiple Solution

Master LeetCode problem 2413 (Smallest Even Multiple), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2413. Smallest Even Multiple

Problem Explanation

Explanation:

To find the smallest positive integer that is a multiple of both 2 and n, we can simply multiply n by 2 if n is already even, or multiply n by 2 until it becomes even. This is because any multiple of n will also be a multiple of 2 if n is even.

  • Initialize a variable result to n.
  • If n is odd, multiply n by 2 until it becomes even.
  • Return the result.

Time Complexity:

The time complexity of this solution is O(log n) because we might need to multiply n by 2 multiple times to make it even.

Space Complexity:

The space complexity of this solution is O(1) as we are using only a constant amount of extra space.

:

Solution Code

class Solution {
    public int smallestEvenMultiple(int n) {
        int result = n;
        while (result % 2 != 0) {
            result *= 2;
        }
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2413 (Smallest Even Multiple)?

This page provides optimized solutions for LeetCode problem 2413 (Smallest Even Multiple) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2413 (Smallest Even Multiple)?

The time complexity for LeetCode 2413 (Smallest Even Multiple) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2413 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2413 in Java, C++, or Python.

Back to LeetCode Solutions