-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSequenceEquation.java
49 lines (41 loc) · 1.44 KB
/
SequenceEquation.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
// https://www.hackerrank.com/challenges/permutation-equation/problem
package implimentation;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class SequenceEquation {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int length = scanner.nextInt();
int[] array = getArray(length);
int[] result = getFunctionalInverse(array);
print(result);
}
private static int[] getArray(int length) {
int[] array = new int[length];
for (int index = 0 ; index < array.length ; index++) {
array[index] = scanner.nextInt();
}
return array;
}
private static void print(int[] array) {
for (int number : array) {
System.out.println(number);
}
}
private static int[] getFunctionalInverse(int[] array) {
Map<Integer, Integer> indices = getIndices(array);
int[] result = new int[array.length];
for (int number = 1 ; number <= result.length ; number++) {
result[number - 1] = indices.get(indices.get(number));
}
return result;
}
private static Map<Integer, Integer> getIndices(int[] array) {
Map<Integer, Integer> indices = new HashMap<>();
for (int index = 0 ; index < array.length ; index++) {
indices.put(array[index], index + 1);
}
return indices;
}
}