Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Next Greater Permutation Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Next Greater Permutation | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given an array, rearrange it to its next greater permutation. Do it in-place with extra constant memory only. Do not use any library function for the next permutation.

Naive Approach

The naive approach here will be a straight brute force. Find all the permutations of the given array, find the current permutation, and then print the next permutation in the list

Analysis

  • Time Complexity: O(n * n!) or O(n * n! * log(n!)) depending on implementation.
  • Space Complexity: O(n) 

Optimal Approach

We can solve this problem in a single pass.

  • First, observe that if the array is sorted in descending order, no next greater permutation is possible for the array, so we just reverse the array and return it.
  • Else, traverse the array from right to left, and find the first position where a[i] > a[i - 1].
  • We can observe that we need to reverse numbers to the right of a[i - 1] for the next greater permutation.
  • How can we get the next greater permutation? Well, observe that the next greater permutation will have the element just greater than a[i - 1] to the right of a[i - 1] swapped with a[i - 1].
  • Now, just reverse the subarray from a[i] to the end of the array to get the next greater permutation.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation

C++
void nextGreaterPermutation(vector<int> &arr) {
   reverse(arr.begin(), arr.end());
   if (isSorted(arr.begin(), arr.end())) {
       return;
   }
   int index = 0;
   for (int i = 1; i < arr.size(); i++) {
       if (arr[i - 1] > arr[i]) {
           index = i;
           break;
       }
   }
   for (int i = 0; i < index; i++) {
       if (arr[i] > arr[index]) {
           swap(arr[i], arr[index]);
           break;
       }
   }
   reverse(arr.begin(), arr.begin() + index);
   reverse(arr.begin(), arr.end());
}
Java
class Solution {
   void nextGreaterPermutation (int[] arr) {
       reverse(arr, 0, arr.length - 1);
       if (isSorted(arr) == true) {
           return;
       }
       int index = 0;
       for (int i = 1; i < arr.length; i++) {
           if (arr[i - 1] > arr[i]) {
               index = i;
               break;
           }
       }
       for (int i = 0; i < index; i++) {
           if (arr[i] > arr[index]) {
               swap(arr, i, index);
               break;
           }
       }
       reverse(arr, 0, index - 1);
       reverse(arr, 0, arr.length - 1);
   }
   boolean isSorted(int[] arr) {
       boolean sorted = true;
       for(int i = 1; i < arr.length; i++) {
           if(arr[i] < arr[i - 1]) {
               sorted = false;
           }
       }
       return sorted;
   }
   void reverse(int[] arr, int start, int end) {
       int left = start, right = end;
       while(left < right) {
           swap(arr, left, right);
           left++;
           right--;
       }
   }
   
   void swap(int[] arr, int first, int second) {
       int temp = arr[first];
       arr[first] = arr[second];
       arr[second] = temp;
   }
}
Related Content
Cumulative Sum
Even Number of Digits
Identical Twins
Largest Contiguous Sum
Matrix Rotation
Max Consecutive Ones
Pascal's Triangle
Positive Cumulative Sum
Primes upto N
Row Column Zero
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet