Leetcode 2413. Smallest Even Multiple
Given a positive integer n
, return the smallest positive integer that is a multiple of both n
and 2
.
n
?
n
is a positive integer, and for this problem, you can assume typical constraints such as 1 <= n <= 1000
.n
be 1?
n
can be 1.The task involves finding the smallest positive integer that is a multiple of both n
and 2
.
n
is already even, the smallest multiple is n
. If n
is odd, the smallest even multiple will be 2 * n
.#include <iostream>
class Solution {
public:
int smallestEvenMultiple(int n) {
return n % 2 == 0 ? n : 2 * n;
}
};
int main() {
Solution solution;
std::cout << solution.smallestEvenMultiple(5) << std::endl; // Outputs 10
std::cout << solution.smallestEvenMultiple(6) << std::endl; // Outputs 6
return 0;
}
The time complexity of this solution is (O(1)) because:
The space complexity is (O(1)) because:
Feel free to ask further questions if any arise!
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?