Next Permutation Problem Solution

Next Permutation Problem: A permutation of an array of integers is an arrangement of its members into a sequence or linear order.

  • For example, for arr = [1,2,3], the following are all the permutations of arr[1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1]

The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).

  • For example, the next permutation of arr = [1,2,3] is [1,3,2].
  • Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
  • While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.

Given an array of integers numsfind the next permutation of nums.

The replacement must be in place and use only constant extra memory.

Example :

Input: nums = [1,2,3]

Output: [1,3,2]

Next Permutation Problem Solution

Problem Solution In Python

class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        
        lenn = len(nums)
        
        if lenn < 2:
            return
        
        a = [nums[-1]]
        
        for i in range(lenn-2,-1,-1):
            if nums[i] < nums[i+1]:
                m = i+1
                for j in range(i+1,lenn):
                    if nums[j] > nums[i]:
                        if nums[j] < nums[i+1]:
                            m = j
                
                nums[i], nums[m] = nums[m], nums[i]
                
                nums[i+1:] = sorted(nums[i+1:])
                
                return
        
        for i in range(lenn//2):
            nums[i],nums[lenn-1-i] = nums[lenn-1-i],nums[i]

Problem Solution In Java

public static void nextPermutation(int[] nums) {
            if (nums == null || nums.length == 0) return;

            int maxElementIndex = nums.length-1;
            for (int i=nums.length-2;i>=0;i--) {
                if (nums[maxElementIndex]< nums[i]) maxElementIndex = i;
                if(nums[i] < nums[i+1]) {
                    for (int j=nums.length-1;j>i;j--) {
                        if (nums[j] > nums[i]){
                            maxElementIndex = j;
                            break;
                        }
                    }
                    swap(nums, i, maxElementIndex);
                    Arrays.sort(nums, i+1, nums.length);
                    return;
                }
            }

            Arrays.sort(nums);
        }

        private static void swap(int[] nums, int i, int j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }

Problem Solution In C++

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int i = nums.size()-1;
        if(nums.size()==1) return;
        while(i>0 && nums[i] <= nums[i-1]) i--;
        if(i != 0){
            for(int j=nums.size()-1;j>=i;j--){
                if(nums[j]>nums[i-1]){
                    swap(nums[j],nums[i-1]);
                    break;
                } 
            }    
        }
        int j = nums.size()-1;
        while(i<j) swap(nums[i],nums[j]),i++,j--;
        
    }
};

Problem Solution In C

void nextPermutation(int* nums, int numsSize)
{
    int i, j;
    int t = -1;
    for (i = numsSize - 1; i > 0; --i) {
        if (nums[i - 1] < nums[i]) {
            t = i - 1;
            break;
        }
    }
    for (i = t + 1, j = numsSize - 1; i < j; ++i, --j) {
        nums[i] ^= nums[j];
        nums[j] ^= nums[i];
        nums[i] ^= nums[j];
    }
    if (t >= 0) {
        for (i = t + 1; i < numsSize; ++i) {
            if (nums[i] > nums[t]) {
                nums[i] ^= nums[t];
                nums[t] ^= nums[i];
                nums[i] ^= nums[t];
                break;
            }
        }
    }
}

Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment