# Check whether a straight line can be formed using N co-ordinate points

Given an array arr[] of N co-ordinate points, the task is to check wheather a straight line can be formed using these co-ordinate points.

Input: arr[] = {{0, 0}, {1, 1}, {2, 2}}
Output: Yes
Explanation:
Slope of every two points is same. That is 1.
Therefore, a straight line can be formed using these points.

Input: arr[] = {{0, 1}, {2, 0}}
Output: Yes
Explanation:
Two points in co-ordinate system always forms a straight line.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to find the slope of line between every pair of points in the array and if the slope of every pair of point is same, then these points together forms a straight line.

```// Slope of line formed by
// two points (y2, y1), (x2, x1)

Slope of Line = y2 - y1
---------
x2 - x1
```

Below is the implementation of the above approach:

## C++

 `// C++ implementation to check  ` `// if a straight line  ` `// can be formed using N points  ` ` `  `#include   ` ` `  `using` `namespace` `std;  ` ` `  `// Function to check if a straight line  ` `// can be formed using N points  ` `bool` `isStraightLinePossible(  ` `    ``vector > arr, ``int` `n)  ` `{  ` `    ``// First pair of point (x0, y0)  ` `    ``int` `x0 = arr.first;  ` `    ``int` `y0 = arr.second;  ` ` `  `    ``// Second pair of point (x1, y1)  ` `    ``int` `x1 = arr.first;  ` `    ``int` `y1 = arr.second;  ` ` `  `    ``int` `dx = x1 - x0, dy = y1 - y0;  ` `     `  `    ``// Loop to iterate over the points  ` `    ``for` `(``int` `i = 0; i < n; i++) {  ` `        ``int` `x = arr[i].first, y = arr[i].second;  ` `        ``if` `(dx * (y - y1) != dy * (x - x1)){  ` `            ``cout << ``"NO"``;  ` `            ``return` `false``;  ` `        ``}  ` `    ``}  ` `    ``cout << ``"YES"``;  ` `    ``return` `true``;  ` `}  ` ` `  `// Driver Code  ` `int` `main()  ` `{  ` `    ``// Array of points  ` `    ``vector > arr =  ` `    ``{ { 0, 0 }, { 1, 1 }, { 3, 3 }, { 2, 2 } };  ` `    ``int` `n = 4;  ` `     `  `    ``// Function Call  ` `    ``isStraightLinePossible(arr, n);  ` `    ``return` `0;  ` `}  `

## Java

 `// Java implementation to check ` `// if a straight line can be  ` `// formed using N points ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `static` `class` `pair ` `{ ` `    ``int` `first, second; ` `    ``pair(``int` `first, ``int` `second) ` `    ``{ ` `        ``this``.first = first; ` `        ``this``.second = second; ` `    ``} ` `} ` ` `  `// Function to check if a straight line ` `// can be formed using N points ` `static` `void` `isStraightLinePossible( ` `       ``ArrayList arr, ``int` `n) ` `{ ` `     `  `    ``// First pair of point (x0, y0) ` `    ``int` `x0 = arr.get(``0``).first; ` `    ``int` `y0 = arr.get(``0``).second; ` ` `  `    ``// Second pair of point (x1, y1) ` `    ``int` `x1 = arr.get(``1``).first; ` `    ``int` `y1 = arr.get(``1``).second; ` ` `  `    ``int` `dx = x1 - x0, dy = y1 - y0; ` ` `  `    ``// Loop to iterate over the points ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `        ``int` `x = arr.get(i).first; ` `        ``int` `y = arr.get(i).second; ` `        ``if` `(dx * (y - y1) != dy * (x - x1)) ` `        ``{ ` `            ``System.out.println(``"NO"``); ` `        ``} ` `    ``} ` `    ``System.out.println(``"YES"``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `     `  `    ``// Array of points ` `    ``ArrayList arr = ``new` `ArrayList<>(); ` `    ``arr.add(``new` `pair(``0``, ``0``)); ` `    ``arr.add(``new` `pair(``1``, ``1``)); ` `    ``arr.add(``new` `pair(``3``, ``3``)); ` `    ``arr.add(``new` `pair(``2``, ``2``)); ` ` `  `    ``int` `n = ``4``; ` ` `  `    ``// Function Call ` `    ``isStraightLinePossible(arr, n); ` `} ` `} ` ` `  `// This code is contributed by offbeat `

Output:

```YES
```

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : offbeat