Question: So i'm trying to generate all binaries of a size n but with the condition that only k 1s. i.e

for size n = 4, k=2, (there is 2 over 4 combinations)

```
1100
1010
1001
0110
0101
0011

```

I'm stuck and can't figure out how to generate this.

Answer: 
Using the basic recursive method for printing all binary sequence all that remains is to enforce your constraints:

```
    private static void binSeq(int n, int k, String seq) {
    if (n == 0) {
        System.out.println(seq);
        return;
    }

    if (n > k) {
        binSeq(n - 1, k, seq + "0");
    }

    if (k > 0) {
        binSeq(n - 1, k - 1, seq + "1");
    }
}

```
int n = 4, k=2;

```
    for (int i = 0; i < Math.pow(2,n) ; i++) {
        int a = Integer.bitCount(i);
        if (a == k) System.out.println(Integer.toBinaryString(i));
    }

```

I think this is the simplest answer.