AfterAcademy Tech
•
27 Sep 2019

Difficulty Level : Medium
Asked in : Google, Facebook, Amazon
Problem Description: Given an array of n integers and given a number K, determines whether there is a pair of elements in the array that sums to exactly K.
For example :
Input : A[] = [-5, 1, -40, 20, 6, 8, 7 ], K=15
Output: true ( 7, 8 and -5, 20 are the pairs with sum 15)
Input : A[] = [-5, 4, -2, 16, 8, 9], K=15
Output: false (There is no pair of elements whose sum is equal to 15)
Possible follow-up questions to ask the interviewer :
We are discussing four ways to solve this problem :
Use two loops and check A[i] + A[j] == K for each pair (i, j) in A[]. If there exists a pair with sum equals to K then return true. By end of both loops, If you didn’t find such a pair then return false.
Pseudo Code
int find_sumPair (A[], n, K)
{
for( i = 0 to n-1 )
{
for(j = i+1 to n-1)
{
if(A[i]+A[j] == K)
return 1
}
}
return -1
}
Complexity Analysis
The total no. of comparison in worst case = Total no. of possible pairs = nC2 = n(n-1)/2 = O(n²)
Time Complexity = O(n²) and Space Complexity = O(1). Can we improve the time complexity? Let's try to think about it.
The idea of binary search works perfectly in the case of the sorted array. We can sort the A[] and for each value A[i], search whether there is another value K-A[i] present in the array or not. Binary search performs searching in O(logn) which could help us to improve the time complexity.
Solution Steps
Pseudo Code
int find_sumPair (A[], n, K)
{
Sort ( A, n)
for( i = 0 to n-1 )
{
x = binarySearch ( A, 0, n-1, K-A[i] )
if ( x )
return 1
}
return -1
}
Complexity Analysis
Suppose we are using heap sort/merge sort and iterative binary search for the implementation.
Time Complexity = Time complexity of sorting + n. Time complexity of Binary Search = O(nlogn) + n. O(logn) = O(nlogn)
Space Complexity = Space Complexity of sorting + Space complexity of the binary search (Iterative Implementation)
If merge sort ,Space Complexity = O(n) + O(1) = O(n)
If Heap Sort, Space Complexity = O(1) + O(1) = O(1)
Critical Ideas to think!
Sort the array A[] then walk two pointers inward from the ends of the array, at each point looking at their sum. If it is exactly k, then we are done. If it exceeds k, then any sum using the larger element is too large, so we walk that pointer inwards. If it is less than k, then any sum using the lower element is too small, so we walk that pointer inwards.
Solution Steps
3) Run a loop while i< j.
4) If didn’t find such a pair in the whole array - return false.
Solution Visualization

Pseudo Code
int find_sumPair( A[], n, K)
{
Sort(A, n)
i = 0
j = n -1
while (i < j)
{
if(A[i] + A[j] == K)
return 1
else if(A[i] + A[j] < K)
i = i+1
else
j = j-1
}
return -1
}
Complexity Analysis
Suppose we are using Heap Sort or merge sort.
Time Complexity = Time complexity of sorting + Time complexity of two pointer approach (while loop) = O (n log n) + O(n) = O (n log n)
If merge sort, Space Complexity = O(n)
If Heap Sort, Space Complexity = O(1)
Critical Ideas to think!
Idea is to use the Hash Table to improve the time complexity because it performs searching and insertion efficiently in O(1) average.
Solution Steps
3. If you didn’t find such a pair by end of the loop then return false.
Pseudo Code
bool checkPair(int A[], int n, int K)
{
Take Hash Table H of size O(n)
for (i = 0 to n-1)
{
int x = K - A[i]
if (H.search(x) is true)
return 1
H.insert(A[i])
}
return -1
}
Complexity Analysis
In worst case scenario, we scan the whole array and didn't find any such pair. Time Complexity = Time complexity of inserting n elements in hash table + Time complexity of searching (K-A[i]) n times in hash table = n. O(1) + n . O(1) = O(n)
Space Complexity = Extra space of Hash Table = O(n)
Critical Ideas to think!

Please write comments if you find any error or bug. Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given an array of integers A[ ], if i < j and A[i] > A[j] then the pair (i, j) is called the inversion of an array A[ ]. You need to write a program to find the total counts of inversion in an array A[ ]. The inversion count of an array suggests how close the array is from being sorted. This problem has been asked in Amazon and Google's interview.

AfterAcademy Tech
Given two integer array A[] and B[] of size m and n(n <= m) respectively. We have to check whether B[] is a subset of A[] or not. An array B is a subset of another array A if each element of B is present in A. (There are no repeated elements in both the arrays)

AfterAcademy Tech
Given two given arrays of equal length, Check if given arrays are equal or not. This is a tricky interview problem as, at first, this looks like problem-related to combinatorics but could be solved quickly upon thinking.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon and Microsoft. Given a Linked List you have to swap nodes of the list in pairs.
