Given an array of size n, generate and print all possible combinations of r elements in array. For example, if input array is {1, 2, 3, 4} and r is 2, then output should be {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} and {3, 4}.

Following are two methods to do this.

Method 1 (Fix Elements and Recur)
We create a temporary array ‘data[]’ which stores all outputs one by one. The idea is to start from first index (index = 0) in data[], one by one fix elements at this index and recur for remaining indexes. Let the input array be {1, 2, 3, 4, 5} and r be 3. We first fix 1 at index 0 in data[], then recur for remaining indexes, then we fix 2 at index 0 and recur. Finally, we fix 3 and recur for remaining indexes. When number of elements in data[] becomes equal to r (size of a combination), we print data[].

Following diagram shows recursion tree for same input.

Combination java

Following is java implementation of above approach.

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20to%20print%20all%20combination%20of%20size%20r%20in%20an%20array%20of%20size%20n%0Aimport%20java.io.*%3B%0A%20%0Aclass%20Permutation%20%7B%0A%20%0A%20%20%20%20%2F*%20arr%5B%5D%20%20—%3E%20Input%20Array%0A%20%20%20%20data%5B%5D%20—%3E%20Temporary%20array%20to%20store%20current%20combination%0A%20%20%20%20start%20%26%20end%20—%3E%20Staring%20and%20Ending%20indexes%20in%20arr%5B%5D%0A%20%20%20%20index%20%20—%3E%20Current%20index%20in%20data%5B%5D%0A%20%20%20%20r%20—%3E%20Size%20of%20a%20combination%20to%20be%20printed%20*%2F%0A%20%20%20%20static%20void%20combinationUtil(int%20arr%5B%5D%2C%20int%20data%5B%5D%2C%20int%20start%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20end%2C%20int%20index%2C%20int%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Current%20combination%20is%20ready%20to%20be%20printed%2C%20print%20it%0A%20%20%20%20%20%20%20%20if%20(index%20%3D%3D%20r)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20j%3D0%3B%20j%3Cr%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(data%5Bj%5D%2B%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20replace%20index%20with%20all%20possible%20elements.%20The%20condition%0A%20%20%20%20%20%20%20%20%2F%2F%20%22end-i%2B1%20%3E%3D%20r-index%22%20makes%20sure%20that%20including%20one%20element%0A%20%20%20%20%20%20%20%20%2F%2F%20at%20index%20will%20make%20a%20combination%20with%20remaining%20elements%0A%20%20%20%20%20%20%20%20%2F%2F%20at%20remaining%20positions%0A%20%20%20%20%20%20%20%20for%20(int%20i%3Dstart%3B%20i%3C%3Dend%20%26%26%20end-i%2B1%20%3E%3D%20r-index%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20data%5Bindex%5D%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20combinationUtil(arr%2C%20data%2C%20i%2B1%2C%20end%2C%20index%2B1%2C%20r)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20The%20main%20function%20that%20prints%20all%20combinations%20of%20size%20r%0A%20%20%20%20%2F%2F%20in%20arr%5B%5D%20of%20size%20n.%20This%20function%20mainly%20uses%20combinationUtil()%0A%20%20%20%20static%20void%20printCombination(int%20arr%5B%5D%2C%20int%20n%2C%20int%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20A%20temporary%20array%20to%20store%20all%20combination%20one%20by%20one%0A%20%20%20%20%20%20%20%20int%20data%5B%5D%3Dnew%20int%5Br%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Print%20all%20combination%20using%20temprary%20array%20’data%5B%5D’%0A%20%20%20%20%20%20%20%20combinationUtil(arr%2C%20data%2C%200%2C%20n-1%2C%200%2C%20r)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*Driver%20function%20to%20check%20for%20above%20function*%2F%0A%20%20%20%20public%20static%20void%20main%20(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B1%2C%202%2C%203%2C%204%2C%205%7D%3B%0A%20%20%20%20%20%20%20%20int%20r%20%3D%203%3B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20printCombination(arr%2C%20n%2C%20r)%3B%0A%20%20%20%20%7D%0A%7D” message=”Java Program” highlight=”” provider=”manual”/]

Output:

1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

How to handle duplicates?
Note that the above method doesn’t handle duplicates. For example, if input array is {1, 2, 1} and r is 2, then the program prints {1, 2} and {2, 1} as two different combinations. We can avoid duplicates by adding following two additional things to above code.
1) Add code to sort the array before calling combinationUtil() in printCombination()
2) Add following lines at the end of for loop in combinationUtil()

        // Since the elements are sorted, all occurrences of an element
        // must be together
        while (arr[i] == arr[i+1])
             i++;

See this for an implementation that handles duplicates.

[ad type=”banner”]

Method 2 (Include and Exclude every element)
Like the above method, We create a temporary array data[]. The idea here is similar to Subset Sum Problem. We one by one consider every element of input array, and recur for two cases:

1) The element is included in current combination (We put the element in data[] and increment next available index in data[])
2) The element is excluded in current combination (We do not put the element and do not change index)

