This problem is screwing with my mind because of the examples.
If I have:
38276
Then the next biggest is
87632
But the problem points it out as the next biggest being 38627
I don't understand what the output should be?
This problem is screwing with my mind because of the examples.
If I have:
38276
Then the next biggest is
87632
But the problem points it out as the next biggest being 38627
I don't understand what the output should be?
38276 < 38627 < 87632
So your suggestion of 87632 cannot be next biggest. As the comment section of the linked problem suggests, look at C++ next_permute.
If it helps, think of this as the next alphabetical ordered solution. I.e. if you have abc, (next biggest) the next alphabetic order will be acb.
The problem is to find the next highest number not the highest number as you are currently thinking.
If the initial number n
is 38276
the next highest number m
would be 38627
since there is no number k
that can be formed from the initial digits such that n < k < m
.
The Next highest number is surely 38627
not 87632
. Using a brute force method, you can find all permutations of the number then sort it. equivalent of the logic in c++ is next_permutation
function