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); } |
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 |
Python3
# 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 |
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 |
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 ?> |
Javascript
<script> Â
// Javascript implementation of the approach   Â
// Function to find the required points function findPoints(l1 , r1 , l2 , r2) { Â Â Â Â var x = (l1 != l2) ? Math.min(l1, l2) : -1; Â Â Â Â var y = (r1 != r2) ? Math.max(r1, r2) : -1; Â Â Â Â document.write(x + " " + y); } Â
// Driver code var l1 = 5, r1 = 10, l2 = 1, r2 = 7; Â
findPoints(l1, r1, l2, r2); Â
// This code is contributed by Rajput-Ji Â
</script> |
1 10
Â
Time Complexity : O(1), since there is only a basic arithmetic operation that takes constant time.
Auxiliary Space : O(1), since no extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!