To find the median of two different data without using extra space in C++. we use this algorithm. For eg: if we need a median of salaries given between two different batches.
We can simply solve by storing all elements in an array but to solve without using any extra space we have to use two pointer approach. we use two variables in a while loop and keep updating values and swapping when required, after the first loop we create two more loops in case if we missed out on some elements, from the beginning we also keep updating the count variable so that we can easily find median as we already know the position of the median from the definition.
Submitted by Podila Rithwik Teja (Rithwik2084)
Download packets of source code on Coders Packet