These are my answers for a codeforces problem and I don't know why the first snippet gives a wrong answer.
The second is accepted though.
I want to know if there is a problem with the judgment test cases because they seem to give the same output.
The problem says the following:
Given the boundaries of 2 intervals. Print the boundaries of their intersection.
Note: Boundaries mean the two ends of an interval which are the starting number and the ending number.
Input: Only one line contains two intervals [l1,r1], [l2,r2] where (1≤l1,l2,r1,r2≤109), (l1≤r1,l2≤r2).
It's guaranteed that l1≤r1 and l2≤r2.
Output: If there is an intersection between these 2 intervals print its boundaries , otherwise print -1.
Snippet 1
#include <bits/stdc .h>
using namespace std;
int main()
{
int a, b, c, d;
int z;
cin >> a >> b >> c >> d;
if(a > b)
{
z = a;
a = b;
b = z;
}
if(c > d)
{
z = c;
c = d;
d = z;
}
if(c > b)
{
cout << -1;
}
else
{
cout << max(a, c) << " " << min(b, d);
}
return 0
}
Snippet 2
#include <bits/stdc .h>
using namespace std;
int main()
{
int l1 , r1 , l2 , r2;
cin >> l1 >> r1 >> l2 >> r2;
int _begin = max(l1,l2);
int _end = min(r1,r2);
if (_begin > _end)
cout << -1;
else
cout << begin << " " << end;
return 0;
}
CodePudding user response:
In the first program you are checking only one condition
if(c > b)
{
cout << -1;
}
But you need to check also the following condition
if ( d < a )
{
cout << -1;
}
For example
if(c > b || d < a )
{
cout << -1;
}
else
{
//...
}