java previous permutation
Java
public static boolean prev_permutation(int[] arr) {
int len = arr.length;
int i = len - 1;
// 1. find largest i where arr[i - 1] > arr[i]
while (i > 0) {
if (arr[i - 1] > arr[i]) break;
i--;
}
if (i <= 0) return false;
// 2. find largest j where arr[i - 1] > arr[j] and j >= i
int j = len - 1;
while (j >= i) {
if (arr[i - 1] > arr[j]) break;
j--;
}
// 3. swap elements between arr[i-1] and arr[j]
swap(i - 1, j, arr);
// 4. reverse elements from i to end of array
len--;
while (i < len) {
swap(i, len, arr);
len--;
i++;
}
return true;
}
public static void swap(int x, int y, int[] arr) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
Also in Java:
- Title
- How to draw Bezier Curve in Android
- Category
- Java
- Title
- how to add integers in java
- Category
- Java
- Title
- java for
- Category
- Java
- Title
- java how to make a gui
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- from file to array java
- Category
- Java
- Title
- count occurrences of character in string java 8
- Category
- Java
- Title
- pascals triangle java
- Category
- Java
- Title
- check if map contains key java
- Category
- Java
- Title
- how to scan a character in java
- Category
- Java
- Title
- read csv java
- Category
- Java
- Title
- java double format
- Category
- Java
- Title
- java for each
- Category
- Java
- Title
- java creare costante
- Category
- Java
- Title
- substring java
- Category
- Java
- Title
- how to change top of window in java
- Category
- Java
- Title
- lightweight java game library
- Category
- Java
- Title
- how to remove components from a JFRame java
- Category
- Java
- Title
- java 8 random string generator
- Category
- Java
- Title
- java create a set with values
- Category
- Java
- Title
- how to append to an array in java
- Category
- Java
- Title
- java min function
- Category
- Java
- Title
- como saber de que tipo es una variable en java
- Category
- Java
- Title
- how to add objects in array java
- Category
- Java
- Title
- java method
- Category
- Java
- Title
- java linked list iterator
- Category
- Java
- Title
- write in file java
- Category
- Java
- Title
- copy array in java
- Category
- Java
- Title
- how to get binary value in java
- Category
- Java
- Title
- how to call a static method in java
- Category
- Java
- Title
- java copy array
- Category
- Java
- Title
- how to change maven java version in po,
- Category
- Java
- Title
- Spring Security JWT Authentication + PostgreSQL – RestAPIs SpringBoot + Spring MVC + Spring JPA
- Category
- Java
- Title
- Write a method that raises a number to a power without using Math.pow() method
- Category
- Java
- Title
- java how to read a text file
- Category
- Java
- Title
- random java
- Category
- Java
- Title
- array in java stack overflow
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- android round double to 2 decimal
- Category
- Java
- Title
- write file from a specific location in java
- Category
- Java
- Title
- split by new line java
- Category
- Java
- Title
- inheritance setter and getter in java
- Category
- Java
- Title
- java loop through map
- Category
- Java
- Title
- java loop through object
- Category
- Java
- Title
- log4j with spring boot restful services
- Category
- Java
- Title
- refrence xml textfield in javafx
- Category
- Java
- Title
- generic method lambda java
- Category
- Java
- Title
- java import
- Category
- Java
- Title
- if else program in java
- Category
- Java
- Title
- to char array java
- Category
- Java
- Title
- generate all prime number less than n java (fastest method)
- Category
- Java
- Title
- how to add to a file in java
- Category
- Java
- Title
- covariant type in java
- Category
- Java
- Title
- how to compare strings java
- Category
- Java
- Title
- how to open a folder in java swing project
- Category
- Java
- Title
- error message pushes button down
- Category
- Java
- Title
- how to exit a for loop in java
- Category
- Java
- Title
- in dom parser how to find processing instruction in java
- Category
- Java
- Title
- filter in list by time java
- Category
- Java
- Title
- java script dynamic varibale creation
- Category
- Java
- Title
- are strings modifiable
- Category
- Java
- Title
- hide elements android
- Category
- Java
- Title
- how to make one java class inherit from another
- Category
- Java
- Title
- separateur JMenu swing java
- Category
- Java
- Title
- shortcut to find a class in java project eclipse
- Category
- Java
- Title
- creating a program about user asked to enter a number and enters 0 to stop, then calculates the sum of numbers entered, the average, the length of positive numbers and negative numbers entered by the user in java
- Category
- Java
- Title
- immagini java
- Category
- Java
- Title
- java split string on two or more spaces except for words in quotes
- Category
- Java
- Title
- Category
- Java
- Title
- android hide and show bottom navigation
- Category
- Java
- Title
- spigot disable join message
- Category
- Java
- Title
- how to access private vairable in java method
- Category
- Java
- Title
- how to add an object to a list of objects in java
- Category
- Java
- Title
- java identifiers
- Category
- Java
- Title
- type javascirpt
- Category
- Java
- Title
- height constraint layout guideline
- Category
- Java
- Title
- write an infinite loop java
- Category
- Java
- Title
- java get command line output
- Category
- Java
- Title
- get material of block bukkit
- Category
- Java
- Title
- how to print in java
- Category
- Java
- Title
- java read file bufferedreader
- Category
- Java
- Title
- java iterate through map
- Category
- Java
- Title
- java read integer from text file into array scanner
- Category
- Java
- Title
- test function that call a function javascrip
- Category
- Java
- Title
- ujava saum of positive integers
- Category
- Java
- Title
- import
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- java call another constructor
- Category
- Java
- Title
- java create directory
- Category
- Java
- Title
- remove item from arraylist in java
- Category
- Java
- Title
- java string to char array
- Category
- Java
- Title
- Java array nested equals
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- how to reverse a string in java
- Category
- Java
- Title
- arraylist to int array java
- Category
- Java
- Title
- lopp array java
- Category
- Java
- Title
- applicationcontext.xml
- Category
- Java
- Title
- context error in android studio
- Category
- Java
- Title
- java stack empty
- Category
- Java
- Title
- init cap java
- Category
- Java