Given a matrix arr[][] of size N * 3 such that each row consists of 3 properties defining an element, i.e. (x-coordinate, y-coordinate, speed), and two integers X and Y, the task is to count the number of possible pairs of collisions at the point (X, Y), if every element in the given array is moving in a straight line towards the given point (X, Y) with their respective speeds.Â
Examples:
Input: arr[] = [ [5, 12, 1], [16, 63, 5], [-10, 24, 2], [7, 24, 2], [-24, 7, 2] ], X = 0, Y = 0Â
Output: 4Â
Explanation:Â
Possible collisions are between the following pair of elements: (arr[0], arr[1]), (arr[0], arr[2]), (arr[1], arr[2]) and (arr[3], arr[4])Â
Hence, total collisions are 4.
Input: arr[] = [ [1, 42, 9] ], X = 1, Y = 1Â
Output: 0Â
Explanation:Â
Since a single point is present, there will be no collisions.Â
Hence, total collisions are 0.
Approach: The idea is to simply find the time at which every point will reach the given point (X, Y). If two points reach the origin at the same time, then they will surely collide. Now, to calculate the time taken by each point do the following:
For a given point (x, y) and speed SÂ
Distance D is given by:Â
D = = ? ((x2 – x1)2 + (y2 – y1)2) = ? ((x)2 + (y)2)Â
Time = Distance/Speed, squaring on both sides to eliminate root from distance,Â
Time2 = Distance2/Speed2
After calculating time2 for each point, simply check which of them are same and count the number of collisions.
Below are the steps:
- Traverse the array arr[].
- Create a new array Time[] and for each point, calculate time and append to this array and sort it.
- Traverse Time[] and count the number of elements which arrive the given point at the same time, using the count calculate the number of collisions at that time.
- Finally, return the total count of collisions.
Below is the implementation of the above approach:
C++14
// C++14 program to implement // the above approach #include <bits/stdc++.h> using namespace std; Â
// Function to find the count of // possible pairs of collisions int solve(vector<vector< int >> &D, int N,                            int X, int Y) {          // Stores the time at which     // points reach the origin     vector< double > T; Â
    // Calculate time for each point     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 the times     sort(T.begin(), T.end()); Â
    int i = 0;     int total = 0; Â
    // Counting total collisions     while (i < T.size() - 1)     {                  // Count of elements arriving at         // a given point at the same time         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; } Â
// Driver Code int main() { Â Â Â Â int N = 5; Â
    // Given set of points with speed     vector<vector< int >> D = { { 5, 12, 1 },                               { 16, 63, 5 },                               { -10, 24, 2 },                               { 7, 24, 2 },                               { -24, 7, 2 } }; Â
    int X = 0, Y = 0; Â
    // Function call     cout << (solve(D, N, X, Y)); Â
    return 0; } Â
// This code is contributed by mohit kumar 29 |
Java
// Java program to implement // the above approach import java.util.*; import java.lang.*; Â
class GFG{ Â
// Function to find the count of // possible pairs of collisions static double solve( int [][] D, int N,                         int X, int Y) {          // Stores the time at which     // points reach the origin     ArrayList<Double> T = new ArrayList<>(); Â
    // Calculate time for each point     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);     } Â
    // Sort the times     Collections.sort(T); Â
    int i = 0 ;     int total = 0 ; Â
    // Counting total collisions     while (i < (T.size() - 1 ))     {                  // Count of elements arriving at         // a given point at the same time         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; } Â
// Driver Code public static void main (String[] args) {     int arr[] = { 1 , 2 , 3 , 4 , 5 };     int N = 5 ;          // Given set of points with speed     int [][] D = { { 5 , 12 , 1 },                   { 16 , 63 , 5 },                   { - 10 , 24 , 2 },                   { 7 , 24 , 2 },                   { - 24 , 7 , 2 } };          int X = 0 , Y = 0 ;          // Function call     System.out.println(solve(D, N, X, Y)); } } Â
// This code is contributed by offbeat |
Python3
# Python3 Program to implement # the above approach Â
# Function to find the count of # possible pairs of collisions def solve(D, N, X, Y): Â
    # Stores the time at which     # points reach the origin     T = []              # Calculate time for each point     for i in range (N): Â
        x = D[i][ 0 ]         y = D[i][ 1 ] Â
        speed = D[i][ 2 ] Â
        time = ((x * x - X * X) +                 (y * y - Y * Y)) / (speed * speed)                  Â
        T.append(time)              # Sort the times     T.sort()          i = 0     total = 0          Â
    # Counting total collisions     while i< len (T) - 1 :                  # Count of elements arriving at         # a given point at the same time         count = 1 Â
        while i< len (T) - 1 and T[i] = = T[i + 1 ]:             count + = 1             i + = 1                  total + = (count * (count - 1 )) / 2         i + = 1 Â
    return total Â
# Driver Code Â
N = 5 Â
# Given set of points with speed D = [[ 5 , 12 , 1 ], [ 16 , 63 , 5 ], \ Â Â Â Â [ - 10 , 24 , 2 ], [ 7 , 24 , 2 ], \ Â Â Â Â [ - 24 , 7 , 2 ]] Â
X = 0 Y = 0 Â
# Function Call print (solve(D, N, X, Y)) |
C#
// C# program to implement // the above approach using System; using System.Collections; Â
class GFG{ Â
// Function to find the count of // possible pairs of collisions static double solve( int [, ] D, int N,                         int X, int Y) {          // Stores the time at which     // points reach the origin     ArrayList T = new ArrayList(); Â
    // Calculate time for each point     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);     } Â
    // Sort the times     T.Sort(); Â
    int j = 0;     int total = 0; Â
    // Counting total collisions     while (j < (T.Count - 1))     {                  // Count of elements arriving at         // a given point at the same time         int count = 1;              while ((j < (T.Count - 1)) &&               (Convert.ToDouble(T[j]).CompareTo(                Convert.ToDouble(T[j + 1])) == 0))         {             count += 1;             j += 1;         }         total += (count * (count - 1)) / 2;         j += 1;     }     return total; } Â
// Driver Code public static void Main (String[] args) {     int N = 5;          // Given set of points with speed     int [,] D = new int [,] { { 5, 12, 1 },                               { 16, 63, 5 },                               { -10, 24, 2 },                               { 7, 24, 2 },                               { -24, 7, 2 } };          int X = 0, Y = 0;          // Function call     Console.WriteLine(solve(D, N, X, Y)); } } Â
// This code is contributed by jana_sayantan |
Javascript
<script> Â
// JavaScript program to implement // the above approach Â
// Function to find the count of // possible pairs of collisions function solve(D,N,X,Y) {     // Stores the time at which     // points reach the origin     let T = [];        // Calculate time for each point     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);     }        // Sort the times     T.sort( function (a,b){ return a-b;});        let i = 0;     let total = 0;        // Counting total collisions     while (i < (T.length - 1))     {                    // Count of elements arriving at         // a given point at the same time         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; } Â
// Driver Code let arr=[1, 2, 3, 4, 5]; let N = 5; Â // Given set of points with speed let D = [[5, 12, 1], [16, 63, 5], Â Â Â Â [-10, 24, 2], [7, 24, 2], Â Â Â Â [-24, 7, 2]]; Â
let X = 0, Y = 0; // Function call document.write(solve(D, N, X, Y).toFixed(1)); Â
// This code is contributed by patel2127 Â
</script> |
4.0
Time Complexity: O(N log N)Â
Auxiliary Space: O(N) because using extra space for vector T
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!