Menu Close

Leetcode 2413: Smallest Even Multiple Solution

Here, we will help to understand about how to solve Smallest Even Multiple Solution of leetcode 2413 problem with code and algorithm.

You are given a positive integer n. You have to return the smallest positive integer that is a multiple of both 2 and n.

Example:

1)
n = 5
Output: 10 
Explanation: 10 is a multiple of both 2 and 5 which means 10 can be divided by 2 and 5.

2)
n = 10
Output: 10
Explanation: 10 is a multiple of both 2 and 10 which means 10 can be divided by 2 and 10.

3)
n = 11
Output: 22
Explanation: 22 is a multiple of both 2 and 11 which means 22 can be divided by 2 and 11.

Smallest Even Multiple solution code in C++

Code 1:

#include <iostream>

using namespace std;

int smallestEvenMultiple(int n) {

    return (n & 1 == 1) ? 2 *n : n;

}
    
int main()
{
    cout<<smallestEvenMultiple(10);

    return 0;
}

Output:

10

Code 2:

#include <iostream>

using namespace std;

int smallestEvenMultiple(int n) {

    if((n & 1) == 0) {

        return n;

    } else {

        return n * 2;

    }

}

    
int main()
{

    cout<<smallestEvenMultiple(11);

    return 0;
}

Output:

22

Smallest Even Multiple solution code in Go

package main

import "fmt"


func smallestEvenMultiple(n int) int {

    if n % 2 == 0 {

        return n 

    }
    
    return n * 2
}


func main() {

    fmt.Println(smallestEvenMultiple(13))

}

Output:

26

To check more leetcode problem’s solution. Pls click given below link:

https://www.techieindoor.com/category/leetcode/

Referes:

https://leetcode.com/

Posted in C++, Easy, Leetcode

Leave a Reply

Your email address will not be published. Required fields are marked *