Choose X elements from A[] and Y elements from B[] which satisfy the given condition
Given two arrays A[] and B[] and two integers X and Y, the task is to choose X elements from A[] and Y elements from B[] such that all the chosen elements from A[] are less than all the chosen elements from B[]
Examples:
Input: A[] = {1, 1, 1, 1, 1}, B[] = {3, 1}, X = 3, Y = 1
Output: Yes
Choose {1, 1, 1} from A[] and {3} from B[].
Input: A[] = {5, 4}, B[] = {2, 3, 2, 2}, X = 2, Y = 1
Output: No
Approach: In order to satisfy the given conditions, the minimum X elements have to be chosen from A[] and the maximum Y elements have to be chosen from B[]. This can be done by sorting both the arrays and then choosing the Xth smallest element from A[] say xSmall and Yth largest element from B[] say yLarge.
This is because if xSmall is smaller than yLarge then all the elements smaller than it will definitely be smaller than yLarge and all the elements larger than yLarge will definitely be greater than xSmall.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to that returns true if // it possible to choose the elements bool isPossible(int A[], int B[], int n, int m, int x, int y) { // If elements can't be chosen if (x > n || y > m) return false; // Sort both the arrays sort(A, A + n); sort(B, B + m); // If xth smallest element of A[] // is smaller than the yth // greatest element of B[] if (A[x - 1] < B[m - y]) return true; else return false; } // Driver code int main() { int A[] = { 1, 1, 1, 1, 1 }; int B[] = { 2, 2 }; int n = sizeof(A) / sizeof(int); int m = sizeof(B) / sizeof(int); int x = 3, y = 1; if (isPossible(A, B, n, m, x, y)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation of the above approach import java.util.*; class GFG { // Function to that returns true if // it possible to choose the elements static boolean isPossible(int A[], int B[], int n, int m, int x, int y) { // If elements can't be chosen if (x > n || y > m) return false; // Sort both the arrays Arrays.sort(A); Arrays.sort(B); // If xth smallest element of A[] // is smaller than the yth // greatest element of B[] if (A[x - 1] < B[m - y]) return true; else return false; } // Driver code public static void main (String[] args) { int A[] = { 1, 1, 1, 1, 1 }; int B[] = { 2, 2 }; int n = A.length; int m = B.length;; int x = 3, y = 1; if (isPossible(A, B, n, m, x, y)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Function to that returns true if # it possible to choose the elements def isPossible(A, B, n, m, x, y) : # If elements can't be chosen if (x > n or y > m) : return False # Sort both the arrays A.sort() B.sort() # If xth smallest element of A[] # is smaller than the yth # greatest element of B[] if (A[x - 1] < B[m - y]) : return True else : return False # Driver code A = [ 1, 1, 1, 1, 1 ] B = [ 2, 2 ] n = len(A) m = len(B) x = 3y = 1 if (isPossible(A, B, n, m, x, y)): print("Yes") else: print("No") # This code is contributed by # divyamohan123 |
C#
// C# implementation of the above approach using System; class GFG { // Function to that returns true if // it possible to choose the elements static bool isPossible(int []A, int []B, int n, int m, int x, int y) { // If elements can't be chosen if (x > n || y > m) return false; // Sort both the arrays Array.Sort(A); Array.Sort(B); // If xth smallest element of A[] // is smaller than the yth // greatest element of B[] if (A[x - 1] < B[m - y]) return true; else return false; } // Driver code public static void Main (String[] args) { int []A = { 1, 1, 1, 1, 1 }; int []B = { 2, 2 }; int n = A.Length; int m = B.Length;; int x = 3, y = 1; if (isPossible(A, B, n, m, x, y)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Rajput-Ji |
Yes
Recommended Posts:
- Generate an array of K elements such that sum of elements is N and the condition a[i] < a[i+1] <= 2*a[i] is met | Set 2
- Choose n elements such that their mean is maximum
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Choose atleast two elements from array such that their GCD is 1 and cost is minimum
- Number of ways to choose elements from the array such that their average is K
- Remove elements to make array satisfy arr[ i+1] < arr[i] for each valid i
- Choose k array elements such that difference of maximum and minimum is minimized
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array
- Count sub-sets that satisfy the given condition
- Pairs from an array that satisfy the given condition
- Count index pairs which satisfy the given condition
- Check if elements of an array can be arranged satisfying the given condition
- Sum of absolute differences of pairs from the given array that satisfy the given condition
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
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.



