Restore a shuffled Queue as per given Conditions
Last Updated :
01 Mar, 2023
Given N people standing in a queue and two arrays A[] and B[]. The array A[] represent the name of the person and array B[] represents how many people are taller than a particular person standing in front of that. Now the queue is shuffled. The task is to print the original sequence of the queue following the above property.
Examples:
Input: N = 4, A[] = {‘a’, ‘b’, ‘c’, ‘d’}, B[] = {0, 2, 0, 0}
Output:
a 1
c 3
d 4
b 2
Explanation:
Looking at the output queue and their generated heights, it can be easily understood that:
1) a is the first one in the queue and so we have the person with 0th index in front of him. So a is associated with 0 in the input.
2) c has only a in front of him/her but a is shorter than c. Therefore c is associated with 0 in the input.
3) d has c and a in front of him/her but they are both shorter than d . Therefore d is associated with 0 in the input.
4) b has d, c and a in front of b. But only c and d are taller than b. So, b is associated with 2 in the input.
Input: N = 4, A[] = { ‘a’, ‘b’, ‘c’, ‘d’}, B[] = { 0, 1, 3, 3}
Output: -1
Explanation:
The given order is the original order of the queue.
Approach:
- Firstly make a pair of the person’s name and their associated integers and sort the pairs.
- Create an array answer[] to store the possible heights of the person.
- Iterate over all the pair and if the number of persons standing in front of is taller and is greater than their current standing position, then return -1.
- Otherwise, store the difference between the current standing position and the height of the person taller than him, in the answer array.
- For every person iterate over the pair and if the value of the answer array for our current person is greater than the person with whom we are comparing, increment in the answer array for current pair.
- Finally, print the possible pairs from the given sequence according to values stored in answer[] array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void OriginalQueue( char A[], int B[],
int N)
{
pair< int , string> a[N + 1];
int ans[N + 1];
bool possible = true ;
for ( int i = 0; i < N; i++) {
a[i].second = A[i];
a[i].first = B[i];
}
sort(a, a + N);
for ( int i = 0; i < N; i++) {
int len = i - a[i].first;
if (len < 0) {
cout << "-1" ;
possible = false ;
}
if (!possible)
break ;
else {
ans[i] = len;
for ( int j = 0; j < i; j++) {
if (ans[j] >= ans[i])
ans[j]++;
}
}
if (i == N - 1 && possible) {
for ( int i = 0; i < N; i++) {
cout << a[i].second << " "
<< ans[i] + 1 << endl;
}
}
}
}
int main()
{
int N = 4;
char A[N] = { 'a' , 'b' , 'c' , 'd' };
int B[N] = { 0, 2, 0, 0 };
OriginalQueue(A, B, N);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static void OriginalQueue( char A[], int B[],
int N)
{
int [][] a = new int [N][ 2 ];
int [] ans = new int [N];
boolean possible = true ;
for ( int i = 0 ; i < N; i++)
{
a[i][ 0 ] = B[i];
a[i][ 1 ] = ( int )A[i];
}
Arrays.sort(a, (o1, o2) -> o1[ 0 ] - o2[ 0 ]);
for ( int i = 0 ; i < N; i++)
{
int len = i - a[i][ 0 ];
if (len < 0 )
{
System.out.print( "-1" );
possible = false ;
}
if (!possible)
break ;
else
{
ans[i] = len;
for ( int j = 0 ; j < i; j++)
{
if (ans[j] >= ans[i])
ans[j]++;
}
}
if (i == N - 1 && possible)
{
for ( int k = 0 ; k < N; k++)
{
System.out.println(( char )a[k][ 1 ] +
" " + (ans[k] + 1 ));
}
}
}
}
public static void main (String[] args)
{
int N = 4 ;
char A[] = { 'a' , 'b' , 'c' , 'd' };
int B[] = { 0 , 2 , 0 , 0 };
OriginalQueue(A, B, N);
}
}
|
Python3
def OriginalQueue(A, B, N):
a = [[ 0 , ""] for i in range (N)]
ans = [ 0 for i in range (N)]
possible = True
for i in range (N):
a[i][ 1 ] = str (A[i])
a[i][ 0 ] = B[i]
a.sort(reverse = False )
for i in range (N):
len1 = i - a[i][ 0 ]
if (len1 < 0 ):
print ( "-1" ,end = "")
possible = False
if (possible = = False ):
break
else :
ans[i] = len1
for j in range (i):
if (ans[j] > = ans[i]):
ans[j] + = 1
if (i = = N - 1 and possible):
for i in range (N):
print (a[i][ 1 ], ans[i] + 1 )
if __name__ = = '__main__' :
N = 4
A = [ 'a' , 'b' , 'c' , 'd' ]
B = [ 0 , 2 , 0 , 0 ]
OriginalQueue(A, B, N)
|
C#
using System;
using System.Linq;
public class GFG {
static void OriginalQueue( char [] A, int [] B, int N) {
int [][] a = new int [N][];
for ( int i = 0; i < N; i++) {
a[i] = new int [] {B[i], ( int )A[i]};
}
int [] ans = new int [N];
bool possible = true ;
Array.Sort(a, (o1, o2) => o1[0] - o2[0]);
for ( int i = 0; i < N; i++) {
int len = i - a[i][0];
if (len < 0) {
Console.Write( "-1" );
possible = false ;
}
if (!possible)
break ;
else {
ans[i] = len;
for ( int j = 0; j < i; j++) {
if (ans[j] >= ans[i])
ans[j]++;
}
}
if (i == N - 1 && possible) {
for ( int k = 0; k < N; k++) {
Console.WriteLine(( char )a[k][1] + " " + (ans[k] + 1));
}
}
}
}
public static void Main( string [] args) {
int N = 4;
char [] A = { 'a' , 'b' , 'c' , 'd' };
int [] B = {0, 2, 0, 0};
OriginalQueue(A, B, N);
}
}
|
Javascript
<script>
function OriginalQueue(A, B, N)
{
var a = Array(N + 1);
for ( var i = 0; i < N; i++)
{
a[i] = [0, "" ];
}
var ans = Array(N + 1);
var possible = true ;
for ( var i = 0; i < N; i++)
{
a[i][1] = A[i];
a[i][0] = B[i];
}
a.sort()
for ( var i = 0; i < N; i++)
{
var len = i - a[i][0];
if (len < 0)
{
document.write( "-1" );
possible = false ;
}
if (!possible)
break ;
else
{
ans[i] = len;
for ( var j = 0; j < i; j++)
{
if (ans[j] >= ans[i])
ans[j]++;
}
}
if (i == N - 1 && possible)
{
for ( var i = 0; i < N; i++)
{
document.write(a[i][1] + " " +
(ans[i] + 1) + "<br>" );
}
}
}
}
var N = 4;
var A = [ 'a' , 'b' , 'c' , 'd' ];
var B = [ 0, 2, 0, 0 ];
OriginalQueue(A, B, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...