π₯ In this video, we solve LeetCode Problem 2413 β Smallest Even Multiple in a simple and beginner-friendly way.
π Problem Statement:
Given a positive integer "n", return the smallest positive integer that is a multiple of both "n" and "2".
---
π‘ Approach Covered:
β Method 1: Using Conditional Statement
- If "n" is even β answer is "n"
- If "n" is odd β answer is "2 Γ n"
β Method 2: Using Simple For Loop
- Iterate numbers until you find a number divisible by both "n" and "2"
---
π» Code Implementation (C++):
// Method 1: Conditional
int smallestEvenMultiple(int n) {
if (n % 2 == 0) return n;
return n * 2;
}
// Method 2: Loop
int smallestEvenMultiple(int n) {
for (int i = 1; ; i++) {
if (i % n == 0 && i % 2 == 0) {
return i;
}
}
}
---
β±οΈ Time Complexity:
- Conditional: O(1)
- Loop: O(n) (in worst case)
---
π― Why this problem is important?
- Helps understand LCM (Least Common Multiple) concept
- Great for beginners starting with problem-solving on LeetCode
---
π If you found this helpful, donβt forget to Like, Share & Subscribe for more coding content!
#LeetCode #Coding #Cplusplus #DSA #BeginnerFriendly #ProblemSolving
Download
0 formats
No download links available.
LeetCode 2413 Smallest Even Multiple | C++ Solution | Loop vs Conditional π‘ Beginner Friendly Tips | NatokHD