Given a permutation write c++ program to print permutation just greater than this
belongs to collection: Interview C++ coding problems/challenges | String
All Answers
total answers (1)
belongs to collection: Interview C++ coding problems/challenges | String
total answers (1)
Algorithm:
1. Find the largest k such that a[k]<a[k+1] , kЄ [0, n-1] //k=-1 initially 2. IF k is not updated (k is -1 still) that means current is the largest permutation (descending order). So the next will be the smallest one (ascending order). ELSE 3. Find largest l such that a[k]<a[l] 4. Swap a[k] and a[l] 5. Reverse a[k+1] to a[n-1]Example with Explanation:
C++ implementation
Output
need an explanation for this answer? contact us directly to get an explanation for this answer