Count of collisions at a point (X, Y)
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
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
def solve(D, N, X, Y):
T = []
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)
T.sort()
i = 0
total = 0
while i< len (T) - 1 :
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
N = 5
D = [[ 5 , 12 , 1 ], [ 16 , 63 , 5 ], \
[ - 10 , 24 , 2 ], [ 7 , 24 , 2 ], \
[ - 24 , 7 , 2 ]]
X = 0
Y = 0
print (solve(D, N, X, Y))
|
C#
using System;
using System.Collections;
class GFG{
static double solve( int [, ] D, int N,
int X, int Y)
{
ArrayList 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);
}
T.Sort();
int j = 0;
int total = 0;
while (j < (T.Count - 1))
{
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;
}
public static void Main (String[] args)
{
int N = 5;
int [,] D = new int [,] { { 5, 12, 1 },
{ 16, 63, 5 },
{ -10, 24, 2 },
{ 7, 24, 2 },
{ -24, 7, 2 } };
int X = 0, Y = 0;
Console.WriteLine(solve(D, N, X, Y));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(N) because using extra space for vector T
Last Updated :
30 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...