When number of elements in data[] become equal to r (size of a combination), we print it.

This method is mainly based on Pascal’s Identity, i.e. ncr = n-1cr + n-1cr-1

Following is Java implementation of method 2.

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20to%20print%20all%20combination%20of%20size%20r%20in%20an%20array%20of%20size%20n%0Aimport%20java.io.*%3B%0A%20%0Aclass%20Permutation%20%7B%0A%20%0A%20%20%20%20%2F*%20arr%5B%5D%20%20—%3E%20Input%20Array%0A%20%20%20%20data%5B%5D%20—%3E%20Temporary%20array%20to%20store%20current%20combination%0A%20%20%20%20start%20%26%20end%20—%3E%20Staring%20and%20Ending%20indexes%20in%20arr%5B%5D%0A%20%20%20%20index%20%20—%3E%20Current%20index%20in%20data%5B%5D%0A%20%20%20%20r%20—%3E%20Size%20of%20a%20combination%20to%20be%20printed%20*%2F%0A%20%20%20%20static%20void%20combinationUtil(int%20arr%5B%5D%2C%20int%20n%2C%20int%20r%2C%20int%20index%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20data%5B%5D%2C%20int%20i)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Current%20combination%20is%20ready%20to%20be%20printed%2C%20print%20it%0A%20%20%20%20%20%20%20%20if%20(index%20%3D%3D%20r)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20j%3D0%3B%20j%3Cr%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(data%5Bj%5D%2B%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22%22)%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20When%20no%20more%20elements%20are%20there%20to%20put%20in%20data%5B%5D%0A%20%20%20%20%20%20%20%20if%20(i%20%3E%3D%20n)%0A%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20current%20is%20included%2C%20put%20next%20at%20next%20location%0A%20%20%20%20%20%20%20%20data%5Bindex%5D%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20combinationUtil(arr%2C%20n%2C%20r%2C%20index%2B1%2C%20data%2C%20i%2B1)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20current%20is%20excluded%2C%20replace%20it%20with%20next%20(Note%20that%0A%20%20%20%20%20%20%20%20%2F%2F%20i%2B1%20is%20passed%2C%20but%20index%20is%20not%20changed)%0A%20%20%20%20%20%20%20%20combinationUtil(arr%2C%20n%2C%20r%2C%20index%2C%20data%2C%20i%2B1)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20The%20main%20function%20that%20prints%20all%20combinations%20of%20size%20r%0A%20%20%20%20%2F%2F%20in%20arr%5B%5D%20of%20size%20n.%20This%20function%20mainly%20uses%20combinationUtil()%0A%20%20%20%20static%20void%20printCombination(int%20arr%5B%5D%2C%20int%20n%2C%20int%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20A%20temporary%20array%20to%20store%20all%20combination%20one%20by%20one%0A%20%20%20%20%20%20%20%20int%20data%5B%5D%3Dnew%20int%5Br%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Print%20all%20combination%20using%20temprary%20array%20’data%5B%5D’%0A%20%20%20%20%20%20%20%20combinationUtil(arr%2C%20n%2C%20r%2C%200%2C%20data%2C%200)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*Driver%20function%20to%20check%20for%20above%20function*%2F%0A%20%20%20%20public%20static%20void%20main%20(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B1%2C%202%2C%203%2C%204%2C%205%7D%3B%0A%20%20%20%20%20%20%20%20int%20r%20%3D%203%3B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20printCombination(arr%2C%20n%2C%20r)%3B%0A%20%20%20%20%7D%0A%7D” message=”Java Program” highlight=”” provider=”manual”/]

Output:

1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

How to handle duplicates in method 2?
Like method 1, we can following two things to handle duplicates.
1) Add code to sort the array before calling combinationUtil() in printCombination()
2) Add following lines between two recursive calls of combinationUtil() in combinationUtil()

        // Since the elements are sorted, all occurrences of an element
        // must be together
        while (arr[i] == arr[i+1])
             i++;

See this for an implementation that handles duplicates.

[ad type=”banner”]

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,