Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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
Programming101
Programmingoneonone

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 solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes