Given an array arr[] of N co-ordinate points, the task is to check whether 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.
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++
#include <bits/stdc++.h>
using namespace std;
bool isStraightLinePossible(
vector<pair< int , int > > arr, int n)
{
int x0 = arr[0].first;
int y0 = arr[0].second;
int x1 = arr[1].first;
int y1 = arr[1].second;
int dx = x1 - x0, dy = y1 - y0;
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 ;
}
int main()
{
vector<pair< int , int > > arr =
{ { 0, 0 }, { 1, 1 }, { 3, 3 }, { 2, 2 } };
int n = 4;
isStraightLinePossible(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class pair
{
int first, second;
pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void isStraightLinePossible(
ArrayList<pair> arr, int n)
{
int x0 = arr.get( 0 ).first;
int y0 = arr.get( 0 ).second;
int x1 = arr.get( 1 ).first;
int y1 = arr.get( 1 ).second;
int dx = x1 - x0, dy = y1 - y0;
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" );
}
public static void main(String[] args)
{
ArrayList<pair> 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 ;
isStraightLinePossible(arr, n);
}
}
|
Python3
def isStraightLinePossible(arr, n):
x0 = arr[ 0 ][ 0 ]
y0 = arr[ 0 ][ 1 ]
x1 = arr[ 1 ][ 0 ]
y1 = arr[ 1 ][ 1 ]
dx = x1 - x0
dy = y1 - y0
for i in range (n):
x = arr[i][ 0 ]
y = arr[i][ 1 ]
if (dx * (y - y1) ! = dy * (x - x1)):
print ( "NO" , end = "")
return False
print ( "YES" , end = "")
return True
arr = [ [ 0 , 0 ], [ 1 , 1 ],
[ 3 , 3 ], [ 2 , 2 ] ]
n = 4
isStraightLinePossible(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void isStraightLinePossible(
List<pair> arr, int n)
{
int x0 = arr[0].first;
int y0 = arr[0].second;
int x1 = arr[1].first;
int y1 = arr[1].second;
int dx = x1 - x0, dy = y1 - y0;
for ( int i = 0; i < n; i++)
{
int x = arr[i].first;
int y = arr[i].second;
if (dx * (y - y1) != dy * (x - x1))
{
Console.WriteLine( "NO" );
}
}
Console.WriteLine( "YES" );
}
public static void Main(String[] args)
{
List<pair> arr = new List<pair>();
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;
isStraightLinePossible(arr, n);
}
}
|
Javascript
<script>
function isStraightLinePossible( arr, n)
{
var x0 = arr[0][0];
var y0 = arr[0][1];
var x1 = arr[1][0];
var y1 = arr[1][1];
var dx = x1 - x0;
var dy = y1 - y0;
for ( var i = 0; i < n; i++) {
var x = arr[i][0], y = arr[i][1];
if (dx * (y - y1) != dy * (x - x1)){
document.write( "NO" );
return false ;
}
}
document.write( "YES" );
return true ;
}
var arr = [[ 0, 0 ], [ 1, 1 ], [ 3, 3 ], [ 2, 2 ]];
var n = 4;
isStraightLinePossible(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)