[LeetCode] Algorithm/Smallest Even Multiple/LeetCode 2413

JIEUN YANG·2022년 9월 20일

This problems is to get a smallest mutiple of both '2' and 'n'.
To do solve it,
Firstly, We need to know what a multiple is.
A multiple is a number that can be divied by another number without a remainder. In other words, a number mutiplied by 1 time 2 times, or 3 times.
Secondly, check the conditionals based on whether a remainder is equals to '0' or not when a positive 'n' is divided by '2'


If Yes?

We only have to return 'n', because 'n' is abled to be divided by '2', which means 'n' is a mutiple of '2' and the smallest even multiple would be 'n' both '2' and 'n'

If No ?

All we have to do is to multiply '2' by 'n'.
Since 2n will be always even, and not only 2n is a mutiple of '2' but also 'n'

So the code i wrote is this.

var smallestEvenMultiple = function(n) {
    // n이 2의 배수인지 확인
    // 맞으면 n return => 2와 n의 공통된 최소배수는 n이니까
    // 틀리면 n과 2를 곱함 => 2*n = 2n은 2와 n의 최소 양의 배수이니까
    if(0 > n || n > 151) return //n의 범위 설정 
    if(n%2 === 0) return n
    else return n * 2 
};


References

LeetCode 문제
한량 지아이 블로그

profile
violet's development note

0개의 댓글