Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Rectangle Area problem solution

YASH PAL, 31 July 202420 January 2026

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

Leetcode Rectangle Area 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

Rectangle Area 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 solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes