Last Updated: 10 Dec, 2021

Count Couples

Easy
Asked in companies
MicrosoftGoogle inc

Problem statement

There are two groups, one of men, and one of women with unique IDs (Each group has unique IDs but two people from different groups can have the same ID). These two groups are represented in the form of a Binary Search Tree.

Now, the IDs are handed out in such a way that a man and woman are married if and only if their IDs sum up to ‘X’. Given the two groups, find the number of married couples that exist.

Input Format:
The first line contains 'T', denoting the number of test cases.
For each Test :
The first line contains three space separated integers => X, representing the sum, and T1 and T2 representing the number of integers in the next 2 lines.
The second line contains an array A of T1 space separated integers, with a positive integer representing a node and -1 representing  a NULL value.
The third line contains an array B of T2 space separated integers, with a positive integer representing a node and -1 representing  a NULL value.

The input is given is 'Level Order'.
(Note that T1 and T2 are not the number of nodes in the BSTs and N1 = number of positive integers in A and N2 = number of positive integers in B.)
Output Format:
For each test case, print one integer, that is, the number of married couples that exist in the groups.
Note:
You are not required to print the expected output. It has already been taken care of. Just implement the function.
Constraints -
1 <= T <= 5
1 <= T1,T2 <= 10^5
1 <= A[i] <= 10^9 or A[i] = -1, i ∈ (1,T1)
1 <= B[i] <= 10^9 or B[i] = -1, i ∈ (1,T2)
1 <= X <= 10^9

Note - The sum of 'T1' and ‘T2’ over all test cases do not exceed 2 * 10^5.

Time Limit: 1 sec

Approaches

01 Approach

Approach: We can traverse all the nodes in the first BST, and for each node T in the first BST, we can search if there exists X-T in the second BST. The number of nodes for which the complement exists in the second BST is our answer.


 

Algorithm:

  • Initialize ans to 0
  • Start traversal from the root of the first BST
    • If current node is NULL, return
    • Else, if value in the current node is T
      • Traverse from root of second BST to search for X-T
      • If it is present, increment ans by 1
    • Traverse to the left child
    • Traverse to the right child
  • Return ans

02 Approach

Approach: Building upon the first approach, instead of searching the entire second BST for each node in the first BST, we can iterate over all the nodes of the second BST and store them in a set or hashmap. Now, we traverse the first BST, and for each node T, we can check in amortized constant time if X-T is present in the map or not. This is a tradeoff as we use memory to save time.


 

Algorithm:

  • Initialize ans to 0
  • Start traversal from the root of the second BST
    • If current node is NULL, return
    • Else, if value in the current node is T, store in a hashmap
    • Traverse to the left child
    • Traverse to the right child
  • Start traversal from the root of the first BST
    • If current node is NULL, return
    • Else, if value in the current node is T
      • If X-T is present in the hashmap, increment ans by 1
    • Traverse to the left child
    • Traverse to the right child
  • Return ans