Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
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 *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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