forked from Shubhamlmp/Programming-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24_TwoIntervals.cpp
More file actions
37 lines (28 loc) · 740 Bytes
/
24_TwoIntervals.cpp
File metadata and controls
37 lines (28 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/stdc++.h>
using namespace std;
int solution()
{
int l1, r1, l2, r2;
//taking 4 integers as input
cin >> l1 >> r1 >> l2 >> r2;
//we will only print -1 if there is no intersection between the two lines which is only possible if any of these two conditions are true
if ((l2 > r1 && r2 > l1) || (l2 < l1 && r2 < l1))
{
cout << "-1";
}
//taking max of l1, l2 and min of r1, r2 as it is guaranteed that l1<=r1 and l2<=r2 and we have to print the intervals
else
{
int l, r;
l = max(l1, l2);
r = min(r1, r2);
cout << l << " " << r;
}
return 0;
}
int main()
{
//calling the function
solution();
return 0;
}