solved using merge sort technique