# Sharing (in Java) and asking

• I think this is a very simple solution. The area is simply the product of the width and the height. The width and height can be found by calculating the length of intersection of two rectangles along x axis and y axis respectively. The solution is like this:

``````    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int width=lengthOfIntersection(A,C,E,G);
int height=lengthOfIntersection(B,D,F,H);
return width*height;
}

int lengthOfIntersection(int A, int B, int C, int D){
int length=Math.min(B,D)-Math.max(A,C);
return length>0?length:0;
}
``````

But this fails in the case (0,0,0,0,-1,-1,1,1). My output is 0 but 4 is expected. But from the input we can see the area of the first rectangle is already 0, how can the area of the overlapping part become 4?!

• Lol I think I may have misunderstood the question....

• I was just computing the area of the intersection but the question is to compute the total area. So the updated version is as follows and is accepted:

``````public 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);
int area2=(G-E)*(H-F);
int width=lengthOfIntersection(A,C,E,G);
int height=lengthOfIntersection(B,D,F,H);
return area1+area2-width*height;
}

int lengthOfIntersection(int A, int B, int C, int D){
int length=0;
if(Math.min(B,D)>Math.max(A,C))
length=Math.min(B,D)-Math.max(A,C);
return length;
}
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.