Doubt in the question?

Do need to write our own next_permutation() ?

yes !!, though c++ stl do provide a function for that but you have to implement this by yourself for this question