Problem · Geometry

Do They Belong?

MediumGoldman SachsINTERNFULLTIMEOA

A triangle formed by the three points a(x1, y1), b(x2, y2) and c(x3, y3) is a non-degenerate triangle if the following rules are respected (|ab| is the length of the line between points a and b):

  • |ab| + |bc| > |ac|
  • |bc| + |ac| > |ab|
  • |ab| + |ac| > |bc|

A point belongs to a triangle if it lies somewhere on or inside the triangle. Given two points p = (xp, yp) and q = (xq, yq), return the correct scenario number:

  • 0: If the triangle abc does not form a valid non-degenerate triangle.
  • 1: If point p belongs to the triangle but point q does not.
  • 2: If point q belongs to the triangle but point p does not.
  • 3: If both points p and q belong to the triangle.
  • 4: If neither point p nor point q belong to the triangle.

Function Description

Complete the function pointsBelong in the editor below.

pointsBelong has the following parameter(s):

  • int x1, y1, x2, y2, x3, y3: integer coordinates of the three points that may create a valid triangle
  • int xp, yp, xq, yq: integer coordinates of the two points p and q

Returns

int: an integer value that represents the scenario

Examples
01 · Example 1
x1 = 0
y1 = 0
x2 = 2
y2 = 0
x3 = 4
y3 = 0
xp = 2
yp = 0
xq = 4
yq = 0
return = 0
Example 1 illustration
The lines do not form a valid non-degenerate triangle: The three points a, b, c lie on the same line, so it is impossible to form a triangle. The answer is 0.
Constraints
0 ≤ x1, y1, x2, y2, x3, y3, xp, yp, xq, yq ≤ 2000
More Goldman Sachs problems
drafts saved locally
public int pointsBelong(int x1, int y1, int x2, int y2, int x3, int y3, int xp, int yp, int xq, int yq) {
  // write your code here
}
x10
y10
x22
y20
x34
y30
xp2
yp0
xq4
yq0
expected0
sign in to submit