DEV Community

Piyush Acharya
Piyush Acharya

Posted on

Fastest Java Solution!! πŸ”₯πŸ”₯πŸ”₯

Intuition

This problem is really simple if you think about the 2 types of testcases. First, the number inputted can be divisible by 2. In this case, we can just return the number itself since it satisfies all of the criteria. Else, you can just return the number times 2.

Complexity

  • Time complexity: O(1)
  • Space complexity: O(1)

Code

class Solution {
    public int smallestEvenMultiple(int n) {
        if (n % 2 == 0) {
            return n;
        } else {
            return n * 2;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)