# Sum of 2 numbers in an array

**Problem statement**

Identify whether there exists a pair of numbers in an array such that their sum is equal to .

**Input**

The first line contains one integer , which is the sum we are trying to find. The second line contains one integer , which is the length of the array. This is followed by lines each containing one element of the array.

**Output**

Output if there exists a pair of numbers in the array such that their sum equals . If such a pair does not exist, output .

**Sample Input**

1 2 3 4 5 6 7 8 9 10 11 12 |
66 10 18 11 21 28 31 38 40 55 60 62 |

**Sample Output**

1 |
1 |

**Solution**

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 |
public class SumOfTwoNumbers { public static void main(String[] args) throws FileNotFoundException { System.setIn(new FileInputStream(System.getProperty("user.home") + "/" + "in.txt")); Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[] a = new int[m]; for (int i=0; i<m; i++) { int num = in.nextInt(); a[i] = num; } Map<Integer, Integer> num2index = new HashMap<Integer, Integer>(); for (int i=0; i<m; i++) { num2index.put(a[i], i); } for (int i=0; i<a.length; i++) { if (num2index.containsKey(n-a[i]) && num2index.get(n-a[i]) != i) { System.out.println("1"); return; } } System.out.println("0"); } } |