BackmediumArrays PhonePe

Rearranging Theater Seating by Seat Popularity Solution

Problem Statement

Rearrange the theater seating arrangement such that the seats with the highest popularity ratings are together, with the most popular seats at the center of the auditorium, followed by the less popular seats towards the sides.

Example 1
Input
[{'seat': 1, 'rating': 5}, {'seat': 2, 'rating': 3}, {'seat': 3, 'rating': 2}, {'seat': 4, 'rating': 1}, {'seat': 5, 'rating': 4}]
Output
[{'seat': 1, 'rating': 5}, {'seat': 3, 'rating': 2}, {'seat': 2, 'rating': 3}, {'seat': 5, 'rating': 4}, {'seat': 4, 'rating': 1}]

Explanation: Step-by-step: Sort the seats by rating in descending order. Then, rearrange the seats so that the seats at odd indices are towards the center and the seats at even indices are towards the sides.

Example 2
Input
[{'seat': 1, 'rating': 4}, {'seat': 2, 'rating': 1}, {'seat': 3, 'rating': 5}, {'seat': 4, 'rating': 2}, {'seat': 5, 'rating': 3}]
Output
[{'seat': 3, 'rating': 5}, {'seat': 1, 'rating': 4}, {'seat': 2, 'rating': 1}, {'seat': 5, 'rating': 3}, {'seat': 4, 'rating': 2}]

Explanation: Step-by-step: Sort the seats by rating in descending order. Then, rearrange the seats so that the seats at odd indices are towards the center and the seats at even indices are towards the sides.

Constraints

  • The input array is never empty
  • The total number of seats is within the range 2 <= n <= 2*10^5
  • All seats in the input array are unique, and the ratings are guaranteed to be non-negative
  • The ratings of seats can be the same
  • If there are an odd number of seats, the middle seat should have a higher rating.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free