


If more than one such pair of indices exist, return the lexicographically smallest pair
You may not use the same element twice.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case or query contains an integer 'N' representing the size of the array (Arr).
The second line contains 'N' single space-separated integers, representing the elements in the array.
The third line contains the value of the target.
For each test case, print the indices of the elements from the array whose sum is equal to the target.
The output for each test case is printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
2 <= N <= 5 * 10^4
-10^5 <= Arr[i] <= 10^5
2 * (-10^5) <= target <= 2 * 10^5
Where T is the number of test cases, N is the size of the input array, Arr[i] refers to the elements of the array and target is the given value of the sum.
The idea is to explore all the possible pairs of indices of the array and check whether the sum of any of the pair elements sums up to the target value.
Algorithm :
The idea is to check if target - Arr[i] exists for any element x in the array. This lookup of target - Arr[i] can be accomplished by creating a hash table, such that the key is the element of the array and the corresponding value is the list of indices at which the element is present.
A simple implementation uses two iterations. In the first iteration, we add each element’s value and its index to the hash table. Then, in the second iteration we check if each element’s complement (target - Arr[i]) exists in the table. Beware that the complement of an element Arr[i] is not the same element itself i.e. the complement should be present at any index other than ‘i’.
Algorithm :
Note: We are traversing the array and adding the indices in the hash map. Thus, the list of indices mapped by an element in the map will always be sorted. Thus, the first pair that we find in step 3 will be the lexicographically smallest.
We can use hashing to find the required pair of indices in one-pass. The hash map will have the value of the elements of the array as key and the first occurring index of the element as the value corresponding to the key.
While we are iterating and inserting elements into the table, we also look back to check if (target - current element) already exists in the table. If it exists, we have found a solution and return immediately, else we keep on iterating and filling our hash table.
Algorithm :