Skip to main content

Valid Boomerang - Solution & Explanation

EasyArrayMathGeometry16 min readAsked at: Microsoft, Google
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: You are given three points on a 2D plane. A boomerang is valid only if the points are all distinct and they do not lie on the same straight line. The task is to determine whether these three coordinates form a non‑collinear triangle.

The core observation: three points form a straight line if their slopes match or if the area of the triangle formed by them equals zero. Both approaches rely on basic geometry and simple math operations on coordinates stored in an array.

Approach 1: Using Slope to Determine Collinearity (Time: O(1), Space: O(1))

Two line segments share the same slope when three points are collinear. Compute the slope between point A→B and A→C and compare them. Instead of dividing (which risks floating‑point errors or division by zero), use cross multiplication: (y2 - y1) * (x3 - x1) == (y3 - y1) * (x2 - x1). If this equality holds, the slopes are identical and the points lie on the same line. Otherwise, they form a valid boomerang.

This method relies purely on integer arithmetic, which avoids precision problems and keeps the implementation clean. Since the input always contains exactly three points, you perform only a constant number of operations. That makes the runtime O(1) and space O(1). This is usually the most straightforward way to solve the problem.

Approach 2: Using Area of Triangle Formed by Points for Collinearity (Time: O(1), Space: O(1))

Three points are collinear when the triangle they form has zero area. Using the determinant formula for the area of a triangle with coordinates:

Area = x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2)

If the computed value equals zero, the points lie on a straight line and the boomerang is invalid. Any non‑zero value means the points form a triangle, which satisfies the boomerang condition. This technique is mathematically equivalent to the slope method but framed through geometric area.

The computation uses only a few arithmetic operations and no additional memory, giving the same O(1) time and O(1) space complexity. Some engineers prefer this approach because the determinant formula is widely used in computational geometry.

Recommended for interviews: The slope comparison using cross multiplication is typically expected. It directly expresses the definition of collinearity and avoids floating‑point division. The triangle area formula demonstrates deeper geometry intuition, but both solutions run in constant time. Showing either approach signals strong fundamentals with coordinate geometry and basic mathematical reasoning.

Approach 1: Using Slope to Determine Collinearity

To determine if the three points form a boomerang, we first check if any two points are the same, which would immediately disqualify them as a boomerang since all points must be distinct. Then, we check that the slope between any two pairs of points is different, which ensures they are not collinear. If the slopes between (points[0], points[1]) and (points[0], points[2]) are different, the points are not on a straight line and hence form a boomerang.

This solution first checks if all points are distinct and ensures the slopes between the points are not equal to determine if the points are not collinear. The slopes are compared using cross multiplication to avoid division.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) because each operation takes constant time.
Space Complexity: O(1) since no additional space outside of input is used.

Try this approach in the editor →

Approach 2: Using Area of Triangle Formed by Points for Collinearity

The area of a triangle can be used to determine collinearity of three points. If the area is zero, the points are collinear. The formula for the area of the triangle formed by points A(x1,y1), B(x2,y2), and C(x3,y3) is 0.5 * |x1(y2-y3) + x2(y3-y1) + x3(y1-y2)|. If the area derived from this formula is zero, the points are on a straight line. If the area is non-zero, the points form a triangle, suggesting a valid boomerang.

This implementation calculates the area using a cross-product-based approach. If the computed area is not zero, the points form a valid triangle.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) because each operation takes constant time.
Space Complexity: O(1) since no additional space outside of input is used.

Try this approach in the editor →

Approach 3: Slope Comparison

Let the three points be (x_1, y_1), (x_2, y_2), and (x_3, y_3). The formula for calculating the slope between two points is \frac{y_2 - y_1}{x_2 - x_1}.

To ensure that the three points are not collinear, the condition \frac{y_2 - y_1}{x_2 - x_1} neq \frac{y_3 - y_2}{x_3 - x_2} must be satisfied. By transforming the equation, we get (y_2 - y_1) cdot (x_3 - x_2) neq (y_3 - y_2) cdot (x_2 - x_1).

Note:

  1. When the slope between two points does not exist, i.e., x_1 = x_2, the transformed equation still holds.
  2. If there are precision issues with division in slope comparison, it can be converted to multiplication.

Time complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using Slope to Determine Collinearity

Time Complexity: O(1) because each operation takes constant time.
Space Complexity: O(1) since no additional space outside of input is used.

Using Area of Triangle Formed by Points for Collinearity

Time Complexity: O(1) because each operation takes constant time.
Space Complexity: O(1) since no additional space outside of input is used.

Slope Comparison

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Slope Comparison (Cross Multiplication)O(1)O(1)General case; simplest implementation without floating point division
Triangle Area / Determinant MethodO(1)O(1)When applying computational geometry formulas or determinant logic

Video Solution

1037. Valid Boomerang | Interview Preparation | Tamil | code iocode io - Tamil2,833 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Valid Boomerang easy or hard?
Valid Boomerang is classified as an Easy problem on LeetCode. The task requires recognizing that three points are collinear when their slopes match or the triangle area is zero. Once that observation is made, the implementation is straightforward.
Valid Boomerang Python/Java solution
In Python or Java, compute the cross multiplication condition: (y2 - y1) * (x3 - x1) != (y3 - y1) * (x2 - x1). If the expression is true, the points form a valid boomerang. The implementation is only a few lines and runs in constant time.
How to solve Valid Boomerang in O(n)?
An O(n) solution is unnecessary because the problem always has three points. The optimal approach directly compares slopes or computes the triangle area using constant arithmetic operations. Both methods achieve O(1) time and O(1) space complexity.
What is the best approach for Valid Boomerang?
The slope comparison approach using cross multiplication is the most common solution. It checks whether (y2 − y1) * (x3 − x1) equals (y3 − y1) * (x2 − x1). If the values differ, the points are not collinear and form a valid boomerang. The method runs in O(1) time and O(1) space.
Is Valid Boomerang asked at Google/Amazon/Meta?
Valid Boomerang represents a basic geometry and math reasoning problem sometimes used in screening rounds or coding practice sets. Similar collinearity or coordinate geometry questions appear in interviews at companies like Google, Amazon, and Meta, especially for entry‑level roles.
What data structure is used in Valid Boomerang?
The input is typically stored in a small array containing three coordinate pairs. The solution mainly relies on arithmetic operations and geometric reasoning rather than complex data structures.
What is the time complexity of Valid Boomerang?
Valid Boomerang runs in O(1) time because the input always contains exactly three points. The algorithm performs only a few arithmetic operations to compare slopes or compute triangle area. Space complexity is also O(1) since no additional data structures are required.

Ready to solve this problem?

Practice Valid Boomerang with our built-in code editor and test cases.

Practice on FleetCode