Valid Boomerang - Problem

Given an array points where points[i] = [xi, yi] represents a point on the X-Y plane, determine if these three points form a valid boomerang.

A boomerang is a set of three points that are:

  • All distinct (no two points are the same)
  • Not collinear (not in a straight line)

Think of it as checking if three points can form a triangle - if they're all on the same line, you can't make a triangle (or a boomerang shape)!

Goal: Return true if the three points form a valid boomerang, false otherwise.

Example: Points [[1,1], [2,3], [3,2]] form a boomerang because they create a triangle, while [[1,1], [2,2], [3,3]] don't because they're all on the same diagonal line.

Input & Output

example_1.py โ€” Basic Valid Boomerang
$ Input: [[1,1],[2,3],[3,2]]
โ€บ Output: true
๐Ÿ’ก Note: These three points form a triangle (not collinear), so they make a valid boomerang. The cross product calculation: (2-1)*(2-1) - (3-1)*(3-1) = 1*1 - 2*2 = -3 โ‰  0
example_2.py โ€” Collinear Points
$ Input: [[1,1],[2,2],[3,3]]
โ€บ Output: false
๐Ÿ’ก Note: All three points lie on the same diagonal line y=x, so they cannot form a boomerang. The cross product: (2-1)*(3-1) - (2-1)*(3-1) = 1*2 - 1*2 = 0
example_3.py โ€” Duplicate Points
$ Input: [[1,1],[2,2],[1,1]]
โ€บ Output: false
๐Ÿ’ก Note: The first and third points are identical, so we don't have three distinct points. The cross product: (2-1)*(1-1) - (2-1)*(1-1) = 1*0 - 1*0 = 0

Constraints

  • points.length == 3
  • points[i].length == 2
  • -100 โ‰ค xi, yi โ‰ค 100
  • The input will always contain exactly three points

Visualization

Tap to expand
Valid Boomerang DetectionValid Boomerang โœ“P1P2P3Cross Product โ‰  0Collinear Points โœ—P1P2P3Cross Product = 0Cross Product FormulaCross Product = (xโ‚‚ - xโ‚) ร— (yโ‚ƒ - yโ‚) - (yโ‚‚ - yโ‚) ร— (xโ‚ƒ - xโ‚)If result = 0: Points are collinear (Invalid Boomerang)If result โ‰  0: Points form a triangle (Valid Boomerang)
Understanding the Visualization
1
Position the Posts
Place three fence posts at the given coordinates
2
Form Vectors
Create directional arrows from the first post to the other two
3
Calculate Cross Product
Use the cross product formula to measure the 'turning angle'
4
Check Result
If cross product is zero, posts are aligned (straight fence). If non-zero, they form a triangle (valid boomerang)!
Key Takeaway
๐ŸŽฏ Key Insight: The cross product method elegantly determines collinearity without division, making it the most robust solution for checking if three points can form a boomerang!
Asked in
Google 15 Amazon 8 Meta 12 Microsoft 6
23.6K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen