Skip to content

Instantly share code, notes, and snippets.

@Bekt
Created September 25, 2013 05:10
Show Gist options
  • Save Bekt/6695384 to your computer and use it in GitHub Desktop.
Save Bekt/6695384 to your computer and use it in GitHub Desktop.
// Twitter Codibility Screening
public class Two {
public int solution(int[] A) {
int evens = getEvensCount(A);
long totalPairs = nChooseK(evens, 2) + nChooseK(A.length - evens, 2);
return totalPairs > 1000000000 ? -1 : (int) totalPairs;
}
private int getEvensCount(int[] A) {
int count = 0;
for (int i = 0; i < A.length; i++) {
count += (A[i] % 2) == 0 ? 1 : 0;
}
return count;
}
private long nChooseK(long n, long k) {
return k == 0 ? 1: ((n * nChooseK(n - 1, k - 1)) / k);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment