forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
342a2be
commit 0604d1c
Showing
2 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Find MSB In O(1) | ||
================ | ||
Problem Statement | ||
You are given a positive integer N. Your task is to find the greatest integer less than or equal to N which is a power of 2. | ||
For Example: | ||
If N = 14, | ||
The nearest integer that is less than or equal to 14 and is a power of two is 8(2^3). So, the answer is 8. | ||
Follow Up: | ||
Can you solve this in constant time and space complexity? | ||
Input Format: | ||
The first line contains an integer 'T' which denotes the number of test cases. Then, the T test cases follow. | ||
The first and only line of each test case contains a single integer N. | ||
Output Format: | ||
For each test case, print the nearest integer that is less than or equal to N and also is a power of 2. | ||
Output for each test case will be printed in a separate line. | ||
Note: | ||
You do not need to print anything. It has already been taken care of. Just implement the given function. | ||
Constraints: | ||
1 <= T <= 2000 | ||
1 <= N <= 10^9 | ||
Time limit: 1 second | ||
Sample Input 1: | ||
2 | ||
4 | ||
22 | ||
Sample Output 1: | ||
4 | ||
16 | ||
Explanation For Sample 1: | ||
For the first test case, 4 itself is a power of two. | ||
For the second test case, the nearest integer that is less than or equal to 22 and also is a power of two is 16. | ||
Sample Input 2: | ||
2 | ||
1 | ||
63 | ||
Sample Output 2: | ||
1 | ||
32 | ||
*/ | ||
|
||
int findMSB(int n) | ||
{ | ||
n = n | n >> 1; | ||
n = n | n >> 2; | ||
n = n | n >> 4; | ||
n = n | n >> 8; | ||
n = n | n >> 16; | ||
|
||
n = n + 1; | ||
return (n >> 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters