Skip to main content

Command Palette

Search for a command to run...

Check If It Is a Straight Line

Published
1 min read
Check If It Is a Straight Line
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem - 1232

class Solution {
    public boolean checkStraightLine(int[][] coordinates) {
        // Extract the x and y coordinates of the first two points
        int x1 = coordinates[0][0], x2 = coordinates[1][0];
        int y1 = coordinates[0][1], y2 = coordinates[1][1];

        // Calculate the difference in x and y coordinates between the first two points
        int dx = x2 - x1;
        int dy = y2 - y1;

        // Loop through the remaining points starting from the third point
        for(int i = 2; i < coordinates.length; i++) {
            int x = coordinates[i][0]; // Current point's x-coordinate
            int y = coordinates[i][1]; // Current point's y-coordinate

            // Check if the current point satisfies the line equation (cross multiplication form)
            // to avoid division and handle slope as dy/dx. If it doesn't, return false.
            if(dy * (x - x1) != dx * (y - y1)) {
                return false; // Points are not in a straight line
            }
        }
        return true; // All points are in a straight line
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.