coding question asked in accenture|3 august 2021 10 am slot| Given a set of points in a plane, check weather the points lie on a straight line or not. If the lie on a straight line return the equation else return 0.
Given a set of points in a plane, check weather the points lie on a straight line or not. If the lie on a straight line return the equation else return 0.
Input specification:
Input 1: First Line contains no of points in the plane.
Input 2: Second line contains x,y coordinates of all the points which are divided with spaces.
Output Specification:
Equation of the line (str) if the points lie on a plane or 0(str).
Sample Input:
3
1 1 2 2 3 3
Output:
1x-1y=0
Explanation:
The three points here are [1,1],[2,2] and [3,3]. These lie on a line so it returned the equation.
program:
//code contributed by Harshvardhan_Mamgai
#include<iostream>
#include<vector>
#include <utility>
#include<string>
using namespace std;
string line(
vector<pair<int, int> > &arr, int n)
{
// First pair of point (x0, y0)
int x0 = arr[0].first;
int y0 = arr[0].second;
// Second pair of point (x1, y1)
int x1 = arr[1].first;
int y1 = arr[1].second;
int dx = x1 - x0, dy = y1 - y0;
// Loop to iterate over the points
int count=0;
for (int i = 0; i < n; i++) {
int x = arr[i].first, y = arr[i].second;
count=count+1;
if (dx * (y - y1) != dy * (x - x1)){
string z=to_string(0);
return z;
}
}
count=count-1;
int w=dx*((arr[count].second)-y1);
int o=dy*((arr[count].first)-x1);
string p=to_string(w)+"x-"+to_string(o)+"y=0";
return p;
}
int main()
{
vector<pair<int,int>> vect;
int n;
cin>>n;
for(int i=0;i<3;i++){
int f,s;
cin>>f>>s;
vect.push_back(make_pair(f,s));
}
// Function Call
cout<<line(vect, n);
return 0;
}
//code contributed by Harshvardhan_Mamgai
output