Array ( [0] => [1] => questions [2] => Arrays [3] => Next-greater-number )
Given an integer. You have to find the next greater number formed by the digits of a given number.
First line contains a number N.
Print the next greater number
1234
1243
15526
15562
22634
22643
Login to see Discussion
Approach1: Next Permutation Algorithm
Step-i) Find the longest non-increasing suffix.
Step-ii) To find it, traverse the array until i-1 th element < ith element.
Step-iii) Mark the index as pivot where this condition fails.
Step-iv) If the suffix is the whole array, then there is no higher order permutation for the data.
Step-v) If the marked index is n-1, return doesnt exist.
Step-vi) Find the rightmost successor to the pivot.
Step-vii) Mark the successor index and swap it with our pivot.
Step-viii) Now reverse the suffix and return the answer.
Time Complexity: O(n)
Space Complexity; O(n)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution