# Simple Java solution (4ms) with explanation.

• This solution is just a java version derived from this post.

At first, lets look at the edge cases -

1. If all digits sorted in descending order, then output is always “Not Possible”. For example, 4321.
1. If all digits are sorted in ascending order, then we need to swap last two digits. For example, 1234.
2. For other cases, we need to process the number from rightmost side (why? because we need to find the smallest of all greater numbers)

Now the main algorithm works in following steps -

I) Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller than the previously traversed digit. For example, if the input number is “534976”, we stop at 4 because 4 is smaller than next digit 9. If we do not find such a digit, then output is “Not Possible”.

II) Now search the right side of above found digit ‘d’ for the smallest digit greater than ‘d’. For “534976″, the right side of 4 contains “976”. The smallest digit greater than 4 is 6.

III) Swap the above found two digits, we get 536974 in above example.

IV) Now sort all digits from position next to ‘d’ to the end of number. The number that we get after sorting is the output. For above example, we sort digits in bold 536974. We get “536479” which is the next greater number for input 534976.

``````public class Solution {
public int nextGreaterElement(int n) {
char[] number = (n + "").toCharArray();

int i, j;
// I) Start from the right most digit and
// find the first digit that is
// smaller than the digit next to it.
for (i = number.length-1; i > 0; i--)
if (number[i-1] < number[i])
break;

// If no such digit is found, its the edge case 1.
if (i == 0)
return -1;

// II) Find the smallest digit on right side of (i-1)'th
// digit that is greater than number[i-1]
int x = number[i-1], smallest = i;
for (j = i+1; j < number.length; j++)
if (number[j] > x && number[j] <= number[smallest])
smallest = j;

// III) Swap the above found smallest digit with
// number[i-1]
char temp = number[i-1];
number[i-1] = number[smallest];
number[smallest] = temp;

// IV) Sort the digits after (i-1) in ascending order
Arrays.sort(number, i, number.length);

long val = Long.parseLong(new String(number));
return (val <= Integer.MAX_VALUE) ? (int) val : -1;
}
}
``````

• I don't think it is need to sort the digits since they are in descending order. Reversing those digits is enough and faster.

• Same idea here without needing to sort or using long

``````public int nextGreaterElement(int n) {
char[] arr = String.valueOf(n).toCharArray();

int i = arr.length - 2;
while (i >= 0 && arr[i] >= arr[i + 1])
i--;

if (i < 0) return -1;

int j = arr.length - 1;
while (arr[j] <= arr[i])
j--;

swap(arr, i, j);
reverse(arr, i + 1, arr.length - 1);

try {
return Integer.valueOf(String.valueOf(arr));
} catch (NumberFormatException e) {
return -1;
}
}

static void swap(char[] arr, int i, int j) {
arr[i] ^= arr[j];
arr[j] ^= arr[i];
arr[i] ^= arr[j];
}

static void reverse(char[] arr, int i, int j) {
int l = i, h = j;
while (l < h)
swap(arr, l++, h--);
}
``````

• This is something I came up with on my own, using a min heap. I think this problem lends itself very well to the use of a min heap.

``````public class Solution {
PriorityQueue<Integer> minHeap;

public Solution() {
minHeap = new PriorityQueue<>();
}

public int nextGreaterElement(int n) {
if (n < 12) {
return -1;
}

int max = n % 10;
n = n / 10;

while (n > 0) {
int digit = n % 10;
n = n / 10;

if (digit < max) {
return appendDigits(n, digit);
}

max = Math.max(max, digit);
}

return -1;
}

private int appendDigits(long n, int digit) {
// find & append the min digit greater than "digit" from the min heap
queue.offer(minHeap.poll());
while (queue.getLast() <= digit) {
queue.offer(minHeap.poll());
}
n = (n * 10) + queue.removeLast();

// next, append all the numbers from the queue above (in sorted order)
while (queue.size() > 0) {
n = (n * 10) + queue.poll();
}

// finally, append all of the remaining digits from the min heap (in sorted order)
while (minHeap.size() > 0) {
int top = minHeap.poll();
n = (n * 10) + top;
}

// check for overflow
if (n > Integer.MAX_VALUE) {
return -1;
}

return (int)n;
}
}
``````

• The tricky part for this question is the final result could go beyond `Integer.MAX_VALUE`.

We can either parse this number to a `long` and then compare it with `Integer.MAX_VALUE`, like

``````long val = Long.parseLong(new String(number));
return (val <= Integer.MAX_VALUE) ? (int) val : -1;
``````

or parse it to 32-bit integer but catch the exception if any

``````try {
return Integer.parseInt(String.valueOf(number));
} catch (Exception e) {
// Intentionally left blank
}
return -1;
``````

• char[] number = (n + "").toCharArray();

What's this `char[] number = (n + "").toCharArray()` for? Why we can add an integer to a string? What's the value for that?

• @sanketdige268 No need to sort. After you find the element you can arrange the right side elements with only 2 traverse!
Here is my code:

public class methods {
public static int nextGreater (int number){
int count=0;
int num = number;
if (num == 0)
count = 1;
else {
while (num >0){
num = num/10;
count++;
}
}
num = number;

``````    int [] numArray = new int [count];
while (num >0){
numArray[count-1] = num%10;
num = num/10;
count--;
}

for (int i=numArray.length-2; i> 0; i--){
if (numArray[i] < numArray[i+1]){
int min = numArray[i+1];
int k = i+1;
int j = i+2;
while (j < numArray.length){
if (min > numArray[j] && numArray[j] > numArray[i]){
min = numArray[j];
k = j;
}
j++;
}
swap(numArray , i , k);

for (int m=k; m < numArray.length-1 ; m++){
if (numArray[m] < numArray[m+1]){
swap(numArray , m , m+1 );
}
}

int r = i+1;
int s = numArray.length-1;

while (r < s){
swap (numArray , r , s);
r++;
s--;
}

int output=0;

for (int u =0; u<numArray.length; u++){
output = (output*10) + numArray[u];
}
return output;
}
}
return -1;
}

public static void swap (int [] array , int i , int j){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}``````

• @sanketdige268 Thanks for the solution! Do you know why this works though?

• Pretty clear solution and explanation! Thanks for sharing. I write it into python.

``````def nextGreaterElement(self, n):
s=list(str(n))
i=len(s)-1
while i>0:
if int(s[i-1])<int(s[i]):
break
i-=1
if i==0:return -1
smallest=i
x=int(s[i-1])
for j in range(i+1,len(s)):
if int(s[j])>x and int(s[smallest])>=int(s[j]):
smallest=j
s[smallest],s[i-1]=s[i-1],s[smallest]
temp=s[i:]
temp.sort()
res=''.join(s[:i])
res+=''.join(temp)
res=int(res)
return res if res>int(n) and res<2147483647 else -1
``````

• Based on the similar idea, no string operations and using bucket sort.

``````public int nextGreaterElement(int n) {
int i = n / 10, j = n % 10, index = 0;
boolean hasGreater = false;
while (i > 0) {
if (i % 10 < j) {
hasGreater = true;
break;
}
j = i % 10;
i /= 10;
index++;
}
if (!hasGreater) {
return -1;
}
index++;
int m = (int) Math.pow(10, index);

// Upper part has to replace the last digit with the smallest bigger digit in the remain part.
int upper = n / m;

// Remain part has to do bucket sort.
int remain = n % m;

int[] buckets = new int[10];
while (remain > 0) {
buckets[remain % 10]++;
remain /= 10;
}

/**
* Find the smallest next digit in the remain part that large than the last digit of the upper
* part.
*/
int d = upper % 10; // Last digit in upper part.
int a = 0, pick = -1;
while (a < 10) {
if (buckets[a] > 0 && a > d) {
pick = a;
// Update buket list.
buckets[a]--;
buckets[d]++;
break;
}
a++;
}
// Update upper part, only add the difference.
upper += pick - d;
int newR = 0;

/**
* Find the smallest value of the remain part.
*/
for (int k = 1; k < 10; k++) {
while (buckets[k]-- > 0) {
newR = newR * 10 + k;
}
}

// Handle overflow.
long res = (long) upper * m + newR;

return (int) (res > Integer.MAX_VALUE ? -1 : res);
}
``````

• @RunRunCode It's just a "simple" way to convert a number to a string. It will call String.valueOf(n) explicitely anyway. So I would suggest to use either String.valueOf or Integer.toString instead.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.