Bharati Vidyapeeth ballroom dance

The Bharati Vidyapeeth Delhi engineering college is hosting a ballroom dance in celebration of its 18 anniversary! n boys and m girls are already busy rehearsing dance moves.

We know that several boy & girl pairs are going to be invited to the ball. However, the partners' dancing skills in each pair must differ by at most one.

For each boy, we know his dancing skills. Similarly, for each girl, we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys and m girls.

Example:

Input:  n = 4, a = [1,4,6,2], m = 5, b = [5,1,5,7,9]
Output: 3

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int ballroomDance(int nint a[], int mint b[])
{
    bool visited[m] = {false};
    int cnt = 0;
    sort(aa + n);
    sort(bb + m);
    for (int i = 0i < ni++)
    {
        for (int j = 0j < mj++)
        {
            int x = abs(a[i] - b[j]);
            if (visited[j] == false && x <= 1)
            {
                visited[j] = true;
                cnt++;

                break;
            }
        }
    }
    return cnt;
}
int main()
{
    int n = 4;
    int a[n] = {1462};

    int m = 5;
    int b[m] = {51579};

    cout << ballroomDance(namb<< "\n";

    return 0;
}


No comments:

Post a Comment