[Morgan Stanley] sub-array sum with 2 arrays

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2023-05-29Added By Anonymous
Source: Link
Morgan Stanley | unknown | unknown | unknown | unknown

Gien two array A & B, count the sub-array combination sum (from both arrays) equal to a target value k. eg: A = {5,2,1,6,4}; B = {3,5}; k=10 Ans: 4 Explanation: The subarray combinations with sum 10 are: a: {5, 2}. b: {3} a: {1, 6}. b: {3} a: {5}. b: {5} a: {2}. b: {3, 5} At first this looks sub-array sum variation but the thing is here we have 2 arrays. I tried to apply prefix sum logic and then do a brute-foce to find different possible sub-array sum combination to it but not able to find an optimal solution to cover all cases. Thoughts ? Any better approach to solve this question? Edit: Constraints: 1< A.size(),B.size() < 10000 1< A[i],B[j] < 10000 Essential array size and each element can range between [1,10000].
Comments