Given two segments **[L1, R1]** and **[L2, R2]**, the task is to choose two elements **x** and **y** from both the ranges (one from range one and other from range two) such that no element belongs to both the ranges i.e. **x** belongs to first range and **y** belongs to second range. If no such element exists then print **-1** instead.

**Examples:**

Input:L1 = 1, R1 = 6, L2 = 3, R2 = 11

Output:1 11

1 lies only in range [1, 6] and 11 lies only in [3, 11]

Input:L1 = 5, R1 = 10, L2 = 1, R2 = 7

Output:1 10

**Approach:**

- If
**L1 != L2**and**R1 != R2**then the points will be**min(L1, L2)**and**max(R1, R2)**. - Else only one point can be chosen from one of the ranges as one of the range is completely inside the other so we print
**-1**for that point.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the required points ` `void` `findPoints(` `int` `l1, ` `int` `r1, ` `int` `l2, ` `int` `r2) ` `{ ` ` ` ` ` `int` `x = (l1 != l2) ? min(l1, l2) : -1; ` ` ` `int` `y = (r1 != r2) ? max(r1, r2) : -1; ` ` ` `cout << x << ` `" "` `<< y; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `l1 = 5, r1 = 10, l2 = 1, r2 = 7; ` ` ` `findPoints(l1, r1, l2, r2); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to find the required points ` `static` `void` `findPoints(` `int` `l1, ` `int` `r1, ` ` ` `int` `l2, ` `int` `r2) ` `{ ` ` ` ` ` `int` `x = (l1 != l2) ? Math.min(l1, l2) : -` `1` `; ` ` ` `int` `y = (r1 != r2) ? Math.max(r1, r2) : -` `1` `; ` ` ` `System.out.println(x + ` `" "` `+ y); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `l1 = ` `5` `, r1 = ` `10` `, l2 = ` `1` `, r2 = ` `7` `; ` ` ` `findPoints(l1, r1, l2, r2); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 implementation of the approach ` ` ` `# Function to find the required points ` `def` `findPoints(l1, r1, l2, r2): ` ` ` ` ` `x ` `=` `min` `(l1, l2) ` `if` `(l1 !` `=` `l2) ` `else` `-` `1` ` ` `y ` `=` `max` `(r1, r2) ` `if` `(r1 !` `=` `r2) ` `else` `-` `1` ` ` `print` `(x, y) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `l1 ` `=` `5` ` ` `r1 ` `=` `10` ` ` `l2 ` `=` `1` ` ` `r2 ` `=` `7` ` ` `findPoints(l1, r1, l2, r2) ` ` ` `# This code is contributed by ita_c ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the required points ` ` ` `static` `void` `findPoints(` `int` `l1, ` `int` `r1, ` ` ` `int` `l2, ` `int` `r2) ` ` ` `{ ` ` ` `int` `x = (l1 != l2) ? Math.Min(l1, l2) : -1; ` ` ` `int` `y = (r1 != r2) ? Math.Max(r1, r2) : -1; ` ` ` `Console.WriteLine(x + ` `" "` `+ y); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `l1 = 5, r1 = 10, l2 = 1, r2 = 7; ` ` ` `findPoints(l1, r1, l2, r2); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to find the required points ` `function` `findPoints(` `$l1` `, ` `$r1` `, ` `$l2` `, ` `$r2` `) ` `{ ` ` ` ` ` `$x` `= (` `$l1` `!= ` `$l2` `) ? min(` `$l1` `, ` `$l2` `) : -1; ` ` ` `$y` `= (` `$r1` `!= ` `$r2` `) ? max(` `$r1` `, ` `$r2` `) : -1; ` ` ` `echo` `$x` `, ` `" "` `, ` `$y` `; ` `} ` ` ` `// Driver code ` `$l1` `= 5; ` `$r1` `= 10; ` `$l2` `= 1; ` `$r2` `= 7; ` `findPoints(` `$l1` `, ` `$r1` `, ` `$l2` `, ` `$r2` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1 10

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if the given point lies inside given N points of a Convex Polygon
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Minimum change in given value so that it lies in all given Ranges
- Check whether a given point lies inside a triangle or not
- How to check if a given point lies inside or outside a polygon?
- Find if a point lies inside a Circle
- Check whether a given point lies on or inside the rectangle | Set 3
- Find a point that lies inside exactly K given squares
- Check whether the point (x, y) lies on a given line
- Find two distinct numbers such that their LCM lies in given range
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Number of Integral Points between Two Points
- Prime points (Points that split a number into two primes)
- Maximum Squares possible parallel to both axes from N distinct points
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Maximum time such that absolute difference between hour and minute lies in given range
- Choose n elements such that their mean is maximum
- Choose X such that (A xor X) + (B xor X) is minimized
- Ways to choose balls such that at least one ball is chosen

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.