'What is the quickest way to print Binary permutation?
If n=3 and r=2, print below
110
101
011
I'm using below code, But it takes a long time.
public void printBinaryPermutation(int n, int r)
{
for(BigInteger i = new BigInteger("0"); i.compareTo(BigInteger.TWO.pow(n)) == -1; i = i.add(BigInteger.ONE))
{
String str = i.toString(2);
while(str.length() < n)
{
str = "0" + str;
}
BigInteger amount = new BigInteger("0");
for(char c : str.toCharArray())
{
if(c == '1')
{
amount = amount.add(BigInteger.ONE);
}
}
if(amount.compareTo(new BigInteger(String.valueOf(r))) == 0)
{
System.out.println(str);
}
}
}
What is the quickest way to print Binary permutation?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
