Given an array arr[] of positive integers representing lengths, the task is to find the maximum possible rectangular area that can be formed using four sides from the array.
Note: A valid rectangle requires at least two pairs of equal values. If no such rectangle is possible, return 0.
Examples:
Input: arr[] = {2, 1, 2, 5, 4, 4}
Output: 8
Explanation: Dimension will be 4 * 2Input: arr[] = {1, 1, 2, 2, 6, 6}
Output: 12
Explanation: Dimension will be 6 * 2Input: arr[] = [2, 1, 3, 5, 4, 4]
Output: 0
Explanation: No rectangle possible
[Expected Approach 1] Using Sorting - O(n log n) Time and O(1) Space
The idea is to find the two largest duplicate numbers in the array, as they can form the sides of the rectangle.
The thought process is to sort the array in non-increasing order, ensuring that the largest duplicate numbers appear first. Then, we traverse the sorted array to find two pairs of equal numbers, which will be the rectangle's sides. Finally, we return the product of these two largest duplicate values to get the maximum rectangle area.
// C++ program to find the maximum possible area
// of a rectangle using Sorting
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum rectangle area
int findArea(vector<int>& arr) {
// Sort array in non-increasing order
sort(arr.rbegin(), arr.rend());
// Initialize two sides of the rectangle
vector<int> dimension(2, 0);
// Traverse through the array to find two pairs
for (int i = 0, j = 0;
i < arr.size() - 1 && j < 2; i++) {
// If a duplicate is found, store it
// as a dimension
if (arr[i] == arr[i + 1]) {
dimension[j++] = arr[i++];
}
}
// Return the product of the
// found dimensions
return dimension[0] * dimension[1];
}
// Driver function
int main() {
vector<int> arr = {2, 1, 2, 5, 4, 4};
cout << findArea(arr);
return 0;
}
// Java program to find the maximum possible area
// of a rectangle using Sorting
import java.util.Arrays;
class GfG {
// Function to find the maximum rectangle area
static int findArea(int[] arr) {
// Sort array in non-increasing order
Arrays.sort(arr);
int n = arr.length;
// Initialize two sides of the rectangle
int[] dimension = {0, 0};
// Traverse through the array to find two pairs
for (int i = n - 1, j = 0;
i > 0 && j < 2; i--) {
// If a duplicate is found, store it
// as a dimension
if (arr[i] == arr[i - 1]) {
dimension[j++] = arr[i--];
}
}
// Return the product of the
// found dimensions
return dimension[0] * dimension[1];
}
// Driver function
public static void main(String[] args) {
int[] arr = {2, 1, 2, 5, 4, 4};
System.out.println(findArea(arr));
}
}
# Python program to find the maximum possible area
# of a rectangle using Sorting
# Function to find the maximum rectangle area
def findArea(arr):
# Sort array in non-increasing order
arr.sort(reverse=True)
# Initialize two sides of the rectangle
dimension = [0, 0]
# Traverse through the array to find two pairs
i, j = 0, 0
while i < len(arr) - 1 and j < 2:
# If a duplicate is found, store it
# as a dimension
if arr[i] == arr[i + 1]:
dimension[j] = arr[i]
j += 1
i += 1
i += 1
# Return the product of the
# found dimensions
return dimension[0] * dimension[1]
# Driver function
if __name__ == "__main__":
arr = [2, 1, 2, 5, 4, 4]
print(findArea(arr))
// C# program to find the maximum possible area
// of a rectangle using Sorting
using System;
class GfG {
// Function to find the maximum rectangle area
static int FindArea(int[] arr) {
// Sort array in non-increasing order
Array.Sort(arr);
Array.Reverse(arr);
// Initialize two sides of the rectangle
int[] dimension = {0, 0};
// Traverse through the array to find two pairs
for (int i = 0, j = 0;
i < arr.Length - 1 && j < 2; i++) {
// If a duplicate is found, store it
// as a dimension
if (arr[i] == arr[i + 1]) {
dimension[j++] = arr[i++];
}
}
// Return the product of the
// found dimensions
return dimension[0] * dimension[1];
}
// Driver function
public static void Main() {
int[] arr = {2, 1, 2, 5, 4, 4};
Console.WriteLine(FindArea(arr));
}
}
// JavaScript program to find the maximum possible area
// of a rectangle using Sorting
// Function to find the maximum rectangle area
function findArea(arr) {
// Sort array in non-increasing order
arr.sort((a, b) => b - a);
// Initialize two sides of the rectangle
let dimension = [0, 0];
// Traverse through the array to find two pairs
let i = 0, j = 0;
while (i < arr.length - 1 && j < 2) {
// If a duplicate is found, store it
// as a dimension
if (arr[i] === arr[i + 1]) {
dimension[j++] = arr[i];
i++;
}
i++;
}
// Return the product of the
// found dimensions
return dimension[0] * dimension[1];
}
// Driver code
let arr = [2, 1, 2, 5, 4, 4];
console.log(findArea(arr));
Output
8
Time Complexity: O(n log n) due to sorting, followed by O(n) traversal, making it O(n log n) overall.
Space Complexity: O(1) as sorting is in-place and only a few extra variables are used.
[Expected Approach 2] Using Set - O(n) Time and O(n) Space
The idea is to find the two largest distinct duplicate elements in the array to form the maximum possible rectangle. The thought process behind this is to use a set to track unique elements and update the two largest duplicate values dynamically. As we traverse the array, we check if an element appears for the second time; if it does, we update our first and second largest values accordingly. Finally, we return the product of these two values as the maximum possible area.
// C++ program to find the maximum possible area
// of a rectangle using Set
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum rectangle area
int findArea(vector<int>& arr) {
unordered_set<int> st;
// Initialize two largest dimensions
int first = 0, second = 0;
// Traverse through the array
for (int num : arr) {
// If this is the first occurrence of num,
// insert into the set and continue
if (st.find(num) == st.end()) {
st.insert(num);
continue;
}
// If this is the second occurrence,
// update the two largest dimensions
if (num > first) {
second = first;
first = num;
st.erase(num);
}
else if (num > second) {
second = num;
st.erase(num);
}
}
// Return the product of the two largest dimensions
return first * second;
}
// Driver function
int main() {
vector<int> arr = {2, 1, 2, 5, 4, 4};
cout << findArea(arr);
return 0;
}
// Java program to find the maximum possible area
// of a rectangle using Set
import java.util.*;
class GfG {
// Function to find the maximum rectangle area
static int findArea(int[] arr) {
HashSet<Integer> st = new HashSet<>();
// Initialize two largest dimensions
int first = 0, second = 0;
// Traverse through the array
for (int num : arr) {
// If this is the first occurrence of num,
// insert into the set and continue
if (!st.contains(num)) {
st.add(num);
continue;
}
// If this is the second occurrence,
// update the two largest dimensions
if (num > first) {
second = first;
first = num;
st.remove(num);
}
else if (num > second) {
second = num;
st.remove(num);
}
}
// Return the product of the two largest dimensions
return first * second;
}
// Driver function
public static void main(String[] args) {
int[] arr = {2, 1, 2, 5, 4, 4};
System.out.println(findArea(arr));
}
}
# Python program to find the maximum possible area
# of a rectangle using Set
# Function to find the maximum rectangle area
def findArea(arr):
st = set()
# Initialize two largest dimensions
first = 0
second = 0
# Traverse through the array
for num in arr:
# If this is the first occurrence of num,
# insert into the set and continue
if num not in st:
st.add(num)
continue
# If this is the second occurrence,
# update the two largest dimensions
if num > first:
second = first
first = num
st.remove(num)
elif num > second:
second = num
st.remove(num)
# Return the product of the two largest dimensions
return first * second
# Driver function
if __name__ == "__main__":
arr = [2, 1, 2, 5, 4, 4]
print(findArea(arr))
// C# program to find the maximum possible area
// of a rectangle using Set
using System;
using System.Collections.Generic;
class GfG {
// Function to find the maximum rectangle area
static int findArea(int[] arr) {
HashSet<int> st = new HashSet<int>();
// Initialize two largest dimensions
int first = 0, second = 0;
// Traverse through the array
foreach (int num in arr) {
// If this is the first occurrence of num,
// insert into the set and continue
if (!st.Contains(num)) {
st.Add(num);
continue;
}
// If this is the second occurrence,
// update the two largest dimensions
if (num > first) {
second = first;
first = num;
st.Remove(num);
}
else if (num > second) {
second = num;
st.Remove(num);
}
}
// Return the product of the two largest dimensions
return first * second;
}
// Driver function
public static void Main() {
int[] arr = {2, 1, 2, 5, 4, 4};
Console.WriteLine(findArea(arr));
}
}
// JavaScript program to find the maximum possible area
// of a rectangle using Set
// Function to find the maximum rectangle area
function findArea(arr) {
let st = new Set();
// Initialize two largest dimensions
let first = 0, second = 0;
// Traverse through the array
for (let num of arr) {
// If this is the first occurrence of num,
// insert into the set and continue
if (!st.has(num)) {
st.add(num);
continue;
}
// If this is the second occurrence,
// update the two largest dimensions
if (num > first) {
second = first;
first = num;
st.delete(num);
}
else if (num > second) {
second = num;
st.delete(num);
}
}
// Return the product of the two largest dimensions
return first * second;
}
// Driver code
let arr = [2, 1, 2, 5, 4, 4];
console.log(findArea(arr));
Output
8
Time Complexity: O(n) since we traverse the array once and set operations (insert, find, erase) take O(1) on average.
Space Complexity: O(n) due to the unordered set storing unique elements.