# Smallest Even Multiple
**Difficulty:** EASY
[External](https://leetcode.com/problems/smallest-even-multiple)
Canonical: https://scaleengineer.com/dsa/problems/smallest-even-multiple
**Patterns:** [Math](https://scaleengineer.com/dsa/patterns/math), [Number Theory](https://scaleengineer.com/dsa/patterns/number-theory)
---
## Problem
Given a **positive** integer `n`, return _the smallest positive integer that is a multiple of **both**_ `2` _and_ `n`. 

**Example 1:**

**Input:** n = 5
**Output:** 10
**Explanation:** The smallest multiple of both 5 and 2 is 10.

**Example 2:**

**Input:** n = 6
**Output:** 6
**Explanation:** The smallest multiple of both 6 and 2 is 6. Note that a number is a multiple of itself.

**Constraints:**

* `1 <= n <= 150`

# Approaches
## Iterative Search
This approach simulates a search for the smallest common multiple. We start with the number `n` and check its multiples one by one (`n`, `2n`, `3n`, ...) until we find one that is also a multiple of 2 (i.e., an even number).
**Time:** O(1). The loop will execute at most twice. If `n` is even, it executes once. If `n` is odd, it executes twice (for `n` and `2n`). · **Space:** O(1). We only use a single extra variable to store the current multiple.
**Pros:** Simple to understand and directly models the process of finding a multiple.; Guaranteed to be correct and terminates quickly for this problem's constraints.
**Cons:** Involves a loop, which is slightly less direct and efficient than a mathematical or bitwise solution.; For this specific problem, the performance difference is negligible, but as a general approach for finding LCM, it can be slow if the numbers are large.
### Explanation
The algorithm starts by initializing a candidate number, let's call it `multiple`, to the input `n`.
It then enters a loop. Inside the loop, it checks if `multiple` is divisible by 2.
If `multiple % 2 == 0`, it means we have found the smallest positive integer that is a multiple of both `n` and 2. The loop terminates, and this value is returned.
If `multiple` is not divisible by 2, we need to check the next multiple of `n`. We update `multiple` by adding `n` to it (`multiple = multiple + n`).
The loop continues until an even multiple is found. Since `2 * n` is always an even multiple of `n`, this loop is guaranteed to terminate quickly.
```java
class Solution {
    public int smallestEvenMultiple(int n) {
        int multiple = n;
        while (true) {
            if (multiple % 2 == 0) {
                return multiple;
            }
            multiple += n;
        }
    }
}
```
### Algorithm
- 1. Initialize a variable `multiple` to `n`.
- 2. Start a `while` loop that runs indefinitely.
- 3. Inside the loop, check if `multiple` is even using the modulo operator (`multiple % 2 == 0`).
- 4. If it is even, return `multiple` as it is the smallest even multiple of `n`.
- 5. If it is odd, update `multiple` to the next multiple of `n` by adding `n` to it (`multiple += n`).

## Mathematical Approach with Conditional Logic
This approach uses basic number theory. The problem asks for the Least Common Multiple (LCM) of `n` and 2. We can determine the result by simply checking if `n` is even or odd.
**Time:** O(1). The solution involves a single modulo operation, a comparison, and potentially a multiplication, all of which are constant-time operations. · **Space:** O(1). No extra space is required beyond the input storage.
**Pros:** Very efficient and directly solves the problem based on its mathematical properties.; The code is clean, easy to read, and requires no loops.
**Cons:** No significant cons for this problem, as it's a direct and optimal solution.
### Explanation
The logic is based on two cases:
- **Case 1: `n` is even.** If `n` is already a multiple of 2, then `n` itself is the smallest positive number that is a multiple of both `n` and 2. For example, if `n=6`, the smallest even multiple is 6.
- **Case 2: `n` is odd.** If `n` is not a multiple of 2, we need to find the smallest multiple of `n` that is even. The multiples of `n` are `n, 2n, 3n, ...`. Since `n` is odd, the first multiple, `n`, is odd. The second multiple, `2n`, is guaranteed to be even. Therefore, `2n` is the smallest even multiple. For example, if `n=5`, the smallest even multiple is `2 * 5 = 10`.
This logic can be implemented with a simple `if-else` statement or a ternary operator.
```java
class Solution {
    public int smallestEvenMultiple(int n) {
        if (n % 2 == 0) {
            return n;
        } else {
            return n * 2;
        }
    }
}

// Using a ternary operator for conciseness:
class SolutionTernary {
    public int smallestEvenMultiple(int n) {
        return (n % 2 == 0) ? n : n * 2;
    }
}
```
### Algorithm
- 1. Check if `n` is even by computing `n % 2`.
- 2. If the result is 0, `n` is even. Return `n`.
- 3. If the result is not 0, `n` is odd. Return `n * 2`.

## Optimized Bitwise Approach
This is the most optimized approach, leveraging bitwise operations to check for even/odd and perform multiplication. It achieves the same result as the conditional check but can be slightly faster at the machine code level and is more concise.
**Time:** O(1). This involves a couple of bitwise operations, which are extremely fast single-CPU instructions. · **Space:** O(1). No auxiliary data structures are used.
**Pros:** Most concise and potentially the fastest implementation due to the use of efficient bitwise operations.; Avoids conditional branching, which can sometimes lead to better performance on modern CPUs.
**Cons:** The logic might be slightly less intuitive for developers not familiar with bitwise operations compared to a standard `if-else` check.
### Explanation
The core idea is the same: if `n` is even, return `n`; if `n` is odd, return `2n`. However, we use bitwise operators for the logic.
- **Checking for odd/even:** A number is odd if its least significant bit (LSB) is 1. We can check this using the bitwise AND operator: `(n & 1)`. If `n` is even, `(n & 1)` is 0. If `n` is odd, `(n & 1)` is 1.
- **Multiplying by 2:** Multiplying an integer by 2 is equivalent to a left bit shift by 1 (`n << 1`).
We can combine these ideas into a single, elegant expression: `n << (n & 1)`.
Let's analyze this expression:
- If `n` is even, `(n & 1)` evaluates to 0. The expression becomes `n << 0`, which is `n`.
- If `n` is odd, `(n & 1)` evaluates to 1. The expression becomes `n << 1`, which is `n * 2`.
This single line of code correctly handles both cases without any explicit branching (`if`/`else` or ternary).
```java
class Solution {
    public int smallestEvenMultiple(int n) {
        // If n is even, (n & 1) is 0, so n << 0 is n.
        // If n is odd, (n & 1) is 1, so n << 1 is n * 2.
        return n << (n & 1);
    }
}
```
### Algorithm
- 1. Perform a bitwise AND of `n` with 1 (`n & 1`). This result is 0 if `n` is even and 1 if `n` is odd.
- 2. Perform a left bit shift on `n` by the result of the previous step.
- 3. Return the final value.

# Solutions
### Java

```java
class Solution {
public
  int smallestEvenMultiple(int n) { return n % 2 == 0 ? n : n * 2; }
}

```

### CPP

```cpp
class Solution {
public:
  int smallestEvenMultiple(int n) { return n % 2 == 0 ? n : n * 2; }
};

```

### Python

```python
class Solution:
    def smallestEvenMultiple(
        self, n: int) -> int: return n if n % 2 == 0 else n * 2

```
