Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to efficiently find the next greater permutation of a list of values, in Java?

/*
	This is an implementation that demonstrates
	how to efficiently find the next greater
	permutation for a list of values.
	For example, next greater permutation for:
	- [1, 2, 3] -> [1, 3, 2]
	- [2, 3, 1] -> [3, 1, 2]

	Let n be the number of values in the list.

	Time complexity: O(n) 
	Space complexity: O(1)
*/
import java.util.Arrays;

public class NextGreaterPermutation {
	public static void main(String[] args) {
		int[] nums = { 1, 2, 3 };
		nextPermutation(nums);
		System.out.println(Arrays.toString(nums)); // [1, 3, 2]
		nums = new int[] { 2, 3, 1 };
		nextPermutation(nums);
		System.out.println(Arrays.toString(nums)); // [3, 1, 2]
	}

	public static void nextPermutation(int[] nums) {
		int i = -1;
		// Find position of rightmost value
		// that is smaller than its successor
		for (int j = nums.length - 2; j >= 0; j--) {
			if (nums[j] < nums[j + 1]) {
				i = j;
				break;
			}
		}
		if (i == -1) {
			// This is the max combination
			// We need to sort it in ascending order
			reverse(nums, 0);
			return;
		}
		// Find rightmost value greater than the one
		// at index i and swap it with the one at i
		for (int j = nums.length - 1; j > i; j--) {
			if (nums[j] > nums[i]) {
				swap(nums, i, j);
				break;
			}
		}
		// Reverse the elements to the right of i
		reverse(nums, i + 1);
	}

	private static void reverse(int[] nums, int start) {
		int i = start, j = nums.length - 1;
		while (i < j) {
			swap(nums, i, j);
			i++;
			j--;
		}
	}

	private static void swap(int[] nums, int i, int j) {
		int temp = nums[i];
		nums[i] = nums[j];
		nums[j] = temp;
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to convert char to uppercase java 
Java :: how to remove duplicate elements from char array in java 
Java :: assert throws exception java 
Java :: string vs stringbuffer 
Java :: find first element of list java 
Java :: java break multiple loops 
Java :: how to check null and empty string in java 
Java :: java string regexp replace 
Java :: sum of list java 
Java :: uninstall java ac 
Java :: java nested loop 
Java :: terminate a frame java 
Java :: jtable get get row 
Java :: export java_home linux 
Java :: java convert date to localdate 
Java :: 64 bit integer java 
Java :: javac celar 
Java :: how to get color from resource android 
Java :: synchronized java 
Java :: compile java 
Java :: this keyword in java 
Java :: java throw keyword 
Java :: set textview text android java 
Java :: spring filter exception handling 
Java :: array arraylist java 
Java :: hollow pyramid star pattern in java 
Java :: how to give square in java 
Java :: how to display the decimal value when 0 after point android studio 
Java :: maven artifact 
Java :: System.out.println("Hello world") 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =