Given K sorted arrays arranged in form of a matrix of size K*N, you are required to merge them into single sorted array
belongs to collection: interview C++ coding problems/challenges | Implementation
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
belongs to collection: interview C++ coding problems/challenges | Implementation
total answers (1)
The basic naive method to approach this method to create an array of size (k*N) and store all the elements of the given matrix and then sort the entire array of given size and print that array. But the time complexity for that method would be huge as O(K*N*log(K*N)), and there is no use of the condition that the array is already sorted.
The optimized method to solve the given problem is with the help of min-heap.
Following steps are used in the heap method:
Time complexity for this approach in the worst case is: O(K*N*log(K))
Space complexity for this approach in the worst case is: O(K*N)
C++ Implementation:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer