public class BinomialCoefDynProgMemEff implements IBinomialCoef {

	public long C(int n, int k) {

		long[] result1 = new long[n + 1];
		long[] result2 = new long[n + 1];

		result1[0] = 1;
		result1[1] = 1;

		int i, j;

		for (i = 2; i <= n; i++) {
			result2[0] = 1;
			for (j = 1; j < i; j++) {
				result2[j] = result1[j - 1] + result1[j];
			}
			result2[i] = 1;

			long[] auxi = result1;
			result1 = result2;
			result2 = auxi;
		}
		return result1[k];
	}

}
