Given a non-negative integer N, the task is to check if N is a power of 2. Print 'YES' if N is the power of 2; else, print 'NO'.
Question
Given a non-negative integer N, the task is to check if N is a power of 2. Print 'YES' if N is the power of 2; else, print 'NO'.
Solution
Sure, here is a step-by-step guide to solve this problem:
-
First, we need to understand what it means for a number to be a power of 2. A number is a power of 2 if it can be expressed as 2^n for some non-negative integer n. For example, 2, 4, 8, 16 are all powers of 2 because they can be expressed as 2^1, 2^2, 2^3, 2^4 respectively.
-
A simple way to check if a number is a power of 2 is to keep dividing the number by 2 until we get a number that is not divisible by 2. If that number is 1, then the original number is a power of 2. If it's anything other than 1, then the original number is not a power of 2.
-
Here is a simple algorithm to implement this:
- If N is 0, print 'NO' because 0 is not a power of 2.
- While N is greater than 1:
- If N is not divisible by 2, print 'NO' and stop.
- Divide N by 2.
- If we've exited the loop, print 'YES' because N is a power of 2.
-
This algorithm works because any number that is a power of 2 will eventually be reduced to 1 by repeatedly dividing by 2. Any number that is not a power of 2 will eventually produce a remainder when divided by 2.
-
This algorithm runs in O(log N) time because in the worst case, we have to divide N by 2 up to log N times.
Similar Questions
Single File Programming QuestionProblem StatementGiven a non-negative integer N, the task is to check if N is a power of 2. Print 'YES' if N is the power of 2; else, print 'NO'.Example 1Input: N = 8Output: YESExplanation: 8 is equal to 2 raised to 3 (23 = 8).Example 2Input: N = 98Output: NOExplanation: 98 cannot be obtained by any power of 2.Note: This question was asked in Adobe coding test.Input format :The input consists of an integer N, representing the non-negative integer.Output format :If N is a power of 2, the output is "YES".If N is not a power of 2, the output is "NO".Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ N ≤ 512Sample test cases :Input 1 :20Output 1 :NOInput 2 :64Output 2 :YESInput 3 :98Output 3 :NONote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks :
Given an integer n, return true if it is a power of two. Otherwise, return false.An integer n is a power of two, if there exists an integer x such that n == 2x.
Find whether the given number is power of 2 or not.Input Format:Accept an Integer as inputOutput Format:Print the output as "YES" or "NO"Constraints:1<=INPUT<=10^15Sample Input 1:128Sample Output 1:YESSample Input 2:36Sample Output 2:NO
What output will the following code produce?n = 10while n != 1: print (n,) if n % 2 == 0: # n is even n = n // 2 else: # n is odd n = n * 3 + 1Question 17Select one:a.10 5 16 8 4 2b.None an error will be displayedc.8 4 2d.9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2
Given an integer n, return true if it is a power of three. Otherwise, return false.An integer n is a power of three, if there exists an integer x such that n == 3x.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.