Skip to content

Commit

Permalink
Update Q31_Next Permutation.cpp
Browse files Browse the repository at this point in the history
Language used: C++
Approach used: Two pointers 
Question Solved: Next Permutation
  • Loading branch information
aanya963 authored Oct 2, 2022
1 parent fe26c3d commit 645ba86
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions leetCode Solutions/Q31_Next Permutation.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#include<bits/stdc++.h>
using namespace std;

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

int n = a.size();
for(int i=n-1; i>0; i--){
for(int i=n-1; i>0; i--){
if(a[i]>a[i-1]){
idx=i;
break;
}
}
if(idx == -1){
if(idx == -1){ // if does not have a lexicographical larger rearrangement.
reverse(a.begin(), a.end());
}else{
}else{
int prev = idx;
for(int i=idx+1; i<n; i++){
if(a[i]>a[idx-1] and a[i]<=a[prev]){
Expand Down

0 comments on commit 645ba86

Please sign in to comment.