Rectangle with Maximum Area using Java Pair
Given an array of pairs containing length and breadth of rectangles. The task is to find the maximum area of the rectangle.
Examples:
Input: (1, 2), (3, 5), (1, 1), (4, 2) Output: 15 Input: (3, 5), (5, 5), (9, 10) Output: 90
Approach:
- Use user defined Pair class to store Breadth and Height of rectangles.
- Make an array of this class.
- Now, traverse the array and find the area each time. Also, keep track of maximum area.
- Return the maximum area of rectangle.
// Java code to find maximum Area import java.io.*; import java.util.*; // Pair class class Rectangle { // length and int length; int breadth; // Rectangle Constructor public Rectangle(int x, int y) { this.length = x; this.breadth = y; } } // Class Area to calculate Area of rectangles class Area { // Function to calculate area static int calculate_Area(Rectangle arr[]) { int max_Area = Integer.MIN_VALUE; // loop to iterate through all rectangles // and keep track of max area for (int i = 0; i < arr.length; i++) { int temp_area = arr[i].length * arr[i].breadth; if (temp_area > max_Area) { max_Area = temp_area; } } return max_Area; } } // Driver class with main function class GFG { // Driver code public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Creating an array of Pair Rectangle arr[] = new Rectangle[3]; int x = 10, y = 20; arr[0] = new Rectangle(x, y); x = 5; y = 25; arr[1] = new Rectangle(x, y); x = 15; y = 10; arr[1] = new Rectangle(x, y); x = 12; y = 12; arr[2] = new Rectangle(x, y); Area obj = new Area(); System.out.println(obj.calculate_Area(arr)); } } |
chevron_right
filter_none
Output:
200
Recommended Posts:
- Maximum area of rectangle possible with given perimeter
- Maximum area rectangle by picking four sides from array
- Ratio of area of a rectangle with the rectangle inscribed in it
- Java Program for Maximum sum rectangle in a 2D matrix | DP-27
- Program for Area And Perimeter Of Rectangle
- Find minimum area of rectangle with given set of coordinates
- Area of the biggest ellipse inscribed within a rectangle
- Probability of a random pair being the maximum weighted pair
- Pair formation such that maximum pair sum is minimized
- Maximum sum rectangle in a 2D matrix | DP-27
- Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
- Maximum AND value of a pair in an array
- Pair with given sum and maximum shortest distance from end
- Find Sum of pair from two arrays with maximum sum
- Maximum area of quadrilateral
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



