-1

swap digit problem

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?

Community
  • 1
  • 1
DJJaved
  • 31
  • 1
  • 2

3 Answers3

1

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.

Nicko Po
  • 727
  • 5
  • 21
1

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.

Shash S
  • 11
  • 1
1

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

Ibukun Muyide
  • 1,294
  • 1
  • 15
  • 23