Sign in with Google

Google will share your name, email, and profile picture with DevExCode. See our privacy policy.

LeetCode 2413: Smallest Even Multiple

MathNumber Theory

LeetCode 2413 Solution 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.

:

LeetCode 2413 Solutions in Java, C++, Python

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

Interactive Code Editor for LeetCode 2413

Improve Your LeetCode 2413 Solution

Use the editor below to refine the provided solution for LeetCode 2413. Select a programming language and try the following:

  • Add import statements if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.

Loading editor...

Related LeetCode Problems