Given an integer **N**, a binary string **S** and an array** W[]**. **S **denotes the sequence of **N * 2** persons entering the hall, where **0** denotes a boy and **1** denotes a girl. **W[]** denotes the width of seats in each row, where each row consists of exactly **2** seats. The task is to find a possible arrangement of the persons entering the hall such that following conditions are satisfied:

- A boy chooses a row in which both seats are empty. Among all such rows, he chooses the one with the minimum width.
- A girl chooses a row in which one seat is occupied by a boy. Among all such rows, she chooses the one with the maximum width.

**Examples:**

Input:N = 3, W[] = {2, 1, 3}, S = “001011”Output:2 1 1 3 3 2Explanation:

People enter the hall in the following way:Person 1:First person is a boy, and all the seats are vacant at this time. So, he can select any row and take a seat. Among all rows, the one with minimum width of seat is 2nd. So, the first person sits on a seat in the 2nd row.Person 2:Second person is also a boy, and now 2 seats are vacant with width of seats 2 & 3. So, he takes the seat in row 1 as it has minimum width of seats.Person 3:Third one is a girl, and she can sit in a row in which one seat is already occupied, i.e, row 1 and row 2. Among these rows, the girl chooses to sit in the row with maximum width, so she sits in row 1.Person 4:Fourth one is a boy, and he can sit only in row 3, as only 3rd row is vacant now.Person 5:Fifth one is a girl, and she can sit in row 2 or row 3, which have one of the seats occupied. So she chooses row 3 as it has a seat with width more than that in row 2.Person 6:Finally a girl comes, and she can take a seat only in row 2.

Input:N = 3, W[] = {1, 3, 2}, S = “010011”Output:1 1 3 2 2 3

**Naive Approach:** The simplest approach is to take an array of pairs of size **N** and mark it occupied on every boy’s entry into the bus if both pair values are marked vacant and have the minimum width. Similarly, mark occupied on girl’s entry only when a single value is marked occupied and having maximum width.

**Time Complexity:** O(N^{2})**Auxiliary Space:** O(N)

**Efficient Approach:** To optimize the above approach, the idea is to use a priority queue. Follow the steps below too solve the problem:

