We will add an ith element in the search space if the search space is empty or if the ith element is greater than the last element of the search space .
if ( $ j == 0 or $ searchSpace [ $ j - 1 ] < $ arr [ $ i ] ) { $ searchSpace [ $ j ] = $ arr [ $ i ] ; $ index [ $ j ] = $ i ; $ j ++ ; }