-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPairWithGivenDifference.java
More file actions
103 lines (61 loc) · 1.62 KB
/
PairWithGivenDifference.java
File metadata and controls
103 lines (61 loc) · 1.62 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
Given an one-dimensional unsorted array A containing N integers.
You are also given an integer B, find if there exists a pair of elements in the array whose difference is B.
Return 1 if any such pair exists else return 0.
Problem Constraints
1 <= N <= 105
-103 <= A[i] <= 103
-105 <= B <= 105
Input Format
First argument is an integer array A of size N.
Second argument is an integer B.
Output Format
Return 1 if any such pair exists else return 0.
Example Input
Input 1:
A = [5, 10, 3, 2, 50, 80]
B = 78
Input 2:
A = [-10, 20]
B = 30
Example Output
Output 1:
1
Output 2:
1
Example Explanation
Explanation 1:
Pair (80, 2) gives a difference of 78.
Explanation 2:
Pair (20, -10) gives a difference of 30 i.e 20 - (-10) => 20 + 10 => 30
**/
public class Solution {
public int solve(int[] A, int B) {
if(B == 0)
{
Set<Integer> set = new HashSet<Integer>();
for(int i=0; i<A.length; i++)
{
if(set.contains(A[i]))
{
return 1;
}
set.add(A[i]);
}
return 0;
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<A.length; i++)
{
map.put(A[i], i);
}
for(int i=0; i<A.length; i++)
{
if(map.containsKey(A[i] + B))
{
return 1;
}
}
return 0;
}
}