Valid Boomerang LeetCode Solution - The Coding Shala

Home >> LeetCode >> Valid Boomerang

 In this post, we will learn how to solve LeetCode's Valid Boomerang problem and will implement its solution in Java.

Valid Boomerang Problem

A boomerang is a set of 3 points that are all distinct and not in a straight line. Given a list of three points in the plane, return whether these points are a boomerang.

Example 1:
Input: [[1,1],[2,3],[3,2]]
Output: true

Example 2:
Input: [[1,1],[2,2],[3,3]]
Output: false

Practice this problem on LeetCode(Click Here).

Valid Boomerang Java Solution

Approach 1

We can find the slope of the first two points and the last two points if it's the same then it will be a straight line.

Solpe is (y2-y1)/(x2-x1) == (y3-y2)/(x3-x2)  or in other words (y2-y1)*(x3-x2) == (y3-y2)*(x2-x1).

We can also find the area of the triangle that is made by these three points. The area will be zero of the straight line.

Java Program: 

class Solution {
    public boolean isBoomerang(int[][] points) {
        //find slope, if same then straight line
        // (y2-y1)/(x2-x1) == (y3-y2)/(x3-x2)
        //or (y2-y1)*(x3-x2) == (y3-y2)*(x2-x1)
        int x1 = points[0][0];
        int y1 = points[0][1];
        int x2 = points[1][0];
        int y2 = points[1][1];
        int x3 = points[2][0];
        int y3 = points[2][1];
        if((y2-y1)*(x3-x2) == (y3-y2)*(x2-x1)) return false;
        return true;

Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.


Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Single Number 3 LeetCode Solution - The Coding Shala

LeetCode - Number of Good Pairs Solution - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Java Method Overloading - The Coding Shala