Skip to content

Instantly share code, notes, and snippets.

@FarazFiroz
FarazFiroz / nextPermutation.txt
Created January 24, 2026 08:04
Leetcode 31 - Next Permutation
class Solution {
public void nextPermutation(int[] nums) {
// find the pivot from where the left part is in descending order
for(int i = nums.length-2; i>=0; i--){
if(nums[i] < nums[i+1]){
int pivot = i;
// now find successor of pivot from rightmost part
int successor = nums.length-1;
for(int j = nums.length-1; j>pivot; j--){
if(nums[j] > nums[pivot]){