VOOZH about

URL: https://www.geeksforgeeks.org/dsa/previous-smaller-integer-one-less-number-set-bits/

⇱ Previous smaller integer having one less number of set bits - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Previous smaller integer having one less number of set bits

Last Updated : 23 Jul, 2025

Given a positive integer ā€˜n’ having ā€˜x’ number of set bits in its binary representation. The problem is to find the previous smaller integer(greatest integer smaller than n), having (x-1) number of set bits in its binary representation.
Note: 1 <= n 

Examples : 

Input : 8
Output : 0
(8)10 = (1000)2
is having 1 set bit.

(0)10 = (0)2
is having 0 set bit and is the previous smaller.

Input : 25
Output : 24

The following are the steps: 

  1. Find the position of the rightmost set bit(considering last bit at position 1, second last bit at position 2 and so on) in the binary representation of n. Let the position be represented by pos. Refer this post.
  2. Turn off or unset the bit at position pos. Refer this post.

Output
24

Time Complexity: O(1)
Auxiliary Space: O(1)


 

Comment
Article Tags:
Article Tags: