71. Power of Four(bitwise 사용)
07 Sep 2020 | Daily Algorithms
Explanation
- Solution Bit Manipulation
the idea is :
as we need to find the Num is power of 4 or not
We Can Solve it by doing Bit Manipulation
- [if Any num is valid power of 4 then it should be valid power of 2 as well] <- use this property
Let’s Suppose it they are asking for finding is the num is power of 2 or not then what should we do
We just trying to find is that Num Have only one set bit or Not
if there is only One set bit exist then we can blindly say that it’s A valid power of 2
so, the same idea we need here also but need to check one more condition.
what is the 2nd Condition would be. Let’s Find out by analysing Few Example
1 2 4 8 16 32 64 128 256 592 1084 …… -> This All Are Power Of Two [ Mean Having Only One Set Bit ]
1 4 16 64 256 1084……-> This All Are Power Of Four , Now We can Greedly Observe That Between two power_of_4 there Was One Invaild Power_Of_4 Exist
So Conditions Are :
- No of Set Bit Always Be Only ONE
- That SetBit Possition Should Be always In odd Place [ starting from Left ]
Let’s See Example :
No Bit_re SetBitPos No_of_setBit Valid_power_of_four
64 1 0 0 0 0 0 0 7 1 True <= :)
128 1 0 0 0 0 0 0 0 8 1 False <=2nd Condition fail :(
18 1 0 0 1 0 5,2 2 False <=1st Condition Fail :(
32 1 0 0 0 0 0 6 1 False <=2nd Condition fail :(
Time Complexcity O ( 1 ) =>as we Only Traverse max 32 Bit
Space Complexcity O ( 1) => No Extra Space
Solution 1
class Solution {
public:
bool isPowerOfFour(int num) {
if(num<0)
return false;
for(int i=0;i<32;i+=2)
{
if(num==1<<i) // 두개씩 왼쪽 쉬프트해서 1이 되는 값이 나오면 true
return true;
}
return false;
}
};
Solution 2
return num>0&&!(num&(num-1))&&!((num-1)%3);
Explanation
- Solution Bit Manipulation the idea is : as we need to find the Num is power of 4 or not We Can Solve it by doing Bit Manipulation
- [if Any num is valid power of 4 then it should be valid power of 2 as well] <- use this property
Let’s Suppose it they are asking for finding is the num is power of 2 or not then what should we do We just trying to find is that Num Have only one set bit or Not if there is only One set bit exist then we can blindly say that it’s A valid power of 2
so, the same idea we need here also but need to check one more condition.
what is the 2nd Condition would be. Let’s Find out by analysing Few Example
1 2 4 8 16 32 64 128 256 592 1084 …… -> This All Are Power Of Two [ Mean Having Only One Set Bit ]
1 4 16 64 256 1084……-> This All Are Power Of Four , Now We can Greedly Observe That Between two power_of_4 there Was One Invaild Power_Of_4 Exist
So Conditions Are :
- No of Set Bit Always Be Only ONE
- That SetBit Possition Should Be always In odd Place [ starting from Left ]
Let’s See Example :
No Bit_re SetBitPos No_of_setBit Valid_power_of_four 64 1 0 0 0 0 0 0 7 1 True <= :) 128 1 0 0 0 0 0 0 0 8 1 False <=2nd Condition fail :( 18 1 0 0 1 0 5,2 2 False <=1st Condition Fail :( 32 1 0 0 0 0 0 6 1 False <=2nd Condition fail :(
Time Complexcity O ( 1 ) =>as we Only Traverse max 32 Bit Space Complexcity O ( 1) => No Extra Space
Solution 1
class Solution {
public:
bool isPowerOfFour(int num) {
if(num<0)
return false;
for(int i=0;i<32;i+=2)
{
if(num==1<<i) // 두개씩 왼쪽 쉬프트해서 1이 되는 값이 나오면 true
return true;
}
return false;
}
};
Solution 2
return num>0&&!(num&(num-1))&&!((num-1)%3);
Comments