-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexMapping.java
72 lines (64 loc) · 1.06 KB
/
IndexMapping.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Java program to implement direct index
// mapping with negative values allowed.
class GFG
{
final static int MAX = 1000;
// Since array is global, it
// is initialized as 0.
static boolean[][] has = new boolean[MAX + 1][2];
// searching if X is Present in
// the given array or not.
static boolean search(int X)
{
if (X >= 0)
{
if (has[X][0] == true)
{
return true;
}
else
{
return false;
}
}
// if X is negative take the
// absolute value of X.
X = Math.abs(X);
if (has[X][1] == true)
{
return true;
}
return false;
}
static void insert(int a[], int n)
{
for (int i = 0; i < n; i++)
{
if (a[i] >= 0)
{
has[a[i]][0] = true;
}
else
{
has[Math.abs(a[i])][1] = true;
}
}
}
// Driver code
public static void main(String args[])
{
int a[] = {-1, 9, -5, -8, -5, -2};
int n = a.length;
insert(a, n);
int X = -5;
if (search(X) == true)
{
System.out.println("Present");
}
else
{
System.out.println("Not Present");
}
}
}
// This code is contributed vickyjsr