Let's say that on a given day she decides to select n people - that is, n boys and n girls. She gets the list of n boys and n girls in a random order initially. Then, she arranges the list of girls in ascending order on the basis of their height and boys in descending order of their heights. A girl Ai can be matched to a boy on the same index only, that is, Bi and no one else. Likewise, a girl standing on Ak can be only matched to a boy on the same index Bk and no one else.
Now to determine if the pair would make an ideal pair, she checks if the modulo of their heights is 0, i.e., Ai % Bi == 0 or Bi % Ai == 0. Given the number of boys and girls, and their respective heights in non-sorted order, determine the number of ideal pairs Mojo can find.
Example:
Input: n = 4, girl[n] = {1, 6, 9, 12}, boys[n] = {4, 12, 3, 9}
Output: 2
Approach
C++
#include <bits/stdc++.h>using namespace std;long long matchMakers(long long n,long long girl[],long long boys[]){sort(girl, girl + n);sort(boys, boys + n, greater<long long>());long long cnt = 0;for (long long i = 0; i < n; i++){if (boys[i] % girl[i] == 0 || girl[i] % boys[i] == 0)cnt++;}return cnt;}int main(){long long n = 4;long long girl[n] = {1, 6, 9, 12};long long boys[n] = {4, 12, 3, 9};cout << matchMakers(n, girl, boys) << "\n";return 0;}
No comments:
Post a Comment