556. Next Greater Element III


Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.

Example 1:

Input: 12
Output: 21

Example 2:

Input: 21
Output: -1


Solution


Approach #1 Brute Force [Time Limit Exceeded]

To solve the given problem, we treat the given number as a string, . In this approach, we find out every possible permutation of list formed by the elements of the string formed. We form a list of strings, , containing all the permutations possible. Then, we sort the given to find out the permutation which is just larger than the given one. But this one will be a very naive approach, since it requires us to find out every possible permutation which will take really long time.

Complexity Analysis

  • Time complexity : . A total of permutations are possible for a number consisting of digits.

  • Space complexity : . A total of permutations are possible for a number consisting of digits, with each permutation consisting of digits.


Approach #2 Linear Solution [Accepted]

Algorithm

In this case as well, we consider the given number as a character array . First, we observe that for any given sequence that is in descending order, no next larger permutation is possible. For example, no next permutation is possible for the following array: [9, 5, 4, 3, 1]

We need to find the first pair of two successive numbers and , from the right, which satisfy . Now, no rearrangements to the right of can create a larger permutation since that subarray consists of numbers in descending order. Thus, we need to rearrange the numbers to the right of including itself.

Now, what kind of rearrangement will produce the next larger number? We want to create the permutation just larger than the current one. Therefore, we need to replace the number with the number which is just larger than itself among the numbers lying to its right section, say .

Next Greater Element

We swap the numbers and . We now have the correct number at index . But still the current permutation isn't the permutation that we are looking for. We need the smallest permutation that can be formed by using the numbers only to the right of . Therefore, we need to place those numbers in ascending order to get their smallest permutation.

But, recall that while scanning the numbers from the right, we simply kept decrementing the index until we found the pair and where, . Thus, all numbers to the right of were already sorted in descending order. Furthermore, swapping and didn't change that order. Therefore, we simply need to reverse the numbers following to get the next smallest lexicographic permutation.

The following animation will make things clearer:

!?!../Documents/556_Next_Greater_Element_III.json!?!

Complexity Analysis

  • Time complexity : . In worst case, only two scans of the whole array are needed. Here, refers to the number of digits in the given number.

  • Space complexity : . An array of size is used, where is the number of digits in the given number.


Analysis written by: @vinod23