@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;
}