Below is a recursive function to calculate the value of Binomial Cofecient 'C' i.e. Combination ! I wish to understand this code's Time and Space complexity in terms of N and K (Assuming that We are calculating NCK).
public class ValueOfBinomialCofecientC {
static int globalhitsToThisMethod = 0;
public static void main(String[] args) {
// Calculate nCk.
int n = 61, k = 55;
long beginTime = System.nanoTime();
int ans = calculateCombinationVal(n, k);
long endTime = System.nanoTime() - beginTime;
System.out.println("Hits Made are : " +globalhitsToThisMethod + " -- Result Is : " + ans + " ANd Time taken is:" + (endTime-beginTime));
}
private static int calculateCombinationVal(int n, int k) {
globalhitsToThisMethod++;
if(k == 0 || k == n){
return 1;
} else if(k == 1){
return n;
} else {
int res = calculateCombinationVal(n-1, k-1) + calculateCombinationVal(n-1, k);
return res;
}
}
}