Skip to content

Commit

Permalink
print all N choose k combination
Browse files Browse the repository at this point in the history
  • Loading branch information
Atousa committed Sep 5, 2021
1 parent 3679b97 commit f54f650
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Recursion/Combination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//N Choose K Combinations
// Given two integers n and k, find all the possible unique combinations of k numbers in range 1 to n.
// Example One:
// Input: 5, 2
// Output: [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]].

//77 leetCode Combination
public class Combination {

public static List<String> combination(int n, int k) {
char[] slate = new char[k];
List<String> result = new ArrayList<>();
combinationHelper(n, k, 1, slate, 0, result);
return result;
}

private static void combinationHelper(int n, int k, int startNum, char[] slate, int slateCur, List<String> result) {
if (slateCur == k) { //backTracking
result.add(new String(slate,0,k));
return;
}

if (startNum > n) // as we exclude this is a possibility
return;

combinationHelper(n , k, startNum + 1, slate, slateCur, result);
slate[slateCur] = (char)('0'+startNum);
combinationHelper(n, k, startNum + 1, slate, slateCur+1, result);
}

public static void main(String[] args) {
List<String> s = combination(4,2);
System.out.println(s);
}
}



0 comments on commit f54f650

Please sign in to comment.