-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndex Mapping
50 lines (44 loc) · 955 Bytes
/
Index Mapping
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
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
// Since array is global, it is initialized as 0.
bool has[MAX + 1][2];
// searching if X is Present in the given array
// or not.
bool search(int X)
{
if (X >= 0) {
if (has[X][0] == 1)
return true;
else
return false;
}
// if X is negative take the absolute
// value of X.
X = abs(X);
if (has[X][1] == 1)
return true;
return false;
}
void insert(int a[], int n)
{
for (int i = 0; i < n; i++) {
if (a[i] >= 0)
has[a[i]][0] = 1;
else
has[abs(a[i])][1] = 1;
}
}
// Driver code
int main()
{
int a[] = { -1, 9, -5, -8, -5, -2 };
int n = sizeof(a)/sizeof(a[0]);
insert(a, n);
int X = -5;
if (search(X) == true)
cout << "Present";
else
cout << "Not Present";
return 0;
}