n people are standing in a queue to buy entry ticket for the carnival. People present there strongly believe in chivalry. Therefore, at time = t, if a man at position x, finds a woman standing behind him then he exchanges his position with her and therefore, at time = t+1, woman is standing at position x while man is standing behind her.
Given the total number of people standing in a queue as n, particular instant of time as t and the initial arrangement of the queue in the form of a string containing ‘M’ representing man at position i and ‘W’ representing woman is at position i, find out the arrangement of the queue at time = t.
Input : n = 6, t = 2 BBGBBG Output: GBBGBB Explanation: At t = 1, 'B' at position 2 will swap with 'G' at position 3 and 'B' at position 5 will swap with 'G' at position 6. String after t = 1 changes to "BGBBGB". Now at t = 2, 'B' at position = 1 will swap with 'G' at position = 2 and 'B' at position = 4 will swap with 'G' at position 5. String changes to "GBBGBB". Since, we have to display arrangement at t = 2, the current arrangement is our answer. Input : n = 8, t = 3 BBGBGBGB Output: GGBGBBBB
Traverse the entire string at every moment of time from 1 to t and if we find pairwise “BG” then swap them and move to check the next pair.
Below is the implementation of above approach:
- Find if neat arrangement of cups and shelves can be made
- Arrangement of words without changing the relative position of vowel and consonants
- Arrangement of the characters of a word such that all vowels are at odd places
- Possible arrangement of persons waiting to sit in a hall
- Reversing a Queue using another Queue
- Find the time which is palindromic and comes after the given time
- Restore a shuffled Queue as per given Conditions
- Queue based approach for first non-repeating character in a stream
- Breadth First Search without using Queue
- Append the elements of queue in mirror-inverse order
- Find the minimum time after which one can exchange notes
- Find time taken for signal to reach all positions in a string
- Queries for rotation and Kth character of the given string in constant time
- Maximum time such that absolute difference between hour and minute lies in given range
- Count of vessels completely filled after a given time
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- VMware Interview Experience | Set3 (On-Campus for Full Time and Internship Offers)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.