I have been trying to write a program which checks if there are any two items in some list L whose sum is equal to a given integer S, and prints those two items found. The program provides an error message of “!ok” when there is no solution available.

Input: On the first line there will be an integer T representing the number of test cases, followed by 2 input lines for each test case: On the first line of each test case, there will be 2 integers S and E, where S is the expected sum and E is the number of elements in the list. On the second line, there will be E integers separated by a space. Each integer represents an element of the list L. The elements are not sorted in any way and some could have the same value. In cases where the number E is 0, the second line will be empty.

All values for the elements of list L will be in the same range as the value S.

output: For each test case , the output should one contains ass following: If there is an unique solution, two elements x and y (of the list L) should be printed, separated by a single space. If there are multiple solutions- only the first complete pair that appears on the list and provides the correct sum will be printed. If there is no solution- the error message “!OK” should be printed.

Constraints and notes:

1≤T≤1000

-10^6≤S≤10^6

0≤E≤2⋅10^4

The sum of values of E is at most 10^7

In order to perform that task, I Have tried to use hash-table , since using arrays might be problematic in this case considering run time complexity.

Here is my code:

`#include <iostream> #include <bits/stdc++.h> #include <set> using namespace std; int main() { long long int numberElements=0, number; long long int sum=0, temp; unordered_set<long long int> s; bool found=0; int numberCases; cin >> numberCases; for(int c = 1; c<=numberCases; c++) { s.clear(); cin >> sum; found=0; cin>> numberElements; if(numberElements==0) cout << "!OK" <<endl; else { for(long long int i = 0; i< numberElements && found==0; i++) { cin >> number; if (sum==(number+number)) { if (s.find(number)!=s.end() ) { cout<<number<<" "<<number<<endl; found=1; } } else { if(s.find(number)==s.end()) s.insert(number); temp = sum - number; if (s.find(temp)!=s.end()) { cout<<temp<<" "<<number<<endl; found=1; } } } if (found==0) cout << "!OK" <<endl; } } return 0; } `

For the following input:

`6 8 4 1 2 4 4 8 4 1 2 7 9 8 4 1 2 8 9 8 4 4 5 3 4 8 4 4 1 1 8 8 4 -1 1 9 8 `

The expected output is:

`4 4 1 7 !OK 3 5 !OK -1 9 `

whereas the output of my code is actually:

`1 7 1 8 4 5 !OK !OK `

I have been trying to detect the source of the problem in my code that causes the unwanted output, but till my efforts have risen no success. Any help would be very appreciated.