How to solve such type of question . Is there any specific approach

Comment body goes here.

[https://stackoverflow.com/questions/1622532/algorithm-to-find-next-greater-permutation-of-a-given-string]

nice post on this

ector Solution::nextPermutation(vector &A) {
// vectorans(A.size());
int flag=0,temp,a,b,m=INT_MAX;
for(int i=A.size()-1;i>0;i–)
{
if(A[i-1]<A[i])
{
a=A[i-1];
for(int j=i;j<A.size();j++)
{
if((A[j]-a)>0&&(A[j]-a)<m)
{
b=j;
m=A[j]-a;
}

          }
          temp=A[b];
          A[b]=A[i-1];
          A[i-1]=temp;
         sort(A.begin()+i,A.end());
          flag=1;
          break;
           
       }
       
   }
   if(!flag)
     sort(A.begin(),A.end());
     return A;

}

@shivom-pandey_577
Let us try an example here.

—> 2 1 5 4 3 is a number for which we need to get next permutaion.
—> 2 1 5 4 3 This bold part is continuous decreasing sequence. from right side.
—> 2 1 5 4 3 This number is the named swap-number which is next to continuous decreasing sequence.
—> Find a smallest number in continuous decreasing sequence which is just greater than swap-number . Here it is 3(smallest possible number which is greater than 1 [4, 5 are not smallest])
—> Swap both the numbers. swap(swap-number, smallest-large number), (2 3 5 4 1) is the number now.
—> Reverse the continuous decreasing sequence to make it continuous increasing sequence (2 3 1 4 5) is the sequence now.
—> This is the next permutation.

For 5 4 3 2 1 The entire number is continuously decreasing sequence. There is no number left of it (there is no swap-number).
Hence reverse the string 1 2 3 4 5.

thanks bro for your help

Click here to start solving coding interview questions