Skip to content

Instantly share code, notes, and snippets.

@janvichhabra
Created December 31, 2021 07:55
Show Gist options
  • Save janvichhabra/4ab8469fd7f482d44c47eb832ef64d66 to your computer and use it in GitHub Desktop.
Save janvichhabra/4ab8469fd7f482d44c47eb832ef64d66 to your computer and use it in GitHub Desktop.
Pairs With Given Sum In Two Sorted Matrices
Q1) What does that denote If I subtract my target with the value of an element in array2 and the value present in val 1 ?
Ans: Its denotes that the sum of both elements is equal to the target.
Q2) What would be the best data structure to solve this problem?
Ans: HashMap, which is going to store elements of array with its frequency.
Q3) What is the average time complexity of this problem?
ans: The time complexity of this code is O(N2) as we are traversing using i and j pointers where the i is traversing till n and the j pointer in the inner loop is traversing till n. .
Q4) What is the average space complexity of this problem?
Ans: The space complexity is also O(N) as we are using a Hash map to store the sum of our pairs.
Q5) What exactly is a valid pair in this question?
Ans -> A pair is called valid if one element of the pair is selected from A and the second element is selected from B.
1) Can you make the use of hashmap in this problem?
2) Make a HashMap which is going to store elements of array1 with its frequency.
3) Traverse the array and Think about it If we subtract my target with the value of an element in array2 and the value present in val 1 .
4) We are gonna check if target-value of array 2 element exist in our map
5) If yes, Then the condition is true and I am gonna simply increase my count and that will be equal to number of frequencies as might have multiple similar elements in our array
Q1) what we are stored into our HashMap ?
1. Sum with their Indexes
2. Sum with their occurences
3. Distinct Sum of elements
4. None of the above
Ans -> 2. Sum with their occurences
Q2) What exactly is a valid pair in this question?
1. if one element of the pair is selected from Array A and the second element is selected from array B.
2. if both elements are selected from array A
3. if both elements are selected from array B
4. None Of the above
Ans -> 1. if one element of the pair is selected from Array A and the second element is selected from array B.
Q3) What is the average time complexity of this problem?
a) O(n*m)
b) O(n*n)
c) O(n)
d) O(1)
Ans: b
Q4) What is the average space complexity of this problem?
a) O(n*n)
b) O(1)
c) O(n)
d) None of the above
Ans: c
Q5) What would be the best data structure to solve this problem?
a) Array
b) Queue
c) stack
d) HashMap
Ans: d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment