Algorithm


Problem Name: 1037. Valid Boomerang

Given an array points where points[i] = [xi, yi] represents a point on the X-Y plane, return true if these points are a boomerang.

A boomerang is a set of three points that are all distinct and not in a straight line.

 

Example 1:

Input: points = [[1,1],[2,3],[3,2]]
Output: true

Example 2:

Input: points = [[1,1],[2,2],[3,3]]
Output: false

 

Constraints:

  • points.length == 3
  • points[i].length == 2
  • 0 <= xi, yi <= 100

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
    public boolean isBoomerang(int[][] p) {
        return (p[0][0] - p[1][0]) * (p[0][1] - p[2][1]) != (p[0][0] - p[2][0]) * (p[0][1] - p[1][1]);
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
points = [[1,1],[2,3],[3,2]]

Output

x
+
cmd
true

#2 Code Example with Javascript Programming

Code - Javascript Programming


const isBoomerang = function(points) {
  if(angle(points[0], points[1], points[2]) &&
     angle(points[1], points[2], points[0]) &&
     angle(points[1], points[0], points[2]) ) return false
  return true
};

// distinct or in a line
function angle(p1, p2, p3) {
  if((p1[0] === p2[0] && p1[1] === p2[1]) ||
     (p2[0] === p3[0] && p2[1] === p3[1]) ||
     (p1[0] === p3[0] && p1[1] === p3[1]) ) return true
  
  return collinear(p1[0], p1[1], p2[0], p2[1], p3[0], p3[1])
}
function collinear(x1, y1, x2, y2,  x3, y3)  { 
  return (y3 - y2) * (x2 - x1) === (y2 - y1) * (x3 - x2)
} 
Copy The Code & Try With Live Editor

Input

x
+
cmd
points = [[1,1],[2,3],[3,2]]

Output

x
+
cmd
true

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def isBoomerang(self, p: List[List[int]]) -> bool:
        return (p[0][0] - p[1][0]) * (p[0][1] - p[2][1]) != (p[0][0] - p[2][0]) * (p[0][1] - p[1][1])
Copy The Code & Try With Live Editor

Input

x
+
cmd
points = [[1,1],[2,2],[3,3]]

Output

x
+
cmd
false

#4 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _1037_ValidBoomerang
    {
        public bool IsBoomerang(int[][] points)
        {
            double x1 = points[0][0] - points[1][0], y1 = points[0][1] - points[1][1];
            double x2 = points[0][0] - points[2][0], y2 = points[0][1] - points[2][1];

            if ((x1 == 0 && y1 == 0) || (x2 == 0 && y2 == 0)) return false;
            if (y1 == 0) return y2 != 0;

            return (x1 / y1) != (x2 / y2);
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
points = [[1,1],[2,2],[3,3]]

Output

x
+
cmd
false
Advertisements

Demonstration


Previous
#1036 Leetcode Escape a Large Maze Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1038 Leetcode Binary Search Tree to Greater Sum Tree Solution in C, C++, Java, JavaScript, Python, C# Leetcode