Skip to content

Instantly share code, notes, and snippets.

@goakley
Created April 27, 2013 20:50
Show Gist options
  • Save goakley/5474669 to your computer and use it in GitHub Desktop.
Save goakley/5474669 to your computer and use it in GitHub Desktop.
Java-based nCr on an array for @AlexaCain and @lhsu92 - Gets all `r` combinations on an array of ints
class NCR {
// Take the factorial of an integer
public static int factorial(int num)
{
int fact = 1;
for (int i = 1; i <= num; i++)
fact = fact*i;
return fact;
}
// Get the nCr
public static int nCr(int n, int r) {
return factorial(n)/(factorial(n-r)*factorial(r));
}
// Get the combinations of the array
public static int[][] nCrArray(int[] array, int r) {
int n = array.length;
// Choose r from the length of the array
int ncr = nCr(n,r);
int[][] result = new int[ncr][r];
int result_index = 0;
// BASE CASE
if (r == 1) {
// Each value in the array is a result
for (; result_index < array.length; result_index++)
result[result_index][0] = array[result_index];
}
// All other cases
else {
// Iterate through each of the starting values
for (int i = 0; i < array.length-r+1; i++) {
// Create the sub-array
int[] recursivearray = new int[n-i-1];
// Copy into the sub-array all values after the active value
System.arraycopy(array, i+1, recursivearray, 0, n-i-1);
// Calculate the sub-combinations (recurse)
int[][] subarrays = nCrArray(recursivearray, r-1);
// Create the results for this active value
for (int j = 0; j < subarrays.length; j++) {
// Augment the active value and sub-combinations
result[result_index][0] = array[i];
System.arraycopy(subarrays[j], 0,
result[result_index], 1,
subarrays[j].length);
result_index++;
}
}
}
return result;
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7};
int r = 4;
int[][] answer = nCrArray(array, r);
for (int i = 0; i < answer.length; i++) {
for (int j = 0; j < answer[i].length-1; j++) {
System.out.print(answer[i][j] + ",");
}
System.out.println(answer[i][answer[i].length-1]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment