Note - str [ i . . n ] is sorted in ascending order . Find rightmost element 's index  that is less than str[i - 1]
j = i - 1 NEW_LINE while ( j + 1 <= n and str [ j + 1 ] <= str [ i - 1 ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT