Given two arrays X[] and Y[] with n-elements, where (Xi, Yi) represent a point on coordinate system, find the smallest rectangle such that all points from given input lie inside that rectangle and sides of rectangle must be parallel to Coordinate axis. Print all four coordinates of obtained rectangle.
Note : n >= 4 (we have at least 4 points as input).
Examples :
Input : X[] = {1, 3, 0, 4}, y[] = {2, 1, 0, 2}
Output : {0, 0}, {0, 2}, {4, 2}, {4, 0}
Input : X[] = {3, 6, 1, 9, 13, 0, 4}, Y[] = {4, 2, 6, 5, 2, 3, 1}
Output : {0, 1}, {0, 6}, {13, 6}, {13, 1}
For finding the smallest rectangle you may take two of basic approach :
- Consider origin of coordinate plane as smallest rectangle and then step by step keep expanding it as per value of coordinates of points if they don’t lie inside the current rectangle. This concept requires a little of complex logic to find the exact smallest rectangle.
- A very simple and efficient logic behind this is to find the smallest as well as largest x & y coordinates among all given points and then the all possible four combinations of these values result the four points of required rectangle as {Xmin, Ymin}, {Xmin, Ymax}, {Xmax, Ymax}, {Xmax, Ymin}.
C++
#include <bits/stdc++.h>
using namespace std;
void printRect(int X[], int Y[], int n)
{
int Xmax = *max_element(X, X + n);
int Xmin = *min_element(X, X + n);
int Ymax = *max_element(Y, Y + n);
int Ymin = *min_element(Y, Y + n);
cout << "{" << Xmin << ", " << Ymin << "}" << endl;
cout << "{" << Xmin << ", " << Ymax << "}" << endl;
cout << "{" << Xmax << ", " << Ymax << "}" << endl;
cout << "{" << Xmax << ", " << Ymin << "}" << endl;
}
int main()
{
int X[] = { 4, 3, 6, 1, -1, 12 };
int Y[] = { 4, 1, 10, 3, 7, -1 };
int n = sizeof(X) / sizeof(X[0]);
printRect(X, Y, n);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
class GFG {
static void printRect(Integer X[], Integer Y[], int n)
{
int Xmax = Collections.max(Arrays.asList(X));
int Xmin = Collections.min(Arrays.asList(X));
int Ymax = Collections.max(Arrays.asList(Y));
int Ymin = Collections.min(Arrays.asList(Y));
System.out.println("{" + Xmin + ", " + Ymin + "}");
System.out.println("{" + Xmin + ", " + Ymax + "}");
System.out.println("{" + Xmax + ", " + Ymax + "}");
System.out.println("{" + Xmax + ", " + Ymin + "}");
}
public static void main (String[] args)
{
Integer X[] = { 4, 3, 6, 1, -1, 12 };
Integer Y[] = { 4, 1, 10, 3, 7, -1 };
int n = X.length;
printRect(X, Y, n);
}
}
|
Python 3
def printRect(X, Y, n):
Xmax = max(X)
Xmin = min(X)
Ymax = max(Y)
Ymin = min(Y)
print("{",Xmin,", ",Ymin,"}",sep="" )
print("{",Xmin,", ",Ymax,"}",sep="" )
print("{",Xmax,", ",Ymax,"}",sep="" )
print("{",Xmax,", ",Ymin,"}",sep="" )
X = [4, 3, 6, 1, -1, 12]
Y = [4, 1, 10, 3, 7, -1]
n = len(X)
printRect(X, Y, n)
|
C#
using System.Linq;
using System;
public class GFG{
static void printRect(int[] X,
int[] Y, int n)
{
int Xmax = X.Max();
int Xmin = X.Min();
int Ymax = Y.Max();
int Ymin = Y.Min();
Console.WriteLine("{" + Xmin + ", "
+ Ymin + "}");
Console.WriteLine("{" + Xmin + ", "
+ Ymax + "}");
Console.WriteLine("{" + Xmax + ", "
+ Ymax + "}");
Console.WriteLine("{" + Xmax + ", "
+ Ymin + "}");
}
static public void Main ()
{
int[] X = { 4, 3, 6, 1, -1, 12 };
int[] Y = { 4, 1, 10, 3, 7, -1 };
int n = X.Length;
printRect(X, Y, n);
}
}
|
PHP
<?php
function printRect($X, $Y, $n)
{
$Xmax = max($X);
$Xmin = min($X);
$Ymax = max($Y);
$Ymin = min($Y);
echo "{" , $Xmin , ", " , $Ymin , "}","\n";
echo "{" , $Xmin , ", " , $Ymax , "}" ,"\n";
echo "{" , $Xmax , ", " , $Ymax , "}","\n";
echo "{" , $Xmax , ", " , $Ymin , "}" ;
}
$X = array(4, 3, 6, 1, -1, 12);
$Y = array(4, 1, 10, 3, 7, -1);
$n =count($X);
printRect($X, $Y, $n);
?>
|
Output:
{-1, -1}
{-1, 10}
{12, 10}
{12, -1}
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.