Created
September 25, 2013 05:10
-
-
Save Bekt/6695384 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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