Given N distinct points[][], the task is to find the maximum area of a triangle that can be formed having one of the sides parallel to X axis and another pair of sides parallel to Y axis.
Examples:
Input: N = 4, points[][] = {{1, 1}, {1, 4}, {3, 1}, {5, 1}}
Output: 6
Explanation: The points {1, 1}, {1, 4} and {5, 1} forms a triangle with area = (1/2) * 4 * 3 = 6Input: N = 4, points[][] = {{2, 2}, {2, 5}, {2, 7}, {5, 5}}
Output: 4.5
Explanation: The points {2, 2}, {2, 5} and {5, 5} forms a triangle with area = (1/2) * 3 * 3 = 4.5
Approach: To solve the problem, follow the below idea:
The problem can be solved by taking all the possible combinations of 3 points and considering one pair as parallel to X axis and another pair as parallel to Y axis. If the sides are parallel to X and Y axis, then calculate the area of the triangle using the formula:
Area = 0.5 * base * height
Step-by-step approach:
- Iterate through all combinations of three points using nested loops.
- Check if the current combination forms a triangle with one side parallel to the X-axis and one side parallel to the Y-axis.
- Calculate the base and height of the triangle formed by the three points.
- Calculate the area of the triangle using the base and height.
- Update maxArea if the current area is greater.
- Return half of the maxArea as the final result.
Below is the implementation of the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to check whether the points are parallel to
// X-axis
bool isParallelToX(pair<int, int> p1, pair<int, int> p2)
{
// Check if the y-coordinates are equal
return p1.second == p2.second;
}
// Function to check whether the points are parallel to
// Y-axis
bool isParallelToY(pair<int, int> p1, pair<int, int> p2)
{
// Check if the x-coordinates are equal
return p1.first == p2.first;
}
// Function to find the maximum area of the triangle
double solve(vector<pair<int, int> > points, int N)
{
// For storing maximum area
int maxArea = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
if (isParallelToX(points[i], points[j])
&& isParallelToY(points[i],
points[k])) {
// Calculate base and height of the
// triangle
int base = abs(points[i].first
- points[j].first);
int height = abs(points[i].second
- points[k].second);
int area = base * height;
// Update maxArea if current area is
// greater
maxArea = max(area, maxArea);
}
}
}
}
// Return half of the maximum area found
return maxArea / 2.0;
}
// Driver code
int main()
{
int N = 4;
vector<pair<int, int> > points
= { { 2, 2 }, { 2, 5 }, { 2, 7 }, { 5, 5 } };
// Call solve function with the points vector and N as
// arguments and output the result
cout << solve(points, N) << endl;
return 0;
}
import java.util.Vector;
class Main {
// Function to check whether the points are parallel to
// X-axis
static boolean isParallelToX(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) {
// Check if the y-coordinates are equal
return p1.second.equals(p2.second);
}
// Function to check whether the points are parallel to
// Y-axis
static boolean isParallelToY(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) {
// Check if the x-coordinates are equal
return p1.first.equals(p2.first);
}
// Function to find the maximum area of the triangle
static double solve(Vector<Pair<Integer, Integer>> points, int N) {
// For storing maximum area
int maxArea = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
if (isParallelToX(points.get(i), points.get(j)) &&
isParallelToY(points.get(i), points.get(k))) {
// Calculate base and height of the triangle
int base = Math.abs(points.get(i).first - points.get(j).first);
int height = Math.abs(points.get(i).second - points.get(k).second);
int area = base * height;
// Update maxArea if current area is greater
maxArea = Math.max(area, maxArea);
}
}
}
}
// Return half of the maximum area found
return maxArea / 2.0;
}
// Driver code
public static void main(String[] args) {
int N = 4;
Vector<Pair<Integer, Integer>> points = new Vector<>();
points.add(new Pair<>(2, 2));
points.add(new Pair<>(2, 5));
points.add(new Pair<>(2, 7));
points.add(new Pair<>(5, 5));
// Call solve function with the points vector and N as
// arguments and output the result
System.out.println(solve(points, N));
}
}
class Pair<U, V> {
public final U first;
public final V second;
public Pair(U first, V second) {
this.first = first;
this.second = second;
}
}
// This code is contributed by shivamgupta0987654321
# Function to check whether the points are parallel to X-axis
def is_parallel_to_x(p1, p2):
# Check if the y-coordinates are equal
return p1[1] == p2[1]
# Function to check whether the points are parallel to Y-axis
def is_parallel_to_y(p1, p2):
# Check if the x-coordinates are equal
return p1[0] == p2[0]
# Function to find the maximum area of the triangle
def solve(points):
# For storing maximum area
max_area = 0
# Iterate through all combinations of points
for i in range(len(points)):
for j in range(len(points)):
for k in range(len(points)):
if is_parallel_to_x(points[i], points[j]) and is_parallel_to_y(points[i], points[k]):
# Calculate base and height of the triangle
base = abs(points[i][0] - points[j][0])
height = abs(points[i][1] - points[k][1])
area = base * height
# Update max_area if current area is greater
max_area = max(area, max_area)
# Return half of the maximum area found
return max_area / 2.0
# Driver code
if __name__ == "__main__":
points = [(2, 2), (2, 5), (2, 7), (5, 5)]
# Call solve function with the points list as argument and output the result
print(solve(points))
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
// Function to check whether the points are parallel to X-axis
static bool IsParallelToX(Tuple<int, int> p1, Tuple<int, int> p2)
{
// Check if the y-coordinates are equal
return p1.Item2 == p2.Item2;
}
// Function to check whether the points are parallel to Y-axis
static bool IsParallelToY(Tuple<int, int> p1, Tuple<int, int> p2)
{
// Check if the x-coordinates are equal
return p1.Item1 == p2.Item1;
}
// Function to find the maximum area of the triangle
static double Solve(List<Tuple<int, int>> points, int N)
{
// For storing maximum area
int maxArea = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < N; k++)
{
if (IsParallelToX(points[i], points[j]) && IsParallelToY(points[i], points[k]))
{
// Calculate baseLength and height of the triangle
int baseLength = Math.Abs(points[i].Item1 - points[j].Item1);
int height = Math.Abs(points[i].Item2 - points[k].Item2);
int area = baseLength * height;
// Update maxArea if current area is greater
maxArea = Math.Max(area, maxArea);
}
}
}
}
// Return half of the maximum area found
return maxArea / 2.0;
}
// Driver code
static void Main(string[] args)
{
int N = 4;
List<Tuple<int, int>> points = new List<Tuple<int, int>>
{
new Tuple<int, int>(2, 2),
new Tuple<int, int>(2, 5),
new Tuple<int, int>(2, 7),
new Tuple<int, int>(5, 5)
};
// Call solve function with the points vector and N as arguments and output the result
Console.WriteLine(Solve(points, N));
}
}
// Function to check whether the points are parallel to X-axis
function isParallelToX(p1, p2) {
// Check if the y-coordinates are equal
return p1[1] === p2[1];
}
// Function to check whether the points are parallel to Y-axis
function isParallelToY(p1, p2) {
// Check if the x-coordinates are equal
return p1[0] === p2[0];
}
// Function to find the maximum area of the triangle
function solve(points) {
// For storing maximum area
let maxArea = 0;
let N = points.length;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
for (let k = 0; k < N; k++) {
if (isParallelToX(points[i], points[j]) && isParallelToY(points[i], points[k])) {
// Calculate base and height of the triangle
let base = Math.abs(points[i][0] - points[j][0]);
let height = Math.abs(points[i][1] - points[k][1]);
let area = base * height;
// Update maxArea if current area is greater
maxArea = Math.max(area, maxArea);
}
}
}
}
// Return half of the maximum area found
return maxArea / 2.0;
}
// Driver code
let points = [[2, 2], [2, 5], [2, 7], [5, 5]];
// Call solve function with the points vector and output the result
console.log(solve(points));
Output
4.5
Time Complexity: O(N^3), where N is the number of co-ordinates given as input.
Auxiliary Space: O(1)