Power of Four - Easy - LeetCode
💻 coding

Power of Four - Easy - LeetCode

1 min read 68 words
1 min read
ShareWhatsAppPost on X
  • 1The task is to determine if a given integer is a power of 4.
  • 2The solution involves checking if the number is positive and a power of two.
  • 3The function operates with O(1) time and space complexity.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

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

Power of Four - Easy - LeetCode

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example 1:

Input: 16 Output: true Example 2:

Input: 5 Output: false Follow up: Could you solve it without loops/recursion?

public class Solution {
 public bool IsPowerOfFour(int num) {
 if(num<=0){
 return false; 
 }
 
 if((num & (num-1))!= 0){
 return false;
 }
 
 return (num-1)%3==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 5 October 2020 · 1 min read · 68 words

Part of AskGif Blog · coding

You might also like