VOOZH about

URL: https://www.geeksforgeeks.org/dsa/logarithm-tricks-for-competitive-programming/

⇱ Logarithm tricks for Competitive Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Logarithm tricks for Competitive Programming

Last Updated : 12 Jul, 2025

Logarithm: is the inverse function of the exponentiation which means the logarithm value of a given number x is the exponent to another number. 

Below are some tricks using Logarithmic function which can be handy in competitive programming. 

Checking if a number is a power of two or not:


Given an integer N, the task is to check that if the number N is the power of 2. 

Examples:

Input: N = 8 
Output: Yes

Input: N = 6 
Output: No 

Approach: A simple method for this is to simply take the log of the number on base 2, if you get an integer then the number is the power of 2.

Below is the implementation of the above approach:  


Output
Yes

Time Complexity: O(logn)

Auxiliary Space: O(1)

Kth root of a Number

Given two integers N and K, the task is to find the Kth root of the number N.

Examples:

Input: N = 8, K = 3 
Output: 2

Input: N = 32, K = 5 
Output:

Approach: A simple solution is to use logarithmic function to find the Kth root of the number. Below is the illustration of the approach: 

Let D be our answer 
then, 
Applying on both side 
=> 
=> 
=> 

Below is the implementation of the above approach: 


Output
2

Time Complexity: O(logn)

Auxiliary Space: O(1)

Count digits in a Number:

Given an integer N, the task is to count the digits in a number N.

Examples:

Input: N = 243 
Output: 3

Input: N = 1000 
Output:

Approach: The idea is to find the logarithm of the number base 10 to count the number of digits.

Below is the implementation of the above approach: 


Output
2

Time Complexity: O(logn)

Auxiliary Space: O(1)

Check if N is a power of K or not:

Given two integers N and K, the task is to check if Y is power of X or not.

Examples:

Input: N = 8, K = 2 
Output: Yes

Input: N = 27, K = 3 
Output: Yes 

Approach: The idea is to take log of N in base K. If it turns out to be an integer, then N is a power of K.

Below is the implementation of the above approach:


Output
Yes

Time Complexity: O(logn)

Auxiliary Space: O(1)

To find the power of K greater than equal to and less than equal to N:

Given two integers N and K, the task is to find the power of K greater than equal to and less than equal to N.

Examples:

Input: N = 7, K = 2 
Output: 4 8

Input: N = 18, K = 3 
Output: 9 27 

Approach: The idea is to find the floor value of the log K value of the given integer N, Then compute the Kth power of this number to compute the previous and next Kth power.

Below is the implementation of the above approach: 


Output
4 8

Time Complexity: O(logn)

Auxiliary Space: O(1)

To Find the position of rightmost set bit:

Given an integer N, the task is to find the position of the rightmost set bit.

Examples:

Input: N = 7 
Output: 1

Input: N = 8 
Output:

Approach:

  • Take two's complement of the given no as all bits are reverted except the first '1' from right to left (0111)
  • Do a bit-wise & with original no, this will return no with the required one only (0100)
  • Take the log2 of the no, you will get (position - 1) (2)

Below is the implementation of the above approach: 


Output
4

Time Complexity: O(logn)

Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: