Pair Sum

Easy
0/40
Average time to solve is 15m
profile
Contributed by
126 upvotes
Asked in companies
AdobeGoldman SachsFacebook

Problem statement

You are given an array/list β€˜ARR’ consisting of β€˜N’ distinct integers arranged in ascending order. You are also given an integer β€˜TARGET’. Your task is to count all the distinct pairs in β€˜ARR’ such that their sum is equal to β€˜TARGET’.

Note:

1. Pair (x,y) and Pair(y,x) are considered as the same pair. 

2. If there exists no such pair with sum equals to 'TARGET', then return -1.

Example:

Let β€˜ARR’ = [1 2 3] and β€˜TARGET’ = 4. Then, there exists only one pair in β€˜ARR’ with a sum of 4 which is (1, 3). (1, 3) and (3, 1) are counted as only one pair.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer β€˜T’ which denotes the number of test cases. 

The first line of each test case contains two single space-separated integers β€˜N’ and β€˜TARGET’ representing the number of elements in the array/list β€˜ARR’ and the required pair-sum respectively.

The next line of each test case contains β€˜N’ single space-separated integers denoting the elements of  β€˜ARR’.
Output Format :
For each test case, return the numbers of pairs in  β€˜ARR’ whose sum is equal to β€˜TARGET’.
Note:
You don't need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= β€˜T’ <= 100
2 <= β€˜N’ <= 5000
1 <= β€˜ARR[i]’, β€˜TARGET’ <= 10^5

Where ARR[i]’ represents the elements of array/list β€˜ARR’. 

Time Limit: 1 sec
Sample Input 1:
2
5 6
1 2 3 4 5
6 7
1 2 3 4 5 6
Sample Output 1:
2
3

Explanation for Sample Output 1:

In test case 1, there exist only 2 pairs whose sum is equal to β€˜TARGET’ i.e (1, 5) and (2, 4).

In test case 2, there are 3 pairs whose sum is equal to β€˜TARGET’ which are  (1, 6), (2, 5), and (3, 4).
Sample Input 2:
2
4 10
1 3 5 6
5 12
1 3 6 9 11
Sample Output 2:
-1
 2

Explanation for Sample Output 2:

In test case 1, there is not a pair whose sum is equal to β€˜TARGET’. So we return -1.

In test case 2, there are 2 pairs whose sum is equal to β€˜TARGET’, (1, 11) and (3, 9) respectively.
Hint

Try to traverse through every pair possible.

Approaches (2)
Brute Force

First, we declare a variable 'COUNTPAIR’ in which we store all pairs whose sum is equal to 'TARGET’. Then, we traverse the array β€˜ARR’ and assume every element as the first element of the pair. Then we again traverse the remaining array and consider every element as a second element of the pair, and check whether the sum of the two elements is equal to 'TARGET' or not. If it is equal to 'TARGET',’ then we increase our β€˜COUNTPAIR’ by 1.

 

The steps are as follows:

 

  1. We declare a variable β€˜COUNTPAIR’ and initialize it with 0.
  2. We run a loop for β€˜i’ = 0 to β€˜N’ - 1:
    • We run a loop for β€˜j’ = β€˜i’ + 1 to β€˜N’:
      • If β€˜ARR[i]’ + β€˜ARR[j]’ is equal to 'TARGET':
        • β€˜COUNTPAIR’++
        • We break the loop as the elements are sorted and distinct. So, the pair sum will increase and can't be equal to β€˜TARGET’.
  3. If β€˜COUNTPAIR’ is equal to β€˜0’:
    • Then, Return - 1.
  4. Else
    • Return β€˜COUNTPAIR’.
Time Complexity

O(N ^ 2), Where β€˜N’ represents the number of elements in the array/list β€˜ARR’.

 

Since we are using two nested loops for finding a pair, thus the time complexity will be O(N ^ 2). 

Space Complexity

O(1).

 

Since we are not using any extra space for finding our resultant answer. Thus the space complexity will be O(1).

Code Solution
(100% EXP penalty)
Pair Sum
Full screen
Console