248.Given two sorted positive integer arrays A(n) and B(n)
Microsoft Interview Questions and Answers
(Continued from previous question...)
248.Given two sorted positive integer arrays A(n) and B(n)
Question:
Given two sorted positive integer arrays A(n) and B(n), we define a set S = {(a,b) | a \in A and b
\in B}. Obviously there are n2 elements in S. The value of such a pair is defined as Val(a,b) = a +
b. Now we want to get the n pairs from S with largest values.
maybe an answer:
1)A[n]+B[n] will be the largest as the arrays r sorted.
2)Compare x=A[n]+B[n-1] and y=A[n-1]+B[n]...whichever is bigger will be the next number.
3)if x>y then x=A[n]+B[n-2]
else y=A[n-2]+B[n]
4)keep comapring the elements in this fashion..untill u get n nos.
5) this will take O(n) time i guess
(Continued on next question...)
Other Interview Questions
- Input is a matrix of size n x m of 0s and 1s......
- 246. An array is given like {1,4,5,2,3,6,7}
- 239. create your own atoi()?
- Given the following prototype:int compact(int * p, int size) ...
- Implement the function ....
- Given 2 sorted integer arrays, find the intersecting element in them.
- Given n arrays, find n number such that sum of their differences is minimum....
- 151. Print all possible palindromes(of length >2) for a given string.
- Given 2 linked lists, merge them in-plac....
- How would you reverse a doubly-linked list?
- More...
|