- Sort the given array
**arr[]**of the width of seats in ascending order. - Create a priority queue and keep an index variable to keep track of the number of rows occupied by a boy in
**arr[]**. - Create a vector
**ans[]**to store the result. - Iterate through each character of the string
**s**and if**s[i]**is**0**, then push the index of**arr[ind]**in the**arr[ind]**into the queue. - Otherwise, pop the top element of the queue, and then push the index of this element in the vector.
- After the above steps. print the value stored in
**ans[]**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the arrangement` `// of seating` `void` `findTheOrder(` `int` `arr[], string s,` ` ` `int` `N)` `{` ` ` `// Stores the row in which the` ` ` `// ith person sits` ` ` `vector<` `int` `> ans;` ` ` `// Stores the width of seats along` ` ` `// with their index or row number` ` ` `pair<` `int` `, ` `int` `> A[N];` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `A[i] = { arr[i], i + 1 };` ` ` `// Sort the array` ` ` `sort(A, A + N);` ` ` `// Store the seats and row for` ` ` `// boy's seat` ` ` `priority_queue<pair<` `int` `, ` `int` `> > q;` ` ` `// Stores the index of row upto` ` ` `// which boys have taken seat` ` ` `int` `index = 0;` ` ` `// Iterate the string` ` ` `for` `(` `int` `i = 0; i < 2 * N; i++) {` ` ` `if` `(s[i] == ` `'0'` `) {` ` ` `// Push the row number at` ` ` `// index in vector and heap` ` ` `ans.push_back(A[index].second);` ` ` `q.push(A[index]);` ` ` `// Increment the index to let` ` ` `// the next boy in the next` ` ` `// minimum width vacant row` ` ` `index++;` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `// If girl then take top of` ` ` `// element of the max heap` ` ` `ans.push_back(q.top().second);` ` ` `// Pop from queue` ` ` `q.pop();` ` ` `}` ` ` `}` ` ` `// Print the values` ` ` `for` `(` `auto` `i : ans) {` ` ` `cout << i << ` `" "` `;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given N` ` ` `int` `N = 3;` ` ` `// Given arr[]` ` ` `int` `arr[] = { 2, 1, 3 };` ` ` `// Given string` ` ` `string s = ` `"001011"` `;` ` ` `// Function Call` ` ` `findTheOrder(arr, s, N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.util.*;` `class` `Pair ` `{` ` ` `int` `first, second;` ` ` ` ` `Pair(` `int` `first, ` `int` `second)` ` ` `{` ` ` `this` `.first = first;` ` ` `this` `.second = second;` ` ` `}` `}` `class` `GFG{` `// Function to find the arrangement` `// of seating` `static` `void` `findTheOrder(` `int` `arr[], String s, ` ` ` `int` `N)` `{` ` ` ` ` `// Stores the row in which the` ` ` `// ith person sits` ` ` `List<Integer> ans = ` `new` `ArrayList<Integer>();` ` ` `// Stores the width of seats along` ` ` `// with their index or row number` ` ` `List<Pair> A = ` `new` `ArrayList<Pair>();` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++)` ` ` `{` ` ` `Pair p = ` `new` `Pair(arr[i], i + ` `1` `);` ` ` `A.add(p);` ` ` `}` ` ` `// Sort the array` ` ` `Collections.sort(A, (p1, p2) -> p1.first -` ` ` `p2.first);` ` ` `// Store the seats and row for` ` ` `// boy's seat` ` ` `PriorityQueue<Pair> q = ` `new` `PriorityQueue<Pair>(` ` ` `N, (p1, p2) -> p2.first - p1.first);` ` ` `// Stores the index of row upto` ` ` `// which boys have taken seat` ` ` `int` `index = ` `0` `;` ` ` `// Iterate the string` ` ` `for` `(` `int` `i = ` `0` `; i < ` `2` `* N; i++)` ` ` `{` ` ` `if` `(s.charAt(i) == ` `'0'` `)` ` ` `{` ` ` ` ` `// Push the row number at` ` ` `// index in vector and heap` ` ` `ans.add(A.get(index).second);` ` ` `q.add(A.get(index));` ` ` `// Increment the index to let` ` ` `// the next boy in the next` ` ` `// minimum width vacant row` ` ` `index++;` ` ` `}` ` ` `// Otherwise` ` ` `else` ` ` `{` ` ` ` ` `// If girl then take top of` ` ` `// element of the max heap` ` ` `ans.add(q.peek().second);` ` ` `// Pop from queue` ` ` `q.poll();` ` ` `}` ` ` `}` ` ` `// Print the values` ` ` `for` `(` `int` `i : ans)` ` ` `{` ` ` `System.out.print(i + ` `" "` `);` ` ` `}` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Given N` ` ` `int` `N = ` `3` `;` ` ` `// Given arr[]` ` ` `int` `arr[] = { ` `2` `, ` `1` `, ` `3` `};` ` ` `// Given string` ` ` `String s = ` `"001011"` `;` ` ` `// Function Call` ` ` `findTheOrder(arr, s, N);` `}` `}` `// This code is contributed by jithin` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to find the arrangement` `# of seating` `def` `findTheOrder(arr, s, N):` ` ` ` ` `# Stores the row in which the` ` ` `# ith person sits` ` ` `ans ` `=` `[]` ` ` `# Stores the width of seats along` ` ` `# with their index or row number` ` ` `A ` `=` `[[] ` `for` `i ` `in` `range` `(N)]` ` ` `for` `i ` `in` `range` `(N):` ` ` `A[i] ` `=` `[arr[i], i ` `+` `1` `]` ` ` `# Sort the array` ` ` `A ` `=` `sorted` `(A)` ` ` `# Store the seats and row for` ` ` `# boy's seat` ` ` `q ` `=` `[]` ` ` `# Stores the index of row upto` ` ` `# which boys have taken seat` ` ` `index ` `=` `0` ` ` `# Iterate the string` ` ` `for` `i ` `in` `range` `(` `2` `*` `N):` ` ` `if` `(s[i] ` `=` `=` `'0'` `):` ` ` `# Push the row number at` ` ` `# index in vector and heap` ` ` `ans.append(A[index][` `1` `])` ` ` `q.append(A[index])` ` ` `# Increment the index to let` ` ` `# the next boy in the next` ` ` `# minimum width vacant row` ` ` `index ` `+` `=` `1` ` ` `# Otherwise` ` ` `else` `:` ` ` `# If girl then take top of` ` ` `# element of the max heap` ` ` `ans.append(q[` `-` `1` `][` `1` `])` ` ` `# Pop from queue` ` ` `del` `q[` `-` `1` `]` ` ` ` ` `q ` `=` `sorted` `(q)` ` ` `# Print the values` ` ` `for` `i ` `in` `ans:` ` ` `print` `(i, end ` `=` `" "` `)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given N` ` ` `N ` `=` `3` ` ` `# Given arr[]` ` ` `arr ` `=` `[ ` `2` `, ` `1` `, ` `3` `]` ` ` `# Given string` ` ` `s ` `=` `"001011"` ` ` `# Function Call` ` ` `findTheOrder(arr, s, N)` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

**Output:**

2 1 1 3 3 2

**Time Complexity:** O(N * log N)**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.