Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Rectangle Area problem solution

YASH PAL, 31 July 2024

In this Leetcode Rectangle Area problem solution, we have given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.

  1. The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).
  2. The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).
Leetcode Rectangle Area problem solution

Problem solution in Python.

def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
    l,w=0,0
    
    if min(ax2,bx2)>max(ax1,bx1):
        l=min(ax2,bx2)-max(ax1,bx1)

    if min(ay2,by2)>max(ay1,by1):
        w=min(ay2,by2)-max(ay1,by1)
    
    return ((abs(ax2-ax1))*(abs(ay1-ay2)))+((abs(bx1-bx2))*(abs(by1-by2)))-l*w

Problem solution in Java.

class Solution {
    public boolean overlaps(int A, int B, int C, int D, int E, int F, int G, int H){
        if(!(B>H || D<F  || A>G || C<E  )){
            return true;
        }
        return false;
    }
    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int rect1=(D-B)*(C-A);
        int rect2 =(G-E)*(H-F);
        int overlap=0;
        if(overlaps(A,B,C,D,E,F,G,H)){
                int overlapY1=Math.max(F,B);
                int overlapX1=Math.max(E,A);
                int overlapY2=Math.min(H,D);
                int overlapX2=Math.min(G,C);
                overlap=(overlapX2-overlapX1)*(overlapY2-overlapY1);
        }
        return rect1+rect2-overlap;
       
    }
}

Problem solution in C++.

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)  {
        int area1 = (C - A) * (D - B), area2 = (G - E) * (H - F);
        int x_min = std::max(A,E), x_max = std::min(C,G);
        if (x_min >= x_max)
            return area1 + area2;
        int y_min = std::max(B,F), y_max = std::min(D,H);
        if (y_min >= y_max)
            return area1 + area2;
        return area1 - (x_max - x_min) * (y_max - y_min) + area2;
    }
};

Problem solution in C.

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {

if(G<=A||E>=C||D<=F||B>=H){
    return (C-A)*(D-B)+(G-E)*(H-F);
}
if(A==C||B==D){
    return (G-E)*(H-F);
}
if(E==G||H==F){
    return (C-A)*(D-B);
}

int tot=(C-A)*(D-B)+(G-E)*(H-F);
int coverwidth=0;
int coverheight=0;
coverwidth=(G>C?C:G)-(E>A?E:A);
coverheight=(D>H?H:D)-(B>F?B:F);

return tot-coverwidth*coverheight;
}

coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes