-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
34 lines (25 loc) · 993 Bytes
/
Solution.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
// Program to convert an array to reduced form
import java.util.Arrays;
import java.util.HashMap;
public class Solution {
//Method for reduced array
public static int[] toReducedForm(int[] array, int arrayLength) {
int temp[] = array.clone();
Arrays.sort(temp);
HashMap<Integer, Integer> umap = new HashMap<>();
/* One by one insert elements of sorted
temp[] and assign them values from 0 to n-1 */
int value = 0;
for (int index = 0; index < arrayLength; index++)
umap.put(temp[index], value++);
// Convert array by taking positions from umap
for (int index = 0; index < arrayLength; index++)
array[index] = umap.get(array[index]);
return array;
}
public static void main(String args[]){
int[] array = {5,10,40,30,20};
int[] reducedArray = toReducedForm(array,5);
System.out.println(Arrays.toString(reducedArray));
}
}