Skip to content
Related Articles

Related Articles

Previous smaller integer having one less number of set bits
  • Difficulty Level : Hard
  • Last Updated : 22 Mar, 2021
GeeksforGeeks - Summer Carnival Banner

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

 

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.

 

C++




// C++ implementation to find the previous
// smaller integer with one less number of
// set bits
#include<bits/stdc++.h>
using namespace std;
 
// function to find the position of
// rightmost set bit.
int getFirstSetBitPos(int n)
{
    return log2(n & -n) + 1;
}
 
// function to find the previous smaller
// integer
int previousSmallerInteger(int n)
{
    // position of rightmost set bit of n
    int pos = getFirstSetBitPos(n);
 
    // turn off or unset the bit at
    // position 'pos'
    return (n & ~(1 << (pos - 1)));
}
 
// Driver program
int main()
{
    int n = 25;
    cout << previousSmallerInteger(n);
    return 0;
}

Java




// Java implementation to find the previous
// smaller integer with one less number of
// set bits
class GFG {
     
    // function to find the position of
    // rightmost set bit.
    static int getFirstSetBitPos(int n)
    {
        return (int)(Math.log(n & -n) / Math.log(2)) + 1;
    }
 
    // function to find the previous smaller
    // integer
    static int previousSmallerInteger(int n)
    {
         
        // position of rightmost set bit of n
        int pos = getFirstSetBitPos(n);
 
        // turn off or unset the bit at
        // position 'pos'
        return (n & ~(1 << (pos - 1)));
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int n = 25;
        System.out.print("Previous smaller Integer ="
                     + previousSmallerInteger(n));
    }
}
 
// This code is contributed by Anant Agarwal.

Python3




# Python3 implementation to find
# the previous smaller integer with
# one less number of set bits
import math
 
# Function to find the position 
# of rightmost set bit.
def getFirstSetBitPos(n):
 
    return (int)(math.log(n & -n) /
                 math.log(2)) + 1
 
# Function to find the 
# previous smaller integer
def previousSmallerInteger(n):
 
    # position of rightmost set bit of n
    pos = getFirstSetBitPos(n)
 
    # turn off or unset the bit 
    # at position 'pos'
    return (n & ~(1 << (pos - 1)))
     
# Driver code
n = 25
print("Previous small Integer = ",
       previousSmallerInteger(n))
        
# This code is contributed by Anant Agarwal.

C#




// C# implementation to find the previous
// smaller integer with one less number of
// set bits
using System;
 
    class GFG {
         
    // function to find the position of
    // rightmost set bit.
    static int getFirstSetBitPos(int n)
    {
        return (int)(Math.Log(n & -n) /
                           Math.Log(2)) + 1;
    }
      
    // function to find the previous smaller
    // integer
    static int previousSmallerInteger(int n)
    {
         
        // position of rightmost set bit of n
        int pos = getFirstSetBitPos(n);
      
        // turn off or unset the bit at
        // position 'pos'
        return (n & ~(1 << (pos - 1)));
    }
     
    // Driver code
    public static void Main()
    {
        int n = 25;
         
        Console.WriteLine("Previous small Integer ="
                      + previousSmallerInteger(n));
    }
}
 
// This code is contributed by anant321.

PHP




<?php
// PHP implementation to find the previous
// smaller integer with one less number of
// set bits
 
// function to find the position of
// rightmost set bit.
 
function getFirstSetBitPos($n)
{
    return log($n & -$n) + 1;
}
 
// function to find the previous
// smaller integer
 
function previousSmallerInteger($n)
{
    // position of rightmost set bit of n
    $pos = getFirstSetBitPos($n);
 
    // turn off or unset the bit at
    // position 'pos'
    return ($n & ~(1 << ($pos - 1)));
}
 
// Driver Code
$n = 25;
echo "Previous smaller Integer = ", previousSmallerInteger($n);
 
// This code is contributed by Ajit
?>

Javascript




<script>
 
// Javascript implementation to find the previous
// smaller integer with one less number of
// set bits
 
// function to find the position of
// rightmost set bit.
function getFirstSetBitPos(n)
{
    return parseInt(Math.log(n & -n)/Math.log(2)) + 1;
}
 
// function to find the previous smaller
// integer
function previousSmallerInteger(n)
{
    // position of rightmost set bit of n
    var pos = getFirstSetBitPos(n);
 
    // turn off or unset the bit at
    // position 'pos'
    return (n & ~(1 << (pos - 1)));
}
 
// Driver program
var n = 25;
document.write("Previous smaller Integer = " + previousSmallerInteger(n));
     
</script>

Output : 

Previous smaller integer = 24

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :