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

Comment body goes here.

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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7434472,"math_prob":0.9951089,"size":1504,"snap":"2021-43-2021-49","text_gpt3_token_len":456,"char_repetition_ratio":0.14133333,"word_repetition_ratio":0.03524229,"special_character_ratio":0.32646278,"punctuation_ratio":0.15186246,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9980891,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-05T02:48:38Z\",\"WARC-Record-ID\":\"<urn:uuid:efca5939-f8f6-4ff7-9b19-c96597277212>\",\"Content-Length\":\"24504\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9b91d720-abf3-410d-bfb6-0470f1deb81b>\",\"WARC-Concurrent-To\":\"<urn:uuid:3560d10f-aeee-41e9-b0f3-8196611091f0>\",\"WARC-IP-Address\":\"44.225.133.206\",\"WARC-Target-URI\":\"https://discuss.interviewbit.com/t/how-to-solve-such-type-of-question-is-there-any-specific-approach/22627\",\"WARC-Payload-Digest\":\"sha1:AMSBRJQ7SN4OENTRQGWEBGSBACKRNDHE\",\"WARC-Block-Digest\":\"sha1:QPFK23WGNC6XHMFMGMZJMHNOYA62ZFBB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363134.25_warc_CC-MAIN-20211205005314-20211205035314-00118.warc.gz\"}"}