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:
- Highest power of 2 less than or equal to given number
- Highest and Smallest power of K less than and greater than equal to N respectively
- 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 a number that divides other number
- Find whether a given integer is a power of 3 or not
- Elements of Array which can be expressed as power of some integer to given exponent K
- Count pairs in Array whose product is a Kth power of any positive integer
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Smallest power of 4 greater than or equal to N
- Smallest power of 2 greater than or equal to n
- Minimum number of power terms with sum equal to n
- Generate integer from 1 to 7 with equal probability
- Smallest Integer to be inserted to have equal sums
- Find the sum of power of bit count raised to the power B
Improved By : AnkitRai01