Given clock
hands positions for different points of time as pairs A[I][0] and A[I][1] where
the order of the hands does not matter but their angle enclosed, count the
number of pairs of points of time where the angles are the same
public static int[]
getClockHandsDelta(int[][] A) {
int[] angles = new int[A.length];
for (int i = 0; i < A.length; i++){
angles[i] = Math.max(A[i][0],
A[i][1]) - Math.min(A[i][0],A[i][1]);
}
return angles;
}
public static int NChooseK(int n, int k)
{
if (k < 0 || k > n || n == 0)
return 0;
if ( k == 0 || k == n) return 1;
return Factorial(n) / (Factorial(n-k) *
Factorial(k));
}
public static int Factorial(int n) {
if (n <= 1) return 1;
return n * Factorial(n-1);
}
public static int countPairsWithIdenticalAnglesDelta(int[]
angles){
Arrays.sort(angles);
int count = 1;
int result = 0;
for (int i = 1; i < angles.length;
i++) {
if (angles[i] == angles[i-1]) {
count += 1;
} else {
if (count > 0) {
result += NChooseK(count,
2);
}
count = 1;
}
}
if (count > 0) {
result += NChooseK(count, 2);
count = 0;
}
return result;
}
int [][] A = new int[5][2];
A[0][0] = 1; A[0][1] = 2;
A[1][0] = 2; A[1][1] = 4;
A[2][0] = 4; A[2][1] = 3;
A[3][0] = 2; A[3][1] = 3;
A[4][0] = 1; A[4][1] = 3;
1 2 1 1 2
1 1 1 2 2
4
No comments:
Post a Comment