-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathConvert an array to reduced form using hashing.cpp
83 lines (59 loc) · 1.75 KB
/
Convert an array to reduced form using hashing.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*Declare an array of name ar[ ] with n elements
Create a temp[ ] array of size n
Create a hashmap for the same datatype of the array
Copy the elements of array ar [ ] to temp[ ] array
Sort the temp[ ] array in ascending order
After sorting, mapping the values from 0 to n-1 with the temp array.
Replace the ar[ ] element with the hashtable values.
Print the ar[ ] */
#include<bits/stdc++.h>
using namespace std;
int main()
{
int ar[] = {12,34,5,6,8};
/* calculating the size of the given array */
int size = sizeof(ar)/sizeof(ar[0]);
/* creating temp array to sort and manipulate the array*/
int temp[size],val=0;
/* creating an unordered_map*/
unordered_map<int,int> mapped;
/* copying the elements from ar to temp array*/
for(int i=0;i<size;i++)
{
temp[i] = ar[i];
}
/*sorting the temp array using the sort method*/
sort(temp,temp+size);
/*mapping the values*/
for(int i=0;i<size;i++)
{
mapped[temp[i]] = val++;
}
/* reducing the elements of original array and printing them*/
cout<<"Reduced array:"<<endl;
for(int i=0;i<size;i++)
{
ar[i] = mapped[ar[i]];
cout<<ar[i]<<" ";
}
}
Python
if __name__ == '__main__':
ar =[12,34,5,6,8]
print("Given array: ",ar)
size = len(ar)
val = 0
#copying the elements in tmp array using the copy method
tmp = ar.copy()
#sorting the tmp array
tmp.sort()
#creating the map
mapvalues = {}
#mapping the reduced values
for i in range(size):
mapvalues[tmp[i]] = val
val+=1
#reducing the original array and printing it
for j in range(size):
ar[j] = mapvalues[ar[j]]
print("Reduced array:",ar)