Question: 9.2 Programming Assignment #2 Sorting Riverfield Country Day School will be hosting its Prom Night tonight.There would be M boys and N girls at the
9.2 Programming Assignment #2
Sorting
Riverfield Country Day School will be hosting its Prom Night tonight.There would be M boys and N girls at the prom tonight. Each boy wants to dance with a girl who is strictly shorter than him. A girl can dance with only one boy and vice-versa. Given the heights of all the boys and girls tell whether it is possible for all boys to dance with a girl.
Input: The first line contains T denoting the number of test cases. Each test case contains three lines. The first line contains M and N. The second line contains M integers each denoting the height of boy. The third contains N integers each denoting the height of girl.
Output: Print YES if it is possible for each boy to dance with a girl else print NO.
Sample input:
1 4 5 2 5 6 8 3 8 5 1 7
Output from sample input:
YES
Note :- please help to fix the code it working but its showing half point
showing special character legend for output YES and NO.
#include
int main() { int t; cin >> t; //number of tests while(t--){ //loop for each test int m, n; cin >> m >> n; //read number of boys and girls
vector
for(int i = 0; i < m; i++) //read heights of boys cin >> boys[i];
for(int i = 0; i < n; i++) //read heights of girls cin >> girls[i];
sort(boys.begin(), boys.end()); //sort heights of boys sort(girls.begin(), girls.end()); //sort heights of girls
//match first least heighted boy with first least heighted girl if girls[0] < boys[0] //match second least heighted boy with second least heighted girl if girls[1] < boys[1] //and so on....
int b = 0, g = 0; while(b <= g && b < m && g < n){ if(girls[g] < boys[b]){ g++; //if match found, move to next boy and girl b++; } else{ break; //match not found, so he cannot be matched further } } if(b == m) //all boys matched cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
