Write a method to compute the difference between two ranges. A range is defined by an integer low and an integer high. A - B (A “minus” B) is “everything in A that is not in B”.
This is an interview question and here is my code:
void findDiff(int a1,int a2, int a3, int a4)
{
cout<<"("<<a1<<","<<a2<<") - ("<<a3<<","<<a4<<") = \n";
if(a1 < a3)
{
int end = (a2<a3) ? a2: a3-1;
for(int i=a1; i<=end; i++) cout<<i<<"\t";
}
if(a4 < a2)
{
int start = (a1>a4) ? a1 : (a4+1);
for(int i=start; i<=a2; i++) cout<<i<<"\t";
}
cout<<"\n";
}
Please look at the code and tell me if it correct and if I'm missing any case. Thanks a lot in advance :)
athat you have tested.) – mnhg Feb 22 at 6:00