http://www.codechef.com/problems/CAKE1AM

I cant figure out why my code is not being accepted. It is giving me wrong output.The link to question is http://www.codechef.com/problems/CAKE1AM. My code is -

#include
#include <math.h>
#include <stdlib.h>
using namespace std;

int main()
{
int t;
int a[5];
int b[5];
int c[3];

cin>>t;
while(t--)
{
	for(int i = 0; i < 4; i++)
	{
		cin>>a[i];
	}
	for(int i = 0; i < 4; i++)
	{
		cin>>b[i];
	}
	if((a[2]<b[0]) && (a[3]<b[1]))
	{
		c[0]=abs((a[2]-a[0]) *(a[1]-a[3]));
		c[1] =abs((b[0]-b[2])*(b[1]-b[3])); 
	
	}
	else  
	{
			c[0]=abs((a[2]-a[0]) *(a[1]-a[3]));
			c[1] =abs((b[0]-b[2])*(b[1]-b[3]) ) - abs((a[2]-b[0])*(a[3]-b[1]));  
	}
	cout<<c[0]+c[1]<<endl;
}
return 0;

}

You haven’t considered numerous conditions in this case. You have only considered the case when the two rectangles overlap from the top right corner of lower rectangle and bottom right corner of upper rectangle . Many cases are missing in this. Other examples are when the second rectangle overlaps on the upper edge or right edge of the other rectangle but not on the corner point and many more.

1 Like