it's worth studying bit manipulation. Not really that tricky (I'm dumb and can understand it), and feels good when you solve problems like this.
@servantofthelord81472 ай бұрын
ikr!
@李炎-d5q7 ай бұрын
another similar solution: return n > 0 and n == (n & -n)
@wp44447 ай бұрын
I'm new to code katas and python in general: Why not just: return n > 0 and math.log2(n).is_integer()? Using built-in libraries is forbidden during code interviews? I know it's probably not the fastest and clearest solution but I think it is still easier to get then bitwise operations. Thank you!
@ecchioni7 ай бұрын
Funny how the fastest solution for me was a loop.
@DeathSugar7 ай бұрын
Coz under hood it could optimize out your loop to popcnt instruction which makes your loop hardware accelerated.
@ecchioni7 ай бұрын
@@DeathSugar Yes, I know that. The moral of the story, don't try to be too clever.
@edis671games7 ай бұрын
cool info about bit operations
@jshaikh38974 ай бұрын
Hi all, I came up with the following solution when I was trying for a brute force approach. Apologies if this sounds silly but Is this good or bad brute force approach to come up with, while interviewing : class Solution: def isPowerOfTwo(self, n: int) -> bool: # base case: if n==1: return True # check if n is odd or less than 1 or check if n has more than one bits set to 1 if ((n%2)!=0) or n
@satyamjha687 ай бұрын
A one liner problem!! I am actually unable to solve hard questions with acceptance rete less than 60%. Do you have any tips on how I can improve?
@SairamDasari20007 ай бұрын
Practice Practice.... That's it
@axaide42107 ай бұрын
Practice as much as you can
@Munchen8887 ай бұрын
Even if you can’t pass the task don’t use chat gpt. If you don’t bring the task to end you are sure that you tried to solve YOURSELF.
@satyamjha687 ай бұрын
@@Munchen888 I never use gpt for dsa. Even I am unable to solve it like after an hour or 2 , I try to see hints. If I am still unable to solve the problem , I look at the solution. Is this approach fine?
@suhasnadiga7 ай бұрын
Didn't spend more than 30 mins
@kartikeysaini72725 ай бұрын
should i quit programming I think my solution is shit: class Solution: def isPowerOfTwo(self, n: int) -> bool: l=[] for i in range(0,100): l.append(2**i) for i in range(0,len(l)): if n == l[i]: return True else: return False
@shenawy043 ай бұрын
This is a bit more intuitive I think. class Solution: def isPowerOfTwo(self, n: int) -> bool: if n
@ETR129352 ай бұрын
Here after getting runtime error with loops haha
@Goe-k5w4 ай бұрын
What about a binary search solution For example 2 ^ x = 16 We will calculate all the possible values to x is between 0 & 16 and by using binary search. we will get the mid check if 2 ^ mid is greater or smaller or equal
@MarriRahul7 ай бұрын
similar answer: if n
@anon_y_mousse7 ай бұрын
I personally prefer the subtractive method because not every processor has divide, and divide is slower anyway even when available.
@dingus23327 ай бұрын
You can achieve the answer by left shift as well -> class Solution: def isPowerOfTwo(self, n: int) -> bool: for i in range(32): ans = 1
@StephenRoseDuo7 ай бұрын
My solution is 99th percentile and uses sets
@MehmetDemir-xi3yy7 ай бұрын
I couldn't get it the constant time explanation. How it is constant time even if it is integer max value? If so let's say when we do linear search in list which has size of int max value, can we say it is constant time?
@MehmetDemir-xi3yy7 ай бұрын
Btw, I am referring the first solution which uses loop. It will iterate logn times as you said.
@NeetCodeIO7 ай бұрын
Technically the if input n is no larger than 2^32 so its O(32) = O(1). In theory, if the input isnt bounded it's O(logn). I guess O(logn) is the more correct time complexity.
@MehmetDemir-xi3yy7 ай бұрын
So I guess you said it for in practical terms and it makes really sense. Don't get me wrong I am just trying to understand correctly not trying to be smart mouth. I understood it will iterate max 32 times(ish) but it is still bounded to N even if they are small numbers because we will log it. It may iterate 4 or 16 times according to n as you know.
@NeetCodeIO7 ай бұрын
@@MehmetDemir-xi3yy no worries, i think you're right that btw
@leeroymlg46927 ай бұрын
@@MehmetDemir-xi3yy it's technically O(logn). Since the time complexity does scale as the input size increases. But we know worst case it won't iterate more than 30 times. So it can be thought of as 0(30) since big O does focus on the worst case performance This function below is slower but it's 0(1) because it's guaranteed to loop 30 times regardless of n. powers = [1] p = 1 for i in range(30): p *= 2 powers.append(p) return True if n in powers else False
@MohanRaj-vp1zt7 ай бұрын
Mathematically, even -1 is an integer.
@NeetCodeIO7 ай бұрын
Yes that's right. I don't think i stated otherwise anywhere in the video.
@MohanRaj-vp1zt7 ай бұрын
@@NeetCodeIO : Thanks for your prompt reply :) . But you said - "We are told, that we given integers only, so we don't worry about negative exponents."
@driss99497 ай бұрын
@@MohanRaj-vp1zt and what does -1 have to do with a negative exponent ?