Power of 2
Thoughts
0:00 This sounds like a super easy question that I should be able to solve in 2 minutes. But since this question is in the string section, so I don't think it's going to be that simple. I'll try it once.
10:00 It's not simple at all. The question asks to check if the given number, which is very huge and in a string, is a power of two or not.
So it's very similar to the factorial problem where we need to do multiplication manually.
I'm getting some errors. I'll take it to OnlineGDB.
25:00 Turns out I messed up in the multiplication function. I forgot to reverse the result of the multiplication.
Also, I forgot some edge cases.
28:00 Done from my side. Let me take a loot at the given solution.
Their solution is not even following the constraint. They are converting the string to
intand then doing the basic bit manipulation technique.This reminds me that I have forgotten the bit manipulation technique to check for the power of two. Time to see notes.
Time Complexity: (nlogn)
Space Complexity: O(n) for products of 2.
Edge Cases:
You initialized
res = 1But is 1 being equal to given number a valid case?What if the input is negative?
Last updated