U

17. Rectangle Area, Perimeter, and Comparison

Easy

Problem Description

Given the width and height of two rectangles R1R_1 and R2R_2:

  • •Compute the area and perimeter of R1R_1.
  • •Compute the area and perimeter of R2R_2.
  • •Compare the two rectangles by area.

Input Format

  • •Line 1: Two space-separated integers w1w_1 and h1h_1 (dimensions of R1R_1).
  • •Line 2: Two space-separated integers w2w_2 and h2h_2 (dimensions of R2R_2).

Output Format

  • •Line 1: Two space-separated integers: Area and Perimeter of R1R_1.
  • •Line 2: Two space-separated integers: Area and Perimeter of R2R_2.
  • •Line 3: R1 if R1R_1 has strictly greater area, R2 if R2R_2 has strictly greater area, or Tie if their areas are equal.
Example 1:
Input:
4 5
3 6
Output:
20 18
18 18
R1
Explanation: R1: area = 20, perimeter = 18. R2: area = 18, perimeter = 18. R1 area (20) > R2 area (18), so R1 wins.
Example 2:
Input:
2 6
3 4
Output:
12 16
12 14
Tie
Explanation: R1: area = 12. R2: area = 12. Areas are equal, output Tie.
Example 3:
Input:
2 3
5 5
Output:
6 10
25 20
R2
Explanation: R1: area = 6. R2: area = 25. R2 has larger area.
Constraints:
  • 1 <= w1, h1, w2, h2 <= 10^4
  • Time complexity target: O(1)
  • Auxiliary space target: O(1)
Follow up:How would you overload the < and == operators in C++ to compare Rectangle instances naturally?
1,180 Online