Given an integer n, return the smallest power of 2 that is greater than or equal to n.
A power of 2 is a number of the form 2^k where k is a non-negative integer: 1, 2, 4, 8, 16, 32, ...
If n is null, return null. If n is less than or equal to 0, return 1 (since 2^0 = 1 is the smallest power of 2).
Example 1:
Input: n = 8
Output: 8
Explanation: 8 is already a power of 2 (2^3), so return it as-is
Example 2:
Input: n = 5
Output: 8
Explanation: The next power of 2 after 5 is 8 (2^3)
Example 3:
Input: n = 1
Output: 1
Explanation: 1 equals 2 to the power of 0, so it is already a power of 2
Example 4:
Input: n = null
Output: null
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay August 2026
Contest runs August 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.