The Next Permutation problem is a classic algorithmic challenge that requires finding the next lexicographical permutation of a given…