I was asked to solve two problems. There are really good problems so I would like to discuss them here.
There is always a valid kth position
Output: p a
For first query, concatenated string is "treazape". After sorting, it becomes "aaeeprtz". 5th character is 'p'. For second query, concatenated string is "trea". After sorting becomes "aert". 1st character is 'a'.
— Moving food to adjacent cell costs 'x' amount of coins
— Cleaning a dirty cell costs 'y' coins
— After cleaning, the dirty cell becomes empty
— You can only move food to an adjacent empty cell
Now, you have to process Q queries of the type "x y". In each query, print the minimum cost required to group all foods together.
It is optimal to first clean the dirty cell at position 3, this will cost 2 amount of coins. After cleaning the array becomes — "FEEEF". Now we can move foods in the following manner — "EFFEE". This will cost 3*1 = 3 coins. So final cost will be 3+2 = 5