-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rohit.java
53 lines (48 loc) · 1.29 KB
/
Rohit.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.ArrayList;
import java.util.Collections;
public class Rohit
{
static ArrayList<String> result;
public static void addToResult(int[] arr, int max)
{
String s = "";
for(int i = 0; i < max; i++)
{
s += arr[i];
}
//System.out.println(s);
result.add(s);
}
public static void permutation(int[] nums, int length, int max)
{
if(length == 1)
{
addToResult(nums, max);
}
for(int i = 0; i < length; i++)
{
permutation(nums, length-1, max);
int temp;
if(length % 2 == 0)
{
temp = nums[i];
nums[i] = nums[length - 1];
nums[length - 1] = temp;
}
else
{
temp = nums[0];
nums[0] = nums[length - 1];
nums[length - 1] = temp;
}
}
}
public static void main(String[] args)
{
result = new ArrayList<>();
int[] nums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
permutation(nums, nums.length, nums.length);
Collections.sort(result);
System.out.println("The millionth lexographic permutation of these numbers is " + result.get(1000000));
}
}