Question: Sort array in o(n) time, heres the problem N oranges and apples are arranged in a line in mixed order. It is night, however, and
N oranges and apples are arranged in a line in mixed order. It is night, however, and you cannot see anything. Yet, your goal is to separate the oranges from the apples, for example putting the oranges first. You are not allowed to count the number of the fruits in the line. The only operations allowed are i) Check the fruit in a given position (say by smelling it) and determine if it is orange or apple, and i) Swap the fruits in two different positions in the line with the help of another friend of yours. Explain how to solve the problem using only these two operations in O(N) time
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
