Nlogn solution