Smallest power of 2 greater than or equal to n
Basic Accuracy: 34.74% Submissions: 791 Points: 1

Given a number N.Find the nearest number which is a power of 2 and greater than or equal to N.

Example 1:

Input:
N = 1
Output:
1
Explanation:
The nearest power of 2 greater than or 
equal to 1 is 2^0=1.

Example 2:

Input:
N = 5
Output:
8
Explanation:
The nearest power of 2 greater than 5 is 
2^3=8.

Your Task:
You don't need to read input or print anything.Your Task is to complete the function nearestPowerOf2() which takes a number N as input parameter and returns the nearest power of 2 that is greater than or equal to N.

Expected Time Complexity:O(LogN)
Expected Auxillary Space:O(1)

Constraints:
1<=N<=1018

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Smallest power of 2 greater than or equal to n

Output Window