102k views
5 votes
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[ ].

There is a group of n children standing in a queue, where their ages are listed in-example-1

1 Answer

6 votes

Answer:

solution for b)

A = [11, 10, 7, 13, 14, 12, 9, 15, 8]

Out = []

for i in range(len(A)):

if i == len(A) - 1:

if A[i] < A[0]:

Out.append(A[0] - A[i])

else:

Out.append(-1)

else:

if A[i] < A[i + 1]:

Out.append(A[i + 1] - A[i])

else:

Out.append(-1)

print(Out)

User Coreyspitzer
by
4.4k points