Given an array of pairs arr[] of size N, the task is to find any two pairs (a, b) and (c, d) such that a < c and b > d always holds. If any such pair exists, print those pairs. Otherwise, print “NO SUCH PAIR EXISTS“.
Examples:
Input: arr[] = {(3, 7), (21, 23), (4, 13), (1, 2), (7, -1)}
Output: Required pairs are (3, 7), (7, -1)
Explanation: (a, b) = (3, 7)
(c, d) = (7, -1)
Clearly, a < c and b > d.Input: arr[]={(1, 6), (-5, 4), (10, 13)}
Output: NO SUCH PAIR EXIST
Naive Approach: The simplest approach to solve this problem is to check for every pair in the array if there exists any other pair which meets the given condition.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find two pairs (a, b) and // (c, d) such that a < c and b > d void findPair(pair< int , int >* arr, int N) { for ( int i = 0; i < N; i++) { int a = arr[i].first, b = arr[i].second; for ( int j = i + 1; j < N; j++) { int c = arr[j].first, d = arr[j].second; if (a < c && b > d) { cout << "(" << a << " " << b << "), (" << c << " " << d << ")\n" ; return ; } } } // If no such pair is found cout << "NO SUCH PAIR EXIST\n" ; } // Driver Code int main() { pair< int , int > arr[] = { { 3, 7 }, { 21, 23 }, { 4, 13 }, { 1, 2 }, { 7, -1 } }; findPair(arr, 5); } |
Java
// Java implementation to sort the // array of points by its distance // from the given point import java.util.*; class GFG { static class pair { int first, second; public pair( int first, int second) { this .first = first; this .second = second; } } // Function to find two pairs (a, b) and // (c, d) such that a < c and b > d static void findPair(pair arr[], int N) { for ( int i = 0 ; i < N; i++) { int a = arr[i].first, b = arr[i].second; for ( int j = i + 1 ; j < N; j++) { int c = arr[j].first, d = arr[j].second; if (a < c && b > d) { System.out.println( "(" + a + " " + b + "), (" + c + " " + d + ")" ); return ; } } } // If no such pair is found System.out.println( "NO SUCH PAIR EXIST" ); } // Driver code public static void main(String[] args) { pair arr[] = { new pair( 3 , 7 ), new pair( 21 , 23 ), new pair( 4 , 13 ), new pair( 1 , 2 ), new pair( 7 , - 1 )}; findPair(arr, 5 ); } } // This code is contributed by sanjoy_62. |
Python3
# Python3 program for the above approach # Function to find two pairs (a, b) and # (c, d) such that a < c and b > d def findPair(arr, N): for i in range (N): a, b = arr[i][ 0 ], arr[i][ 1 ] for j in range (i + 1 , N): c, d = arr[j][ 0 ], arr[j][ 1 ] if (a < c and b > d): print ( "(" , a, b, "), (" , c, d, ")" ) return # If no such pair is found print ( "NO SUCH PAIR EXIST" ) # Driver Code if __name__ = = '__main__' : arr = [ [ 3 , 7 ], [ 21 , 23 ], [ 4 , 13 ], [ 1 , 2 ], [ 7 , - 1 ] ] findPair(arr, 5 ) # This code is contributed by mohit kumar 29 |
C#
// C# implementation to sort the // array of points by its distance // from the given point using System; public class GFG { class pair { public int first, second; public pair( int first, int second) { this .first = first; this .second = second; } } // Function to find two pairs (a, b) and // (c, d) such that a < c and b > d static void findPair(pair []arr, int N) { for ( int i = 0; i < N; i++) { int a = arr[i].first, b = arr[i].second; for ( int j = i + 1; j < N; j++) { int c = arr[j].first, d = arr[j].second; if (a < c && b > d) { Console.WriteLine( "(" + a + " " + b + "), (" + c + " " + d + ")" ); return ; } } } // If no such pair is found Console.WriteLine( "NO SUCH PAIR EXIST" ); } // Driver code public static void Main(String[] args) { pair []arr = { new pair( 3, 7 ), new pair( 21, 23 ), new pair( 4, 13 ), new pair( 1, 2 ), new pair( 7, -1 )}; findPair(arr, 5); } } // This code is contributed by 29AjayKumar |
(3 7), (7 -1)
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea si to solve the problem:
- Sort the given array according to the first element of each pair.
- Now, the task reduces to check if there exists any pair in arr[] where arr[i].second < arr[i – 1].second as the first element of each pair is already sorted.
- So, simply traverse the array and check if there exists such a pair.
- If found to be true, print the pair.
- Otherwise, print “NO SUCH PAIR EXIST”.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find two pairs (a, b) and // (c, d) such that a < c and b > d void findPair(pair< int , int >* arr, int N) { // Sort the array in increasing // order of first element of pairs sort(arr, arr + N); // Traverse the array for ( int i = 1; i < N; i++) { int b = arr[i - 1].second; int d = arr[i].second; if (b > d) { cout << "(" << arr[i - 1].first << " " << b << "), (" << arr[i].first << " " << d << ")" ; return ; } } // If no such pair found cout << "NO SUCH PAIR EXIST\n" ; } // Driver Code int main() { pair< int , int > arr[] = { { 3, 7 }, { 21, 23 }, { 4, 13 }, { 1, 2 }, { 7, -1 } }; findPair(arr, 5); } |
Java
// Java program for the above approach import java.util.*; class GFG { static class pair implements Comparable<pair> { int first, second; public pair( int first, int second) { this .first = first; this .second = second; } public int compareTo(pair p) { return this .first - p.first; } } // Function to find two pairs (a, b) and // (c, d) such that a < c and b > d static void findpair(pair []arr, int N) { // Sort the array in increasing // order of first element of pairs Arrays.sort(arr); // Traverse the array for ( int i = 1 ; i < N; i++) { int b = arr[i - 1 ].second; int d = arr[i].second; if (b > d) { System.out.print( "(" + arr[i - 1 ].first + " " + b + "), (" + arr[i].first + " " + d + ")" ); return ; } } // If no such pair found System.out.print( "NO SUCH PAIR EXIST\n" ); } // Driver Code public static void main(String[] args) { pair arr[] = { new pair( 3 , 7 ), new pair( 21 , 23 ), new pair( 4 , 13 ), new pair( 1 , 2 ), new pair( 7 , - 1 ) }; findpair(arr, 5 ); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program for the above approach # Function to find two pairs (a, b) and # (c, d) such that a < c and b > d def findPair(arr, N): # Sort the array in increasing # order of first element of pairs arr.sort(key = lambda x: x[ 0 ]) # Traverse the array for i in range ( 1 , N): b = arr[i - 1 ][ 1 ] d = arr[i][ 1 ] if (b > d): print ( "(" , arr[i - 1 ][ 0 ], b, "), (" , arr[i][ 0 ], d, ")" ) return #If no such pair found print ( "NO SUCH PAIR EXIST\n" ); # Driver Code arr = [ [ 3 , 7 ], [ 21 , 23 ], [ 4 , 13 ], [ 1 , 2 ], [ 7 , - 1 ]] findPair(arr, 5 ) # This code is contributed by Dharanendra L V |
C#
// C# program for the above approach using System; public class GFG { class pair : IComparable<pair> { public int first, second; public pair( int first, int second) { this .first = first; this .second = second; } public int CompareTo(pair p) { return this .first - p.first; } } // Function to find two pairs (a, b) and // (c, d) such that a < c and b > d static void findpair(pair []arr, int N) { // Sort the array in increasing // order of first element of pairs Array.Sort(arr); // Traverse the array for ( int i = 1; i < N; i++) { int b = arr[i - 1].second; int d = arr[i].second; if (b > d) { Console.Write( "(" + arr[i - 1].first + " " + b + "), (" + arr[i].first + " " + d + ")" ); return ; } } // If no such pair found Console.Write( "NO SUCH PAIR EXIST\n" ); } // Driver Code public static void Main(String[] args) { pair []arr = { new pair( 3, 7 ), new pair( 21, 23 ), new pair( 4, 13 ), new pair( 1, 2 ), new pair( 7, -1 ) }; findpair(arr, 5); } } // This code is contributed by 29AjayKumar |
(4 13), (7 -1)
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
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.