Next Permutation Solution

    

  def nextPermutation(nums):
  # Find the first decreasing element from the right
  i = len(nums) - 2
  while i >= 0 and nums[i] >= nums[i + 1]:
      i -= 1

  # Find the first element greater than the decreasing element
  if i >= 0:
      j = len(nums) - 1
      while j >= 0 and nums[j] <= nums[i]:
          j -= 1
      nums[i], nums[j] = nums[j], nums[i]

  # Reverse the remaining elements
  left = i + 1
  right = len(nums) - 1
  while left < right:
      nums[left], nums[right] = nums[right], nums[left]
      left += 1
      right -= 1

  nums = [1, 2, 3]
  nextPermutation(nums)
  print(nums)

Explanation (Python):

The Python solution uses a three-step process to find the next permutation:

Find the first decreasing element from the right, denoted as i. This element will be swapped later.

Find the first element greater than the decreasing element, denoted as j.

Reverse the remaining elements to get the smallest lexicographically larger permutation.


         
  class Solution {
    public void nextPermutation(int[] nums) {
        int i = nums.length - 2;
        while (i >= 0 && nums[i] >= nums[i + 1]) {
            i--;
        }

        if (i >= 0) {
            int j = nums.length - 1;
            while (j >= 0 && nums[j] <= nums[i]) {
                j--;
            }
            swap(nums, i, j);
        }

        reverse(nums, i + 1, nums.length - 1);
    }

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

    private void reverse(int[] nums, int start, int end) {
        while (start < end) {
            swap(nums, start, end);
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        Solution solution = new Solution();
        solution.nextPermutation(nums);
        for (int num : nums) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
  }

  


Explanation (Java):

The Java solution follows a similar three-step process as the Python solution:

Find the first decreasing element from the right, denoted as i.

Find the first element greater than the decreasing element, denoted as j.

Reverse the remaining elements to get the smallest lexicographically larger permutation.


         
        #include <iostream>
        #include <vector>
        #include <algorithm>
        using namespace std;
        
        void nextPermutation(vector& nums) {
            int i = nums.size() - 2;
            while (i >= 0 && nums[i] >= nums[i + 1]) {
                i--;
            }
        
            if (i >= 0) {
                int j = nums.size() - 1;
                while (j >= 0 && nums[j] <= nums[i]) {
                    j--;
                }
                swap(nums[i], nums[j]);
            }
        
            reverse(nums.begin() + i + 1, nums.end());
        }
        
        int main() {
            vector nums = {1, 2, 3};
            nextPermutation(nums);
            for (int num : nums) {
                cout << num << " ";
            }
            cout << endl;
            return 0;
        }
        
      
    
    
        
        

Explanation (C++):

The C++ solution also follows the same three-step process as the Python and Java solutions:

Find the first decreasing element from the right, denoted as i.

Find the first element greater than the decreasing element, denoted as j.

Reverse the remaining elements to get the smallest lexicographically larger permutation.

>