NEXT PERMUTATION

 

Next Permutation 

Introduction

A permutation is an arrangement of elements in a particular order. The next permutation refers to the next greater arrangement of numbers in lexicographical (dictionary) order.

If no such permutation exists ( the array is in descending order), we rearrange it into the smallest possible order (ascending).

Problem Statement

Given an array nums[], find the next lexicographically greater permutation of its elements.

  • Modify the array in-place

  • Use constant extra space

Example

Input:

nums = [1, 2, 3]

Output:

[1, 3, 2]

Solution Code

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

        if (i == 0) {
            reverse(nums, 0, nums.length-1);
            return;
        }

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

        swap(nums, i-1, j);
        reverse(nums, i, 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) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }
}

Approach Used: Lexicographical Permutation Logic

Idea

We find the next greater permutation by modifying the array minimally.

How It Works

Step 1: Find the “breaking point”

Traverse from right to left and find the first index i such that:

nums[i-1] < nums[i]

 This is where the order can be increased

Step 2: If no such point exists

  • The array is in descending order

  • Reverse the entire array to get the smallest permutation

Step 3: Find the next greater element

From the right side, find the first element greater than nums[i-1] and swap them

Step 4: Reverse the remaining part

Reverse the subarray from index i to end

This ensures the next smallest arrangement after the swap

Why This Approach Was Chosen

  • Finds the next permutation in O(n) time

  • Uses constant extra space

  • Avoids generating all permutations (which is very expensive)

Alternative Approaches

  • Generate all permutations and sort them
     Time Complexity: O(n!)
     Not practical

Why not used?
Too inefficient for large inputs

Time and Space Complexity

  • Time Complexity: O(n)

  • Space Complexity: O(1)

Important Observations

  • The suffix after index i is always in descending order

  • Reversing it makes it the smallest possible order

  • This ensures we get the next permutation, not just any greater one

Example Walkthrough

Input:

[2, 3, 1]

Steps:

  • Find breaking point = 2 < 3

  • Swap 2 and 3= [3, 2, 1]

  • Reverse suffix = [3, 1, 2]

Output:

[3, 1, 2]

Conclusion

The next permutation problem is a great example of applying logical thinking to achieve an optimal solution. By identifying the correct position to modify and rearranging only a part of the array, we efficiently compute the next lexicographical permutation in linear time.

Comments

Popular posts from this blog

THE DELIVERY MAN

EC2 LAUNCHING

SORT A LINKED LIST USING MERGE SORT