algoadvance

Leetcode 2413. Smallest Even Multiple

Problem Statement

Given a positive integer n, return the smallest positive integer that is a multiple of both n and 2.

Clarifying Questions

  1. Q: What is the range of n?
    • A: n is a positive integer, and for this problem, you can assume typical constraints such as 1 <= n <= 1000.
  2. Q: Can n be 1?
    • A: Yes, n can be 1.
  3. Q: Should we handle large numbers or only typical input ranges?
    • A: Typical ranges suffices, typically up to 1000 or similar.

Strategy

The task involves finding the smallest positive integer that is a multiple of both n and 2.

Code

#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;
}

Time Complexity

The time complexity of this solution is (O(1)) because:

Space Complexity

The space complexity is (O(1)) because:

Feel free to ask further questions if any arise!

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI