Power of Two - Math - Easy - LeetCode
💻 coding

Power of Two - Math - Easy - LeetCode

1 min read 95 words
1 min read
ShareWhatsAppPost on X
  • 1The task is to determine if a given integer n is a power of two.
  • 2The function returns true for inputs like 1, 16, and 4, and false for inputs like 3 and 5.
  • 3The solution has a time complexity of O(1) and a space complexity of O(1).

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The task is to determine if a given integer n is a power of two."

Power of Two - Math - Easy - LeetCode

Given an integer n, write a function to determine if it is a power of two.

Example 1:

Input: n = 1 Output: true Explanation: 20 = 1 Example 2:

Input: n = 16 Output: true Explanation: 24 = 16 Example 3:

Input: n = 3 Output: false Example 4:

Input: n = 4 Output: true Example 5:

Input: n = 5 Output: false

Constraints:

-231 <= n <= 231 - 1

public class Solution {
 public bool IsPowerOfTwo(int n) {
 if(n<=0){
 return false;
 }
 return ((n&(n-1))==0);
 }
}

Time Complexity: O(1)

Space Complexity: O(1)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 1 October 2020 · 1 min read · 95 words

Part of AskGif Blog · coding

You might also like

Power of Two - Math - Easy - LeetCode | AskGif Blog