Count of collisions at a point ( X , Y ) | Java program to implement the above approach ; Function to find the count of possible pairs of collisions ; Stores the time at which points reach the origin ; Calculate time for each point ; Sort the times ; Counting total collisions ; Count of elements arriving at a given point at the same time ; Driver Code ; Given set of points with speed ; Function call
import java . util . * ; import java . lang . * ; class GFG { static double solve ( int [ ] [ ] D , int N , int X , int Y ) { ArrayList < Double > T = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int x = D [ i ] [ 0 ] ; int y = D [ i ] [ 1 ] ; double speed = D [ i ] [ 2 ] ; double time = ( ( x * x - X * X ) + ( y * y - Y * Y ) ) / ( speed * speed ) ; T . add ( time ) ; } Collections . sort ( T ) ; int i = 0 ; int total = 0 ; while ( i < ( T . size ( ) - 1 ) ) { int count = 1 ; while ( ( i < ( T . size ( ) - 1 ) ) && ( Double . compare ( T . get ( i ) , T . get ( i + 1 ) ) == 0 ) ) { count += 1 ; i += 1 ; } total += ( count * ( count - 1 ) ) / 2 ; i += 1 ; } return total ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = 5 ; int [ ] [ ] D = { { 5 , 12 , 1 } , { 16 , 63 , 5 } , { - 10 , 24 , 2 } , { 7 , 24 , 2 } , { - 24 , 7 , 2 } } ; int X = 0 , Y = 0 ; System . out . println ( solve ( D , N , X , Y ) ) ; } }