Let A, B and C be three arrays of n integers each. I want to find 3 integers a,b,c s.t. a belongs to A, b belongs to B, c belongs to C and c=a+b.
Approach:
- Calculate all possible sums a+b and store it in hash map. Time Complexity = O(n^2)
- Parse through array C and check if element is present in hash map or not.
This approach requires O(n^2) space and O(n^2) time complexity. Can it be optimized to find a,b,c in O(n^2) time without extra space(ie- Space Complexity = O(1)) ?