Efficient algorithm
There is a group of n children standing in a queue, where their ages are listed in the array A[ ]. The children are standing randomly in the queue. Most probably, their ages in the array A[ ] are randomly listed too. For each child, you are required to search for the next older child in the queue and to print the difference of their ages. Print all the outputs in an array Out[ ].
The given array A[] of heights is as follows:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8]
11 10 7 13 14 12 9 15 8
The kid at the starting point of the queue 11 years old (i.e., A[0]). The next older kid in the queue at A[3] with age 13 years. The difference between their heights is 13 – 11 = 2, therefore 2 is printed. The second kid is 10 years old (i.e., A[1]) has also the same kid as the next first older kid.
Therefore 3 is printed and so on.
Remember that the queue is circular. Therefore, the last kid in the queue with age 8 is compared with the kid at the starting point of the queue, where the kid with 11 years old (i.e., A[0]) is the next older taller kid, and hence, 3 is printed.
Output: Out[ ]={2, 3, 6, 2, 1, 3, 6, -1, 3}
- a) Design an efficient algorithm to solve this problem and analyze the complexity of your solution.
- b) Develop a python code to implement your efficient algorithm. depend on the correctness of the code, indentation, comments, test-case.
- c) Prepare a brief report comparing the two algorithms .