We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Smallest Even Multiple

Number: 2491

Difficulty: Easy

Paid? No

Companies: Adobe, Apple


Problem Description

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


Key Insights

  • The result must be even since it is a multiple of 2.
  • If n is even, then n is already the smallest even multiple.
  • If n is odd, multiplying n by 2 yields the smallest even multiple.

Space and Time Complexity

Time Complexity: O(1) since the solution involves a constant number of operations. Space Complexity: O(1) as we only use a constant amount of space.


Solution

The problem is solved by checking if n is even or odd. If n is even, n is returned because it is a multiple of 2. If n is odd, then multiplying n by 2 gives the smallest even multiple. This solution uses a simple conditional check and arithmetic operation without extra data structures, achieving O(1) time and space complexity.


Code Solutions

# Function to return the smallest even multiple of n
def smallest_even_multiple(n):
    # If n is even, return n
    if n % 2 == 0:
        return n
    else:
        # If n is odd, multiply by 2 to get an even number
        return n * 2

# Test cases
print(smallest_even_multiple(5))  # Expected output: 10
print(smallest_even_multiple(6))  # Expected output: 6
← Back to All Questions