Power of Two
JavaScript
easy
20 mins
Determine if a given integer is a power of two using efficient bit manipulation techniques.
Problem
Given an integer n, return true if it is a power of two. Otherwise, return false.
A number is a power of two if there exists an integer x such that n === 2^x.
Examples
Input: n = 1 Output: true Explanation: 2^0 = 1 Input: n = 16 Output: true Explanation: 2^4 = 16 Input: n = 3 Output: false Explanation: 3 is not a power of 2 Input: n = 0 Output: false Explanation: 0 is not a power of 2
Constraints
- -2^31 ≤ n ≤ 2^31 - 1
- Return
falsefor:- n ≤ 0
- non-integer inputs
Companies:
adobe
google
amazon
oracle
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
