#### Problem Description

On a busy road, multiple cars are passing by. A simulation is run to see what happens if brakes fail for all cars on the road. The only way for them to be safe is if they don’t collide and pass by each other. The goal is to identify whether any of the given cars would collide or pass by each other safely around a Roundabout. Think of this as a reference point O ( Origin with coordinates (0, 0) ), but instead of going around it, cars pass through it.

Considering that each car is moving in a straight line towards the origin with individual uniform speed. Cars will continue to travel in that same straight line even after crossing origin. Calculate the number of collisions that will happen in such a scenario.

**Note:** Calculate collisions only at origin. Ignore the other collisions. Assume that each car continues on its respective path even after the collision without change of direction or speed for an infinite distance.

Given an array **car[]** which contains the co-ordinates and their speed for each element. Find the total number of collisions at origin.

**Example:**

Input:car[] ={(5 12 1), (16 63 5), (-10 24 2), (7 24 2), (-24 7 2)}Output:4Explanation:

Let the 5 cars be A, B, C, D, and E respectively.

4 Collisions are as follows –

A & B, A & C, B & C, D & E

**Approach: **The idea is to find the number of cars colliding at origin. We are given coordinate position of cars, from which we can find the distance of cars from origin.

Let x, y be position of a car then distance from origin will be:

Dividing this distance with speed will give us time at which a car is present at origin. So if there are ‘N’ cars present at origin at some instant of time then total collisions will be

Adding all collision at a different instance of time will give our required answer.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// collision at the origin ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Structure to store the ` `// co-ordinates of car and speed ` `struct` `Car { ` ` ` `long` `long` `x; ` ` ` `long` `long` `y; ` ` ` `long` `long` `v; ` `}; ` ` ` `// Function to find the co-ordinates ` `// of the car and speed ` `long` `long` `solve(` `long` `long` `c, ` ` ` `vector<Car>& arr) ` `{ ` ` ` `map<` `long` `long` `, ` `long` `long` `> freq; ` ` ` ` ` `long` `sum = 0; ` ` ` `for` `(` `long` `long` `i = 0; i < c; i++) { ` ` ` `long` `long` `x = arr[i].x, ` ` ` `y = arr[i].y, ` ` ` `v = arr[i].v; ` ` ` `long` `long` `dist_square ` ` ` `= (x * x) + (y * y); ` ` ` `long` `long` `time_square ` ` ` `= dist_square / (v * v); ` ` ` `freq[time_square]++; ` ` ` `} ` ` ` ` ` `// Loop to iterate over the ` ` ` `// frequency of the elements ` ` ` `for` `(` `auto` `it = freq.begin(); ` ` ` `it != freq.end(); it++) { ` ` ` `long` `long` `f = it->second; ` ` ` `if` `(f <= 0) ` ` ` `continue` `; ` ` ` ` ` `sum += (f * (f - 1)) / 2; ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `long` `long` `c = 5; ` ` ` ` ` `vector<Car> arr; ` ` ` ` ` `Car tmp; ` ` ` `tmp.x = 5; ` ` ` `tmp.y = 12; ` ` ` `tmp.v = 1; ` ` ` `arr.push_back(tmp); ` ` ` ` ` `tmp.x = 16; ` ` ` `tmp.y = 63; ` ` ` `tmp.v = 5; ` ` ` `arr.push_back(tmp); ` ` ` ` ` `tmp.x = -10; ` ` ` `tmp.y = 24; ` ` ` `tmp.v = 2; ` ` ` `arr.push_back(tmp); ` ` ` ` ` `tmp.x = 7; ` ` ` `tmp.y = 24; ` ` ` `tmp.v = 2; ` ` ` `arr.push_back(tmp); ` ` ` ` ` `tmp.x = -24; ` ` ` `tmp.y = 7; ` ` ` `tmp.v = 2; ` ` ` `arr.push_back(tmp); ` ` ` ` ` `cout << solve(c, arr); ` ` ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

**Output:**

4

## Recommended Posts:

- Prime Fibonnaci | TCS Mockvita 2020
- Philaland Coin | TCS Mockvita 2020
- Television Sets | TCS MockVita 2020
- TCS Interview experience through TCS Ninja
- TCS NQT Coding Questions & How Coding Task Evaluated in TCS NQT
- TCS Ninja Interview Experience CSE 2020
- TCS NQT (2019-2020) Interview Experience
- TCS DIGITAL Interview Experience (Through NQT for 2020 passing outs)
- TCS NQT Interview Experience | 2020 Outgoing batch
- TCS NQT 2020 Interview Experience
- TCS Ninja National Qualifier Test 2020 Interview Experience ECE
- TCS Ninja Interview Experience | ECE 2020
- How I cracked TCS BCA/BSc Hiring Drive for 2020 ?
- TCS Interview Experience( Off Campus-2020)
- TCS Ninja Interview Experience (2020 batch)
- TCS Interview Experience 2020 | Digital profile
- TCS Codevita Interview Experience 2020
- TCS CodeVita 2020 - Important Dates, Eligibility, How to Apply?
- TCS Codevita Interview Experience | Season 9 2020
- TCS Codevita Interview Experience (September - 2020)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.