Easy 0 solved 0% acceptance

Point Quadrant

Find which quadrant a point lies in.

What you'll learn

  • Check the signs of x and y
  • Handle points on the axes
  • Combine two conditions

Problem

The x and y axes split the plane into four quadrants: Q1 (x > 0, y > 0), Q2 (x < 0, y > 0), Q3 (x < 0, y < 0) and Q4 (x > 0, y < 0). Points on an axis belong to no quadrant.

Implement point_quadrant(x, y).

Input

x: The x coordinate. y: The y coordinate.

Output

Return "Q1", "Q2", "Q3" or "Q4"; "X-axis" or "Y-axis" for points on an axis; or "Origin" for (0, 0).

Constraints

-1,000,000 ≤ x, y ≤ 1,000,000

Examples

Example 1
Input:
x = -3
y = 4

Expected output:
expected = "Q2"
Input:
long long x = -3;
long long y = 4;

Expected output:
std::string expected = "Q2";
Input:
long x = -3;
long y = 4;

Expected output:
string expected = "Q2";

Explanation

x is negative and y is positive, so the point is in Q2.

Example 2
Input:
x = 5
y = 0

Expected output:
expected = "X-axis"
Input:
long long x = 5;
long long y = 0;

Expected output:
std::string expected = "X-axis";
Input:
long x = 5;
long y = 0;

Expected output:
string expected = "X-axis";

Explanation

y is 0, so the point lies on the X-axis.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

First determine whether the point lies on an axis or at the origin.

Hint 2

If it is not on an axis, the signs of x and y tell you the quadrant.

Hint 3

The origin is a special case, and axis points are not in any quadrant.

Expected complexity

O(1) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.