how to find all subset of k length of an array?

please reply fast

you can use bitwise operators. :slight_smile:

can you explain it because i don,t know about bitwise operator

You can use a recursive algorithm. You can take 2 cases :
1.The element is included in the subset
2.The element is not included in the subset.

You can refer to this stackoverflow answer for further reference and logic

i already seen this link but i m not comfortable in java please provide any c++ link

Just try and understand the logic and code on your own. The logic used is as follows:

The logic used is that the for each iteration the coder performs 2 recursive calls.

First he pushes the number into a stack(the number is a part of the subset) and is in the position say i. He then calls the function recursively to find the remaining i-1 numbers.

Then he pops the number from the answer stack (the number is not a part of the subset) and calls the function recursively again to find the remaining i elements.

Try to code with this logic.Refer to this Quora answer (though not exactly this question this is somewhat similar to this one and is in c++).

If possible I will try to provide a C++ solution later in the day.

Please upvote if you like my answer.

1 Like

it will print subsets of all lengths, so before actually printing u need to check whether the number of “ON” bits is equal to k or not, if equal then only print.In addition to that it is faster then recursive algorithms cause you are operating with bitwise operations, without making any function calls.

thanks @mayank_r_b

Before opening this link :
try it yourself first. Happy coding :slight_smile: