Restore a Shuffled Queue as Per given Conditions


In this problem, we have given the persona names and the number of taller people standing in front of that people.

We can sort the position of the people according to the number of taller people standing in front of any person. After that, we update the position of each person according to the nums[] array value to get the original queue.

Problem statement − We have given an array persons[] and nums[]. The persons[] array contains the person's name, and the nums[] array contains the number of taller people standing in front of each person. This queue is shuffled, and we need to find the original queue.

Sample examples

Input

persons[N] = {"pq", "rs", "tu", "vw"}; nums[N] = {0, 1, 0, 0};

Output

pq 1, tu 2, vw 4, rs 3

Explanation − The ‘pq’ is the shortest person. Also, ‘tu’ has 1 taller person standing in front of him.

Input

persons[N] = {"pq", "rs", "tu", "vw"}; nums[N] = {0, 2, 3, 3};

Output

-1

Explanation − It is not possible to reshuffle the queue.

Approach

In this approach, we will sort the pair of person names and the number of taller people standing before each person. After that, we will update the position of each person to get the updated queue.

Algorithm

Step 1 − Define the ‘pairs[]’ array to store the pair of person names and given numeric values.

Step 2 − Start traversing the persons[] and nums[] array and insert them into the pairs array.

Step 3 − Use the sort() method to sort the pairs[] array.

Step 4 − Also, define the ‘res’ list, and start traversing the pairs[] array.

Step 5 − If p − pairs[p].first is less than 0, print −1, as getting the original queue is impossible.

Step 6 − Otherwise, update the res[p] with temp.

Step 7 − Now, we need to update the position of the person in the res[q] array. So, start traversing the ‘res’ array.

Step 8 − Use the nested loop to traverse the res array from 0 to q index.

Step 9 − If res[q] is greater than or equal to res[p], increment the res[q] by 1.

Step 10 − Print the position of each person.

Example

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

void FindOriginalQueue(string persons[], int nums[], int N) {
    pair<int, string> pairs[N];
    int res[N];
    // Insert elements to pairs[] array
    for (int p = 0; p < N; p++) {
        pairs[p].first = nums[p];
        pairs[p].second = persons[p];
    }
    // Sort the pairs[] array
    sort(pairs, pairs + N);
    // Calculate the temporary positions and check for validity
    for (int p = 0; p < N; p++) {
        int temp = p - pairs[p].first;
        // Not possible to create the original queue.
        if (temp < 0) {
            cout << "It is not possible to create the original queue" << endl;
            return;
        }
        res[p] = temp;
    }
    // Update the temporary positions to handle multiple people with the same number of taller people in front
    for (int p = 0; p < N; p++) {
        for (int q = 0; q < p; q++) {
            if (res[q] >= res[p])
                res[q]++;
        }
    }
    cout << "The original queue is " << endl;
    for (int p = 0; p < N; p++) {
        cout << pairs[p].second << " " << res[p] + 1 << endl;
    }
}
int main() {
    int N = 4;
    // Person names
    string persons[N] = {"pq", "rs", "tu", "vw"};
    // Number of taller people
    int nums[N] = {0, 1, 0, 0};
    FindOriginalQueue(persons, nums, N);
    return 0;
}

Output

The original queue is

pq 1
tu 2
vw 4
rs 3

Time complexity − O(N*N) for traversing ‘res’ array.

Space complexity − O(N) to store array elements in pairs array.

Updated on: 02-Aug-2023

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements