You are given an array
coordinates
, coordinates[i] = [x, y]
, where [x, y]
represents the coordinate of a point. Check if these points make a straight line in the XY plane.Example 1:
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Output: true
Approach
Java
public class CheckIfStraightLine {public static void main(String[] args) {int[][] coordinates = { { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 },{ 5, 6 }, { 6, 7 } };System.out.println(checkStraightLine(coordinates));}// function to check if all points// form a single linestatic boolean checkStraightLine(int[][] coordinates) {int n = coordinates.length;float slop = 0, c=0;// find the slopeif ((coordinates[n - 1][0] - coordinates[0][0]) != 0)slop = (float) (coordinates[n - 1][1] - coordinates[0][1]) /(coordinates[n - 1][0] - coordinates[0][0]);c = coordinates[0][1] - coordinates[0][0] * slop;int flag = 0;for (int i = 0; i < n - 1; i++)if (coordinates[i][0] != coordinates[i + 1][0]) {flag = 1;break;}if (flag == 0)return true;for (int i = 1; i < coordinates.length - 1; i++) {float a = coordinates[i][1] - coordinates[0][1];float b = coordinates[i][0] - coordinates[0][0];if ((float) (a / b) != slop)return false;}return true;}}
C++
#include <bits/stdc++.h>using namespace std;//function to check if all points//form a single linebool checkStraightLine(vector<vector<int>> &coordinates){int n = coordinates.size();float slop = 0, c;//find the slopeif ((coordinates[n - 1][0] - coordinates[0][0]) != 0)slop = (float)(coordinates[n - 1][1] - coordinates[0][1]) /(coordinates[n - 1][0] - coordinates[0][0]);c = coordinates[0][1] - coordinates[0][0] * slop;int flag = 0;for (int i = 0; i < n - 1; i++)if (coordinates[i][0] != coordinates[i + 1][0]){flag = 1;break;}if (flag == 0)return true;for (int i = 1; i < coordinates.size() - 1; i++){float a = coordinates[i][1] - coordinates[0][1];float b = coordinates[i][0] - coordinates[0][0];if ((float)(a / b) != slop)return false;}return true;}int main(){vector<vector<int>> coordinates = {{1, 2}, {2, 3}, {3, 4}, {4, 5},{5, 6}, {6, 7}};if (checkStraightLine(coordinates))cout << "true";elsecout << "false";return 0;}
No comments:
Post a Comment