Next Permutation LeetCode Solution


Frequently asked in Adobe Amazon Apple Atlassian Bloomberg ByteDance Cisco DoorDash Facebook Factset Flipkart Goldman Sachs IBM JP Morgan Microsoft PayPal PayU Qualtrics Rubrik Salesforce Snapchat TCS VMware Yahoo
categories - Medium tiktok WalmartViews 97

Problem Statement

Next Permutation LeetCode Solution – 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 considered permutations of arr[1,2,3][1,3,2][3,1,2][2,3,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 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 1:

Input:

 nums = [1,2,3]

Output:

 [1,3,2]

Example 2:

Input:

 nums = [3,2,1]

Output:

 [1,2,3]

Example 3:

Input:

 nums = [1,1,5]

Output:

 [1,5,1]

ALGORITHM OF Next Permutation LeetCode –

IDEA –

  • What we will do with these questions? We will focus on the number just greater than the given number.
  • So, at first, we will find the decreasing sequence from the last of the given array i.e we will focus on the decreasing order and keep checking for the order if the order breaks then will keep storing that index k.
  • Again we will check from the last if we find any number greater than the value of the k index then we will swap those two elements and reverse the whole array from the k+1 position to the last element.

Approach –

  • At first, we will make three variables first one n, i, and j. n is the length of the array and i is at the second last position and j is at the last position.
  • we will use a while loop and check for decreasing the order.
  • Then we will check if i is greater than or equal to zero then we will check for the next condition otherwise we will not check further.
  • Then will swap the index value and reverse the whole array after the i+1 position.

IMAGES OF THE Next Permutation LeetCode Solution-

Next Permutation LeetCode Solution Next Permutation LeetCode SolutionNext Permutation LeetCode Solution

class Solution:
    def nextPermutation(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        
        i = n-2
        j = n-1
        
        while(i >= 0 and nums[i] >= nums[i+1]):
            i-=1
            
            
        if i >= 0:
            while(nums[i] >= nums[j]):
                j-=1
                
        nums[i],nums[j] = nums[j],nums[i]
        self.reverse(nums,i+1,len(nums)-1)
        
        return nums
    
    def reverse(self,nums,i,j):
        while(i < j):
            nums[i],nums[j] = nums[j],nums[i]
            i+=1
            j-=1
class Solution {
    public void nextPermutation(int[] nums) {
        if(nums==null||nums.length<=1) return;
        
        int i = nums.length-2;
        while(i>=0 && nums[i]>=nums[i+1]) i--;
        
        if(i>=0)
        {
            int j = nums.length-1;
            while(nums[j]<=nums[i]) j--;            
            swap(nums,i,j);                
        }
        reverse(nums,i+1,nums.length-1);
    }
    
    public void swap(int[] nums, int i, int j)
    {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;        
    }
    
    public void reverse(int[] nums, int i,int j)
    {
        while(i<j)
        swap(nums,i++,j--);
    }
}

Complexity Analysis for Next Permutation LeetCode Solution

Time Complexity: O(N), As we have traversed through the whole array.

Space Complexity: O(1), As we have not taken any extra space.

SIMILAR QUESTION – https://www.tutorialcup.com/interview/string/palindrome-permutations-of-a-string.htm

Translate »