Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Next Permutation problem solution

YASH PAL, 31 July 2024

In this Leetcode Next Permutation problem solution Implement the next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order). The replacement must be in place and use only constant extra memory.

Leetcode 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;
            }
        }
    }
}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes