Skip to content

Commit

Permalink
Added Solution of Q31 of Leetcode
Browse files Browse the repository at this point in the history
I have added the solution of Q31 (Next Permutation in CPP ) of leetcode.
  • Loading branch information
aanya963 authored Oct 1, 2022
1 parent ebd6521 commit fe26c3d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions leetCode Solutions/Q31_Next Permutation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

class Solution {
public:
void nextPermutation(vector<int>& a) {
int idx=-1;

int n = a.size();
for(int i=n-1; i>0; i--){
if(a[i]>a[i-1]){
idx=i;
break;
}
}
if(idx == -1){
reverse(a.begin(), a.end());
}else{
int prev = idx;
for(int i=idx+1; i<n; i++){
if(a[i]>a[idx-1] and a[i]<=a[prev]){
prev=i;
}
}
swap(a[idx-1],a[prev]);
reverse(a.begin()+idx,a.end());
}
}
};

0 comments on commit fe26c3d

Please sign in to comment.