-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainpairsum.java
38 lines (32 loc) · 1.05 KB
/
Mainpairsum.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
import java.util.*;
class Mainpairsum
{
// Function to find a pair in an array with a given sum using hashing
public static void findPair(int[] nums, int target)
{
// create an empty HashMap
Map<Integer, Integer> map = new HashMap<>();
// do for each element
for (int i = 0; i < nums.length; i++)
{
// check if pair (nums[i], target-nums[i]) exists
// if the difference is seen before, print the pair
if (map.containsKey(target - nums[i]))
{
System.out.printf("Pair found (%d, %d)",
nums[map.get(target - nums[i])], nums[i]);
return;
}
// store index of the current element in the map
map.put(nums[i], i);
}
// we reach here if the pair is not found
System.out.println("Pair not found");
}
public static void main (String[] args)
{
int[] nums = { 8, 7, 2, 5, 3, 1 };
int target = 10;
findPair(nums, target);
}
}