L&T Mindtree · Question 74
Power of Two
Problem
You are given integer n. Check if it is a power of two.
Approach
A power of two has exactly one bit set: n & (n-1) clears it to zero.
Time O(1)Space O(1)
How it works
- 1Positive numbers only.
- 2One set bit clears to exactly zero.
Sample input / output
Input: n = -16 Output: false
Constraints
-2^31 <= n < 2^31.
Solution — read it, then walk it
Time O(1)Space O(1)Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/7
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits