Given two non-negative numbers n and m. The problem is to find the largest number having n number of set bits and m number of unset bits in its binary representation.
Note : 0 bits before leading 1 (or leftmost 1) in binary representation are counted
Constraints: 1 <= n, 0 <= m, (m+n) <= 31
Examples :
Input : n = 2, m = 2
Output : 12
(12)10 = (1100)2
We can see that in the binary representation of 12
there are 2 set and 2 unsets bits and it is the largest number.
Input : n = 4, m = 1
Output : 30
Following are the steps:
- Calculate num = (1 << (n + m)) – 1. This will produce a number num having (n + m) number of bits and all are set.
- Now, toggle the last m bits of num and then return the toggled number. Refer this post.
C++
#include <bits/stdc++.h>
using namespace std;
unsigned int toggleLastMBits(unsigned int n,
unsigned int m)
{
if (m == 0)
return n;
unsigned int num = (1 << m) - 1;
return (n ^ num);
}
unsigned int largeNumWithNSetAndMUnsetBits(unsigned int n,
unsigned int m)
{
unsigned int num = (1 << (n + m)) - 1;
return toggleLastMBits(num, m);
}
int main()
{
unsigned int n = 2, m = 2;
cout << largeNumWithNSetAndMUnsetBits(n, m);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int toggleLastMBits( int n, int m)
{
if (m == 0 )
return n;
int num = ( 1 << m) - 1 ;
return (n ^ num);
}
static int largeNumWithNSetAndMUnsetBits( int n, int m)
{
int num = ( 1 << (n + m)) - 1 ;
return toggleLastMBits(num, m);
}
public static void main (String[] args)
{
int n = 2 , m = 2 ;
System.out.println(largeNumWithNSetAndMUnsetBits(n, m));
}
}
|
Python3
def toggleLastMBits(n,m):
if (m = = 0 ):
return n
num = ( 1 << m) - 1
return (n ^ num)
def largeNumWithNSetAndMUnsetBits(n,m):
num = ( 1 << (n + m)) - 1
return toggleLastMBits(num, m)
n = 2
m = 2
print (largeNumWithNSetAndMUnsetBits(n, m))
|
C#
using System;
class GFG
{
static int toggleLastMBits( int n, int m)
{
if (m == 0)
return n;
int num = (1 << m) - 1;
return (n ^ num);
}
static int largeNumWithNSetAndMUnsetBits( int n, int m)
{
int num = (1 << (n + m)) - 1;
return toggleLastMBits(num, m);
}
public static void Main ()
{
int n = 2, m = 2;
Console.Write(largeNumWithNSetAndMUnsetBits(n, m));
}
}
|
PHP
<?php
function toggleLastMBits( $n , $m )
{
if ( $m == 0)
return $n ;
$num = (1 << $m ) - 1;
return ( $n ^ $num );
}
function largeNumWithNSetAndMUnsetBits( $n ,
$m )
{
$num = (1 << ( $n + $m )) - 1;
return toggleLastMBits( $num , $m );
}
$n = 2; $m = 2;
echo largeNumWithNSetAndMUnsetBits( $n , $m );
?>
|
Javascript
<script>
function toggleLastMBits(n, m)
{
if (m == 0)
return n;
var num = (1 << m) - 1;
return (n ^ num);
}
function largeNumWithNSetAndMUnsetBits(n, m)
{
num = (1 << (n + m)) - 1;
return toggleLastMBits(num, m);
}
var n = 2, m = 2;
document.write( largeNumWithNSetAndMUnsetBits(n, m));
</script>
|
Output :
12
Time Complexity : O(1)
Auxiliary Space: O(1)
For greater values of n and m, you can use long int and long long int datatypes to generate the required number.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!