Hiring Test codechef solution in c++

Here is a solution to the Hiring Test problem on CodeChef, implemented in C++:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    vector<int> b(m);
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }

    sort(a.begin(), a.end());

    int maxDiff = 0;
    int maxDiffIndex = -1;
    for (int i = 0; i < n; i++) {
        int diff = b[0] - a[i];
        if (diff > maxDiff) {
            maxDiff = diff;
            maxDiffIndex = i;
        }
    }

    cout << a[maxDiffIndex] << " " << b[0] << endl;

    return 0;
}

Explanation:

  1. We start by including necessary libraries for our program: iostream, vector, and algorithm. These libraries provide us with input/output operations, dynamic arrays (vectors), and the sort function, respectively.

  2. We declare two variables, n and m, to store the number of elements in the arrays a and b, respectively. We read these values from the standard input using the cin object.

  3. We declare a vector a of size n to store the elements of array a, and then we read the elements of a from the standard input using a loop.

  4. Similarly, we declare a vector b of size m to store the elements of array b, and then we read the elements of b from the standard input using a loop.

  5. We sort the vector a in ascending order using the sort function from the algorithm library. This will help us find the element in a that is closest to the smallest element in b.

  6. We declare two variables, maxDiff and maxDiffIndex, to keep track of the maximum difference and the index of the corresponding element in a. We initialize maxDiff to 0 and maxDiffIndex to -1.

  7. We iterate through each element in a using a loop. For each element, we calculate the difference between the smallest element in b and the current element in a. If this difference is greater than the current maximum difference (maxDiff), we update maxDiff and maxDiffIndex accordingly.

  8. Finally, we print the element in a at index maxDiffIndex and the smallest element in b as the output.

  9. We return 0 to indicate successful execution of the program.

Note: This solution finds the element in a that is closest to the smallest element in b, based on the difference between the two.