You are given Q queries. Each query contains an integer k and a person’s information i.e, first name, last name, age. For each query, we need to output Kth person among them if all person information are arrange in ascending order.
Note: Person A comes before person B if first name of A is lexicographic smaller than that of B. But if first name is same then we compare last name and also if last name also same then we have to compare their ages.
Given: K will always less than or equal to number of person present.
Input : Q = 2 1 aa bb 10 2 bb cc 10 Output : First name:aa Last name:bb Age: 10 First name:bb Last name:cc Age: 10
Approach: The below algorithm is followed to solve the above problem.
- Basically we are given a person detail with each query and we have to tell Kth person if they are arranged in ascending order.
- Basic approach: After each query, we sort our list and just print kth person’s detail. But if we solve the above problem using sorting then time complexity will be O(q*q*log(q)). i.e, O(qlog(q)) for sorting each time and q queries.
- Optimized : We can improve time complexity for the problem stated above.As we know insertion in multiset can be done in log(n).
- So, just make a multiset which can be use for storing structure, and then After each query just insert the person’s detail in our multiset.
- So, this seems to be better approach.Our overall complexity will be O(q*log(q)).
First name:aa Last name:bb Age: 10 First name:bb Last name:cc Age: 10
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.
- User defined Data Types in C++
- Function Interposition in C with an example of user defined malloc()
- User Defined Literals in C++
- How to create an unordered_map of user defined class in C++?
- How to create an unordered_set of user defined class or struct in C++?
- How to implement user defined Shared Pointers in C++
- User-defined Custom Exception with class in C++
- Difference between user defined function and library function in C/C++
- 2D Vector In C++ With User Defined Size
- C++ set for user define data type
- C++ map having key as a user define data type
- Multiset in C++ Standard Template Library (STL)
- Difference between set, multiset, unordered_set, unordered_multiset
- multiset::operator= in C++ STL
- multiset::swap() in C++ STL
- multiset::emplace() in C++ STL
- multiset lower_bound() in C++ STL with Examples
- multiset upper_bound() in C++ STL with Examples
- multiset max_size() in C++ STL with Examples
- multiset insert() function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : nidhi_biet