Find subsets of size K in N set
As you probably noticed, the slowdown happens when n is large and k is close to n. If it is slower than it should be, it is likely because it is doing unnecessary work. Lets have a look at the case where n=5 and k=3, and go to the point in the execution of your program where arr[0] = 1 and index = 1. In the for-loop in subset(), you are looking at all possible values from start = 2 to n = 5. However, not all values will actually result in valid solutions. Here is a list of all {1, i} you check, and which subsets are possible:

{1, 2} -&gt; {1, 2, 3} {1, 2, 4} {1, 2, 5}
{1, 3} -&gt; {1, 3, 4} {1, 3, 5}
{1, 4} -&gt; {1, 4, 5}
{1, 5} -&gt; no solutions


As you can see, {1, 5} does not have any possible subsets. Or put otherwise, 5 is too large a number at pos[1] to give any solutions.

Instead of trying all numbers from start to n, try to find out what the actual maximum is that will still give solutions.