Next Permutation

Advanced Array Logic DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Find the lexicographically next greater permutation of a given integer array, Arrays, Permutations, Sorting, Time Complexity, Space Complexity, in-place algorithms, complexity analysis, brute force, string operations, greedy algorithm, array manipulation, combinatorics, Permutations, Array Reversal, Greedy Approach.

Next Permutation Given an integer array Arr[], rearrange its numbers to form the lexicographically next greater permutation of numbers. If such an arrangement is not possible (i.e., the array is already in its largest possible lexicographical order), it must be rearranged to the lowest possible order (i.e., sorted in ascending order). Examples Example 1: Explanation: All permutations of {1, 2, 3} are {{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}. The next permutation just after {1, 3, 2} is {2, 1, 3}. Example 2: Explanation: Among all permutations of {1, 2, 3}, {3, 2, 1} is at the last position in lexicographical order. Therefore, the next permutation is the first one, which is the array sorted in ascending order.