solution to rearange problem