Count of collisions at a point ( X , Y ) | 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
function solve ( D , N , X , Y ) { let T = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let x = D [ i ] [ 0 ] ; let y = D [ i ] [ 1 ] ; let speed = D [ i ] [ 2 ] ; let time = ( ( x * x - X * X ) + ( y * y - Y * Y ) ) / ( speed * speed ) ; T . push ( time ) ; } T . sort ( function ( a , b ) { return a - b ; } ) ; let i = 0 ; let total = 0 ; while ( i < ( T . length - 1 ) ) { let count = 1 ; while ( ( i < ( T . length - 1 ) ) && ( T [ i ] == T [ i + 1 ] ) ) { count += 1 ; i += 1 ; } total += ( count * ( count - 1 ) ) / 2 ; i += 1 ; } return total ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = 5 ; let D = [ [ 5 , 12 , 1 ] , [ 16 , 63 , 5 ] , [ - 10 , 24 , 2 ] , [ 7 , 24 , 2 ] , [ - 24 , 7 , 2 ] ] ; let X = 0 , Y = 0 ; document . write ( solve ( D , N , X , Y ) . toFixed ( 1 ) ) ;