Rectangular Plane Division - Min and Max Coordinates
Last Updated : 23 Jul, 2025
There is a rectangular plane with dimensions W * H which occupies rectangular area {(x,y): 0 ≤ x ≤ W, 0 ≤ y ≤H. There are N coordinates represented by array cor[][2] and These all co-ordinates are present on a rectangular plane, this rectangular plane is divided into multiple smaller rectangles by performing cut parallel to the y-axis at locations given by array A[] of size X and parallel tothe x-axis at locations given by array B[] of size Y, the rectangular plane will be divided into (X + 1) * (Y + 1) new rectangular pieces. Now your task for this problem is to find the minimum and maximum number of coordinates on all rectangles.
Constraints for the problem:
Width (W) and Height (H) of the grid: 3 <= W, H <= 10^9
Number of points (N): 1 <= N <= 10^5
Coordinates (X, Y) of points: 1 <= X, Y <= 10^5
Points' coordinates (cor[i][0], cor[i][1]) are within the grid bounds: 0 < cor[i][0] < W, 0 < cor[i][1] < H
Length of arrays A and B: 0 < A[0] < A[1] < A[2] < ... < A[X - 1] < W, 0 < B[0] < B[1] < B[2] < ... < B[Y - 1] < H
cor[i][0] is not present in the array A, and cor[i][1] is not present in the array B.
Note: It is Guaranteed that coordinate points will never lie on the edge of any rectangle.
Red dots represent co-ordinates given by array cor[][2]
Cutting rectangle (W, H) at A[] = {2, 5} parallel to y-axis and at B[] = {3, 4} parallel to x-axis. By figure we can tell that maximum number of co-ordinates any rectangle can have is two and minimum is zero.
Input: W = 4, H = 4, cor[][2] = {{1, 1}, {3, 1}, {3, 3}, {1, 3}}, N = 4, A[] = {2}, B[] = {2} Output: 1 1
Approach: To solve the problem follow the below idea:
Hashing can be used to solve this problem. By creating HashMap[][][][] that stores in which rectangle co-ordinate belongs. we can know the maximum and minimum number of co-ordinates in all rectangle pieces. if the HashMap size is equal to (X + 1) * (Y + 1) then each rectangle has at least one co-ordinate otherwise there are rectangles which are empty. We can simply iterate in HashMap and find rectangle which contains maximum co-ordinates and minimum co-ordinates.
Below are the steps for the above approach:
Create HashMap[][][][] that stores a rectangle with 4 co-ordinates its lower-left and upper-right coordinates.
Create two arrays arr1[] and arr2[] of size X + 2 and Y + 2.
Copy the array A[] in arr1[] and B[] in arr2[] with their first elements zero in both arrays and the last element W in arr1[] and H in arr2[]
Iterate over all N coordinates and find the lower bound index of the given x coordinate in arr1[] and y coordinate in arr2[] and store it in ind1 and ind2 respectively.
Increase the counter of Hashmap[arr1[ind1 - 1]][arr1[ind1]][arr2[ind2 - 1]][arr2[ind2]] by 1 which represents this co-ordinate in current iteration belongs to rectangle with lower left co-ordinates (arr1[ind1 - 1], arr2[ind2 - 1]) and upper right co-ordinates (arr1[ind1], arr2[ind2]).
Declare two variables minAns initialized with INT_MAX and maxAns initialized with INT_MIN.
Iterate in HashMap and find the rectangle that consists of the minimum number of coordinates and store it in minAns and find the rectangle that consists of the maximum number of coordinates and store it in maxAns.
if size of HashMap is equal to (X + 1) * (Y + 1) then print minAns and maxAns.
else print 0 and maxAns.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach#include<bits/stdc++.h>usingnamespacestd;// Function to find maximum and minimum// number of co-ordinates in given// rectangle after making cutsvoidfindMinMax(intW,intH,intcor[][2],intN,vector<int>&A,vector<int>&B,intX,intY){// Creating hashmapmap<pair<pair<int,int>,pair<int,int>>,int>HashMap;// creating edges of rectanglevector<int>arr1(X+2,0),arr2(Y+2,0);// copying array A to arr1for(inti=1;i<=X;i++)arr1[i]=A[i-1];arr1.back()=W;// copying array B to arr2for(inti=1;i<=Y;i++)arr2[i]=B[i-1];arr2.back()=H;// iterating each co-ordinatefor(inti=0;i<N;i++){// locating x co-ordinateintind1=lower_bound(arr1.begin(),arr1.end(),cor[i][0])-arr1.begin();// locating y co-ordinateintind2=lower_bound(arr2.begin(),arr2.end(),cor[i][1])-arr2.begin();// increasing counter of HashMapHashMap[{{arr1[ind1-1],arr1[ind1]},{arr2[ind2-1],arr2[ind2]}}]++;}// max and min answerintminAns=INT_MAX,maxAns=INT_MIN;// iterating the HashMapfor(autoe:HashMap){minAns=min(e.second,minAns);maxAns=max(e.second,maxAns);}// if HashMap has all rectanglesif(HashMap.size()==((X+1)*(Y+1))){// printing minAns and maxAnscout<<minAns<<" "<<maxAns<<endl;}else{// printing the minimum answer and maximum answercout<<0<<" "<<maxAns<<endl;}}// Driver Codeint32_tmain(){// Input 1intW=7,H=6;intcor[][2]={{6,1},{3,1},{4,2},{1,5},{6,2}},N=5;vector<int>A={2,5};vector<int>B={3,4};intX=2,Y=2;// Function CallfindMinMax(W,H,cor,N,A,B,X,Y);// Input 2intW1=4,H1=4;intcor1[][2]={{1,1},{3,1},{3,3},{1,3}},N1=4;vector<int>A1={2};vector<int>B1={2};intX1=1,Y1=1;// Function CallfindMinMax(W1,H1,cor1,N1,A1,B1,X1,Y1);return0;}
Java
// Java code to implement the approachimportjava.util.*;classGFG{// Function to find maximum and minimum// number of co-ordinates in given// rectangle after making cutsstaticvoidfindMinMax(intW,intH,intcor[][],intN,intA[],intB[],intX,intY){// Creating hashmapHashMap<String,Integer>HashMap=newHashMap<>();// creating edges of rectangleintarr1[]=newint[X+2];intarr2[]=newint[Y+2];// copying array A to arr1for(inti=1;i<=X;i++)arr1[i]=A[i-1];arr1[X+1]=W;// copying array B to arr2for(inti=1;i<=Y;i++)arr2[i]=B[i-1];arr2[Y+1]=H;// iterating each co-ordinatefor(inti=0;i<N;i++){// locating x co-ordinateintind1=Arrays.binarySearch(arr1,cor[i][0]);if(ind1<0)ind1=-ind1-1;// locating y co-ordinateintind2=Arrays.binarySearch(arr2,cor[i][1]);if(ind2<0)ind2=-ind2-1;// increasing counter of HashMapStringkey=arr1[ind1-1]+" "+arr1[ind1]+" "+arr2[ind2-1]+" "+arr2[ind2];if(HashMap.containsKey(key))HashMap.put(key,HashMap.get(key)+1);elseHashMap.put(key,1);}// max and min answerintminAns=Integer.MAX_VALUE,maxAns=Integer.MIN_VALUE;// iterating the HashMapfor(Map.Entry<String,Integer>e:HashMap.entrySet()){minAns=Math.min(e.getValue(),minAns);maxAns=Math.max(e.getValue(),maxAns);}// if HashMap has all rectanglesif(HashMap.size()==((X+1)*(Y+1))){// printing minAns and maxAnsSystem.out.println(minAns+" "+maxAns);}else{// printing the minimum answer and maximum// answerSystem.out.println(0+" "+maxAns);}}// Driver Codepublicstaticvoidmain(String[]args){// Input 1intW=7,H=6;intcor[][]={{6,1},{3,1},{4,2},{1,5},{6,2}};intN=5;intA[]={2,5};intB[]={3,4};intX=2,Y=2;// Function CallfindMinMax(W,H,cor,N,A,B,X,Y);// Input 2intW1=4,H1=4;intcor1[][]={{1,1},{3,1},{3,3},{1,3}};intN1=4;intA1[]={2};intB1[]={2};intX1=1,Y1=1;// Function CallfindMinMax(W1,H1,cor1,N1,A1,B1,X1,Y1);}}// This code is contributed by Tapesh(tapeshdua420)
Python3
# Python code to implement the approachfrombisectimportbisect_left# Function to find maximum and minimum# number of co-ordinates in given# rectangle after making cutsdeffindMinMax(W,H,cor,N,A,B,X,Y):# Creating hashmapHashMap={}# creating edges of rectanglearr1=[0]*(X+2)arr2=[0]*(Y+2)# copying array A to arr1foriinrange(X):arr1[i+1]=A[i]arr1[-1]=W# copying array B to arr2foriinrange(Y):arr2[i+1]=B[i]arr2[-1]=H# iterating each co-ordinateforiinrange(N):# locating x co-ordinateind1=bisect_left(arr1,cor[i][0])# locating y co-ordinateind2=bisect_left(arr2,cor[i][1])# increasing counter of HashMapHashMap[(arr1[ind1-1],arr1[ind1],arr2[ind2-1],arr2[ind2])]=HashMap.get((arr1[ind1-1],arr1[ind1],arr2[ind2-1],arr2[ind2]),0)+1# max and min answerminAns=float('inf')maxAns=float('-inf')# iterating the HashMapforeinHashMap:minAns=min(HashMap[e],minAns)maxAns=max(HashMap[e],maxAns)# if HashMap has all rectanglesiflen(HashMap)==((X+1)*(Y+1)):# printing minAns and maxAnsprint(minAns,maxAns)else:# printing the minimum answer and maximum answerprint(0,maxAns)# Driver Codeif__name__=='__main__':# Input 1W=7H=6cor=[[6,1],[3,1],[4,2],[1,5],[6,2]]N=5A=[2,5]B=[3,4]X=2Y=2# Function CallfindMinMax(W,H,cor,N,A,B,X,Y)# Input 2W1=4H1=4cor1=[[1,1],[3,1],[3,3],[1,3]]N1=4A1=[2]B1=[2]X1=1Y1=1# Function CallfindMinMax(W1,H1,cor1,N1,A1,B1,X1,Y1)# This code is contributed by Tapesh(tapeshdua420)
C#
usingSystem;usingSystem.Collections.Generic;classGFG{// Function to find maximum and minimum// number of co-ordinates in given// rectangle after making cutsstaticvoidFindMinMax(intW,intH,int[][]cor,intN,int[]A,int[]B,intX,intY){// Creating dictionaryDictionary<string,int>dictionary=newDictionary<string,int>();// creating edges of rectangleint[]arr1=newint[X+2];int[]arr2=newint[Y+2];// copying array A to arr1for(inti=1;i<=X;i++)arr1[i]=A[i-1];arr1[X+1]=W;// copying array B to arr2for(inti=1;i<=Y;i++)arr2[i]=B[i-1];arr2[Y+1]=H;// iterating each co-ordinatefor(inti=0;i<N;i++){// locating x co-ordinateintind1=Array.BinarySearch(arr1,cor[i][0]);if(ind1<0)ind1=~ind1;// locating y co-ordinateintind2=Array.BinarySearch(arr2,cor[i][1]);if(ind2<0)ind2=~ind2;// increasing counter of dictionarystringkey=arr1[ind1-1]+" "+arr1[ind1]+" "+arr2[ind2-1]+" "+arr2[ind2];if(dictionary.ContainsKey(key))dictionary[key]++;elsedictionary[key]=1;}// max and min answerintminAns=int.MaxValue,maxAns=int.MinValue;// iterating the dictionaryforeach(varentryindictionary){minAns=Math.Min(entry.Value,minAns);maxAns=Math.Max(entry.Value,maxAns);}// if dictionary has all rectanglesif(dictionary.Count==((X+1)*(Y+1))){// printing minAns and maxAnsConsole.WriteLine(minAns+" "+maxAns);}else{// printing the minimum answer and maximum// answerConsole.WriteLine(0+" "+maxAns);}}// Driver CodepublicstaticvoidMain(string[]args){// Input 1intW=7,H=6;int[][]cor={newint[]{6,1},newint[]{3,1},newint[]{4,2},newint[]{1,5},newint[]{6,2}};intN=5;int[]A={2,5};int[]B={3,4};intX=2,Y=2;// Function CallFindMinMax(W,H,cor,N,A,B,X,Y);// Input 2intW1=4,H1=4;int[][]cor1={newint[]{1,1},newint[]{3,1},newint[]{3,3},newint[]{1,3}};intN1=4;int[]A1={2};int[]B1={2};intX1=1,Y1=1;// Function CallFindMinMax(W1,H1,cor1,N1,A1,B1,X1,Y1);}}
JavaScript
functionbisect_left(arr,target){// Binary search to find the leftmost index where target can be inserted in the sorted arrayletleft=0;letright=arr.length;while(left<right){constmid=Math.floor((left+right)/2);if(arr[mid]<target){left=mid+1;}else{right=mid;}}returnleft;}functionfindMinMax(W,H,cor,N,A,B,X,Y){constHashMap={};constarr1=newArray(X+2).fill(0);// Array for X coordinatesconstarr2=newArray(Y+2).fill(0);// Array for Y coordinates// Copying array A to arr1for(leti=0;i<X;i++){arr1[i+1]=A[i];}arr1[X+1]=W;// Set the last element as the width of the rectangle// Copying array B to arr2for(leti=0;i<Y;i++){arr2[i+1]=B[i];}arr2[Y+1]=H;// Set the last element as the height of the rectangle// Iterate through each coordinatefor(leti=0;i<N;i++){constind1=bisect_left(arr1,cor[i][0]);// Locate x coordinateconstind2=bisect_left(arr2,cor[i][1]);// Locate y coordinateconstkey=[arr1[ind1-1],arr1[ind1],arr2[ind2-1],arr2[ind2]].toString();// Increase the counter for the coordinate in HashMapHashMap[key]=(HashMap[key]||0)+1;}letminAns=Infinity;letmaxAns=-Infinity;// Iterate through the HashMap to find min and max answersfor(consteinHashMap){constcount=HashMap[e];minAns=Math.min(count,minAns);maxAns=Math.max(count,maxAns);}if(Object.keys(HashMap).length===(X+1)*(Y+1)){console.log("Minimum number of coordinates:",minAns,"Maximum number of coordinates:",maxAns);}else{console.log("Minimum number of coordinates:",0,"Maximum number of coordinates:",maxAns);}}// Driver CodeconstW=7;constH=6;constcor=[[6,1],[3,1],[4,2],[1,5],[6,2]];constN=5;constA=[2,5];constB=[3,4];constX=2;constY=2;findMinMax(W,H,cor,N,A,B,X,Y);constW1=4;constH1=4;constcor1=[[1,1],[3,1],[3,3],[1,3]];constN1=4;constA1=[2];constB1=[2];constX1=1;constY1=1;findMinMax(W1,H1,cor1,N1,A1,B1,X1,Y1);