Count of collisions at a point ( X , Y ) | C ++ 14 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( vector < vector < int > > & D , int N , int X , int Y ) { vector < double > T ; 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 . push_back ( time ) ; } sort ( T . begin ( ) , T . end ( ) ) ; int i = 0 ; int total = 0 ; while ( i < T . size ( ) - 1 ) { int count = 1 ; while ( i < T . size ( ) - 1 and T [ i ] == T [ i + 1 ] ) { count += 1 ; i += 1 ; } total += ( count * ( count - 1 ) ) / 2 ; i += 1 ; } return total ; } int main ( ) { int N = 5 ; vector < vector < int > > D = { { 5 , 12 , 1 } , { 16 , 63 , 5 } , { -10 , 24 , 2 } , { 7 , 24 , 2 } , { -24 , 7 , 2 } } ; int X = 0 , Y = 0 ; cout << ( solve ( D , N , X , Y ) ) ; return 0 ; }