Given an integer N, the task is to find the highest power of 2 that is smaller than or equal to N.
Input: N = 9
Highest power of 2 less than 9 is 8.
Input: N = -20
Highest power of 2 less than -20 is -32.
Input: N = -84
Approach: The idea is to use logarithm to solve the above problem. For any given number N, it can be either positive or negative. The following task can be performed for each case:
- If the input is positive: floor(log2(N)) can be calculated.
- If the input is negative: ceil(log2(N)) can be calculated and a -ve sign can be added to the value.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Highest and Smallest power of K less than and greater than equal to N respectively
- Highest power of 2 less than or equal to given number
- Highest power of a number that divides other number
- Highest power of 2 that divides a number represented in binary
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Highest power of 2 that divides the LCM of first N Natural numbers.
- Find Largest Special Prime which is less than or equal to a given number
- Find all powers of 2 less than or equal to a given number
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number less than N with digit sum greater than the digit sum of N
- Find all factorial numbers less than or equal to n
- Sum of largest prime factor of each number less than equal to n
- Find unique pairs such that each element is less than or equal to N
- Find three integers less than or equal to N such that their LCM is maximum
- Print all prime numbers less than or equal to N
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Count number of integers less than or equal to N which has exactly 9 divisors
- Largest number less than or equal to N/2 which is coprime to N
- Greatest number less than equal to B that can be formed from the digits of A
- Print all Semi-Prime Numbers less than or equal to N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01