Given K sorted arrays of size N each, merge them and print the sorted output.
Examples:
Input: K = 3, N = 4, arr = { {1, 3, 5, 7}, {2, 4, 6, 8}, {0, 9, 10, 11}}
Output: 0 1 2 3 4 5 6 7 8 9 10 11
Explanation: The output array is a sorted array that contains all the elements of the input matrix.
Input: k = 4, n = 4, arr = { {1, 5, 6, 8}, {2, 4, 10, 12}, {3, 7, 9, 11}, {13, 14, 15, 16}}
Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Explanation: The output array is a sorted array that contains all the elements of the input matrix.
Naive Approach for Merging k sorted arrays:
Create an output array of size (N * K) and then copy all the elements into the output array followed by sorting.
Follow the given steps to solve the problem:
- Create an output array of size N * K.
- Traverse the matrix from start to end and insert all the elements in the output array.
- Sort and print the output array.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
#define N 4
void printArray( int arr[], int size)
{
for ( int i = 0; i < size; i++)
cout << arr[i] << " " ;
}
void mergeKArrays( int arr[][N], int a, int output[])
{
int c = 0;
for ( int i = 0; i < a; i++) {
for ( int j = 0; j < N; j++)
output = arr[i][j];
}
sort(output, output + N * a);
}
int main()
{
int arr[][N] = { { 2, 6, 12, 34 },
{ 1, 9, 20, 1000 },
{ 23, 34, 90, 2000 } };
int K = sizeof (arr) / sizeof (arr[0]);
int output[N * K];
mergeKArrays(arr, 3, output);
cout << "Merged array is " << endl;
printArray(output, N * K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void mergeKArrays( int [][] arr, int a,
int [] output)
{
int c = 0 ;
for ( int i = 0 ; i < a; i++) {
for ( int j = 0 ; j < 4 ; j++)
output = arr[i][j];
}
Arrays.sort(output);
}
public static void printArray( int [] arr, int size)
{
for ( int i = 0 ; i < size; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int [][] arr = { { 2 , 6 , 12 , 34 },
{ 1 , 9 , 20 , 1000 },
{ 23 , 34 , 90 , 2000 } };
int K = 4 ;
int N = 3 ;
int [] output = new int [N * K];
mergeKArrays(arr, N, output);
System.out.println( "Merged array is " );
printArray(output, N * K);
}
}
|
Python3
def mergeKArrays(arr, a, output):
c = 0
for i in range (a):
for j in range ( 4 ):
output = arr[i][j]
c + = 1
output.sort()
def printArray(arr, size):
for i in range (size):
print (arr[i], end = " " )
if __name__ = = '__main__' :
arr = [[ 2 , 6 , 12 , 34 ], [ 1 , 9 , 20 , 1000 ], [ 23 , 34 , 90 , 2000 ]]
K = 4
N = 3
output = [ 0 for i in range (N * K)]
mergeKArrays(arr, N, output)
print ( "Merged array is " )
printArray(output, N * K)
|
C#
using System;
public class GFG {
public static void mergeKArrays( int [, ] arr, int a,
int [] output)
{
int c = 0;
for ( int i = 0; i < a; i++) {
for ( int j = 0; j < 4; j++)
output = arr[i, j];
}
Array.Sort(output);
}
public static void printArray( int [] arr, int size)
{
for ( int i = 0; i < size; i++)
Console.Write(arr[i] + " " );
}
public static void Main(String[] args)
{
int [, ] arr = { { 2, 6, 12, 34 },
{ 1, 9, 20, 1000 },
{ 23, 34, 90, 2000 } };
int K = 4;
int N = 3;
int [] output = new int [N * K];
mergeKArrays(arr, N, output);
Console.WriteLine( "Merged array is " );
printArray(output, N * K);
}
}
|
Javascript
function mergeKArrays(arr , a, output)
{
var c = 0;
for (i = 0; i < a; i++) {
for (j = 0; j < 4; j++)
output = arr[i][j];
}
output.sort((a,b)=>a-b);
}
function printArray(arr , size) {
for (i = 0; i < size; i++)
document.write(arr[i] + " " );
}
var arr = [ [ 2, 6, 12, 34 ],
[ 1, 9, 20, 1000 ],
[ 23, 34, 90, 2000 ] ];
var K = 4;
var N = 3;
var output = Array(N * K).fill(0);
mergeKArrays(arr, N, output);
document.write( "Merged array is " );
printArray(output, N * K);
|
Output
Merged array is
1 2 6 9 12 20 23 34 34 90 1000 2000
Time Complexity: O(N * K * log (N*K)), Since the resulting array is of size N*K.
Space Complexity: O(N * K), The output array is of size N * K.
Merge K sorted arrays using merging:
The process begins with merging arrays into groups of two. After the first merge, there will be K/2 arrays remaining. Again merge arrays in groups, now K/4 arrays will be remaining. This is similar to merge sort. Divide K arrays into two halves containing an equal number of arrays until there are two arrays in a group. This is followed by merging the arrays in a bottom-up manner.
Follow the given steps to solve the problem:
- Create a recursive function that takes K arrays and returns the output array.
- In the recursive function, if the value of K is 1 then return the array else if the value of K is 2 then merge the two arrays in linear time and return the array.
- If the value of K is greater than 2 then divide the group of k elements into two equal halves and recursively call the function, i.e 0 to K/2 array in one recursive function and K/2 to K array in another recursive function.
- Print the output array.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
#define N 4
void mergeArrays( int arr1[], int arr2[], int N1, int N2,
int arr3[])
{
int i = 0, j = 0, k = 0;
while (i < N1 && j < N2) {
if (arr1[i] < arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}
while (i < N1)
arr3[k++] = arr1[i++];
while (j < N2)
arr3[k++] = arr2[j++];
}
void printArray( int arr[], int size)
{
for ( int i = 0; i < size; i++)
cout << arr[i] << " " ;
}
void mergeKArrays( int arr[][N], int i, int j, int output[])
{
if (i == j) {
for ( int p = 0; p < N; p++)
output[p] = arr[i][p];
return ;
}
if (j - i == 1) {
mergeArrays(arr[i], arr[j], N, N, output);
return ;
}
int out1[N * (((i + j) / 2) - i + 1)],
out2[N * (j - ((i + j) / 2))];
mergeKArrays(arr, i, (i + j) / 2, out1);
mergeKArrays(arr, (i + j) / 2 + 1, j, out2);
mergeArrays(out1, out2, N * (((i + j) / 2) - i + 1),
N * (j - ((i + j) / 2)), output);
}
int main()
{
int arr[][N] = { { 2, 6, 12, 34 },
{ 1, 9, 20, 1000 },
{ 23, 34, 90, 2000 } };
int K = sizeof (arr) / sizeof (arr[0]);
int output[N * K];
mergeKArrays(arr, 0, 2, output);
cout << "Merged array is " << endl;
printArray(output, N * K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static final int N = 4 ;
static void mergeArrays( int arr1[], int arr2[], int N1,
int N2, int arr3[])
{
int i = 0 , j = 0 , k = 0 ;
while (i < N1 && j < N2) {
if (arr1[i] < arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}
while (i < N1)
arr3[k++] = arr1[i++];
while (j < N2)
arr3[k++] = arr2[j++];
}
static void printArray( int arr[], int size)
{
for ( int i = 0 ; i < size; i++)
System.out.print(arr[i] + " " );
}
static void mergeKArrays( int arr[][], int i, int j,
int output[])
{
if (i == j) {
for ( int p = 0 ; p < N; p++)
output[p] = arr[i][p];
return ;
}
if (j - i == 1 ) {
mergeArrays(arr[i], arr[j], N, N, output);
return ;
}
int [] out1 = new int [N * (((i + j) / 2 ) - i + 1 )];
int [] out2 = new int [N * (j - ((i + j) / 2 ))];
mergeKArrays(arr, i, (i + j) / 2 , out1);
mergeKArrays(arr, (i + j) / 2 + 1 , j, out2);
mergeArrays(out1, out2, N * (((i + j) / 2 ) - i + 1 ),
N * (j - ((i + j) / 2 )), output);
}
public static void main(String[] args)
{
int arr[][] = { { 2 , 6 , 12 , 34 },
{ 1 , 9 , 20 , 1000 },
{ 23 , 34 , 90 , 2000 } };
int K = arr.length;
int [] output = new int [N * K];
mergeKArrays(arr, 0 , 2 , output);
System.out.print( "Merged array is "
+ "\n" );
printArray(output, N * K);
}
}
|
Python3
N = 4
def mergeArrays(arr1, arr2, N1, N2, arr3):
i, j, k = 0 , 0 , 0
while (i < N1 and j < N2):
if (arr1[i] < arr2[j]):
arr3[k] = arr1[i]
k + = 1
i + = 1
else :
arr3[k] = arr2[j]
k + = 1
j + = 1
while (i < N1):
arr3[k] = arr1[i]
k + = 1
i + = 1
while (j < N2):
arr3[k] = arr2[j]
k + = 1
j + = 1
def printArray(arr, size):
for i in range (size):
print (arr[i], end = " " )
def mergeKArrays(arr, i, j, output):
global N
if (i = = j):
for p in range (N):
output[p] = arr[i][p]
return
if (j - i = = 1 ):
mergeArrays(arr[i], arr[j],
N, N, output)
return
out1 = [ 0 for i in range (N * (((i + j) / / 2 ) - i + 1 ))]
out2 = [ 0 for i in range (N * (j - ((i + j) / / 2 )))]
mergeKArrays(arr, i, (i + j) / / 2 , out1)
mergeKArrays(arr, (i + j) / / 2 + 1 , j, out2)
mergeArrays(out1, out2,
N * (((i + j) / 2 ) - i + 1 ),
N * (j - ((i + j) / 2 )), output)
if __name__ = = '__main__' :
arr = [[ 2 , 6 , 12 , 34 ],
[ 1 , 9 , 20 , 1000 ],
[ 23 , 34 , 90 , 2000 ]]
K = len (arr)
output = [ 0 for i in range (N * K)]
mergeKArrays(arr, 0 , 2 , output)
print ( "Merged array is " )
printArray(output, N * K)
|
C#
using System;
class GFG {
static readonly int N = 4;
public static int [] GetRow( int [, ] matrix, int row)
{
var rowLength = matrix.GetLength(1);
var rowVector = new int [rowLength];
for ( var i = 0; i < rowLength; i++)
rowVector[i] = matrix[row, i];
return rowVector;
}
static void mergeArrays( int [] arr1, int [] arr2, int N1,
int N2, int [] arr3)
{
int i = 0, j = 0, k = 0;
while (i < N1 && j < N2) {
if (arr1[i] < arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}
while (i < N1)
arr3[k++] = arr1[i++];
while (j < N2)
arr3[k++] = arr2[j++];
}
static void printArray( int [] arr, int size)
{
for ( int i = 0; i < size; i++)
Console.Write(arr[i] + " " );
}
static void mergeKArrays( int [, ] arr, int i, int j,
int [] output)
{
if (i == j) {
for ( int p = 0; p < N; p++)
output[p] = arr[i, p];
return ;
}
if (j - i == 1) {
mergeArrays(GetRow(arr, i), GetRow(arr, j), N,
N, output);
return ;
}
int [] out1 = new int [N * (((i + j) / 2) - i + 1)];
int [] out2 = new int [N * (j - ((i + j) / 2))];
mergeKArrays(arr, i, (i + j) / 2, out1);
mergeKArrays(arr, (i + j) / 2 + 1, j, out2);
mergeArrays(out1, out2, N * (((i + j) / 2) - i + 1),
N * (j - ((i + j) / 2)), output);
}
public static void Main(String[] args)
{
int [, ] arr = { { 2, 6, 12, 34 },
{ 1, 9, 20, 1000 },
{ 23, 34, 90, 2000 } };
int K = arr.GetLength(0);
int [] output = new int [N * K];
mergeKArrays(arr, 0, 2, output);
Console.Write( "Merged array is "
+ "\n" );
printArray(output, N * K);
}
}
|
Javascript
let N = 4
function mergeArrays(arr1, arr2, N1, N2, arr3)
{
let i = 0, j = 0, k = 0;
while (i < N1 && j < N2)
{
if (arr1[i] < arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}
while (i < N1)
arr3[k++] = arr1[i++];
while (j < N2)
arr3[k++] = arr2[j++];
}
function printArray(arr, size)
{
for (let i = 0; i < size; i++)
document.write(arr[i] + " " );
}
function mergeKArrays(arr, i, j, output)
{
if (i == j)
{
for (let p = 0; p < N; p++)
output[p] = arr[i][p];
return ;
}
if (j - i == 1)
{
mergeArrays(arr[i], arr[j],
N, N, output);
return ;
}
let out1 = new Array(N * (((i + j) / 2) - i + 1))
let out2 = new Array(N * (j - ((i + j) / 2)));
mergeKArrays(arr, i, (i + j) / 2, out1);
mergeKArrays(arr, (i + j) / 2 + 1, j, out2);
mergeArrays(out1, out2,
N * (((i + j) / 2) - i + 1),
N * (j - ((i + j) / 2)), output);
}
let arr = [ [ 2, 6, 12, 34 ],
[ 1, 9, 20, 1000 ],
[ 23, 34, 90, 2000 ] ];
let K = arr.length;
let output = new Array(N * K);
mergeKArrays(arr, 0, 2, output);
document.write( "Merged array is " + "<br>" );
printArray(output, N * K);
|
Output
Merged array is
1 2 6 9 12 20 23 34 34 90 1000 2000
Time Complexity: O(N * K * log K). There are log K levels as in each level the K arrays are divided in half and at each level, the K arrays are traversed.
Auxiliary Space: O(N * K * log K). In each level O(N * K) space is required.
Merge K sorted arrays using Min-Heap:
The idea is to use Min Heap. This MinHeap based solution has the same time complexity which is O(NK log K). But for a different and particular sized array, this solution works much better. The process must start with creating a MinHeap and inserting the first element of all the k arrays. Remove the root element of Minheap and put it in the output array and insert the next element from the array of removed element. To get the result the step must continue until there is no element left in the MinHeap.
Follow the given steps to solve the problem:
- Create a min Heap and insert the first element of all the K arrays.
- Run a loop until the size of MinHeap is greater than zero.
- Remove the top element of the MinHeap and print the element.
- Now insert the next element from the same array in which the removed element belonged.
- If the array doesn’t have any more elements, then replace root with infinite. After replacing the root, heapify the tree.
- Return the output array
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 4
struct MinHeapNode {
int element;
int i;
int j;
};
void swap(MinHeapNode* x, MinHeapNode* y);
class MinHeap {
MinHeapNode* harr;
int heap_size;
public :
MinHeap(MinHeapNode a[], int size);
void MinHeapify( int );
int left( int i) { return (2 * i + 1); }
int right( int i) { return (2 * i + 2); }
MinHeapNode getMin() { return harr[0]; }
void replaceMin(MinHeapNode x)
{
harr[0] = x;
MinHeapify(0);
}
};
int * mergeKArrays( int arr[][N], int K)
{
int * output = new int [N * K];
MinHeapNode* harr = new MinHeapNode[K];
for ( int i = 0; i < K; i++) {
harr[i].element = arr[i][0];
harr[i].i = i;
harr[i].j = 1;
}
MinHeap hp(harr, K);
for ( int count = 0; count < N * K; count++) {
MinHeapNode root = hp.getMin();
output[count] = root.element;
if (root.j < N) {
root.element = arr[root.i][root.j];
root.j += 1;
}
else
root.element = INT_MAX;
hp.replaceMin(root);
}
return output;
}
MinHeap::MinHeap(MinHeapNode a[], int size)
{
heap_size = size;
harr = a;
int i = (heap_size - 1) / 2;
while (i >= 0) {
MinHeapify(i);
i--;
}
}
void MinHeap::MinHeapify( int i)
{
int l = left(i);
int r = right(i);
int smallest = i;
if (l < heap_size && harr[l].element < harr[i].element)
smallest = l;
if (r < heap_size
&& harr[r].element < harr[smallest].element)
smallest = r;
if (smallest != i) {
swap(&harr[i], &harr[smallest]);
MinHeapify(smallest);
}
}
void swap(MinHeapNode* x, MinHeapNode* y)
{
MinHeapNode temp = *x;
*x = *y;
*y = temp;
}
void printArray( int arr[], int size)
{
for ( int i = 0; i < size; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[][N] = { { 2, 6, 12, 34 },
{ 1, 9, 20, 1000 },
{ 23, 34, 90, 2000 } };
int K = sizeof (arr) / sizeof (arr[0]);
int * output = mergeKArrays(arr, K);
cout << "Merged array is " << endl;
printArray(output, N * K);
return 0;
}
|
Java
class MinHeapNode {
int element;
int i;
int j;
public MinHeapNode( int element, int i, int j)
{
this .element = element;
this .i = i;
this .j = j;
}
};
class MinHeap {
MinHeapNode[] harr;
int heap_size;
public MinHeap(MinHeapNode a[], int size)
{
heap_size = size;
harr = a;
int i = (heap_size - 1 ) / 2 ;
while (i >= 0 ) {
MinHeapify(i);
i--;
}
}
void MinHeapify( int i)
{
int l = left(i);
int r = right(i);
int smallest = i;
if (l < heap_size
&& harr[l].element < harr[i].element)
smallest = l;
if (r < heap_size
&& harr[r].element < harr[smallest].element)
smallest = r;
if (smallest != i) {
swap(harr, i, smallest);
MinHeapify(smallest);
}
}
int left( int i) { return ( 2 * i + 1 ); }
int right( int i) { return ( 2 * i + 2 ); }
MinHeapNode getMin()
{
if (heap_size <= 0 ) {
System.out.println( "Heap underflow" );
return null ;
}
return harr[ 0 ];
}
void replaceMin(MinHeapNode root)
{
harr[ 0 ] = root;
MinHeapify( 0 );
}
void swap(MinHeapNode[] arr, int i, int j)
{
MinHeapNode temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void printArray( int [] arr)
{
for ( int i : arr)
System.out.print(i + " " );
System.out.println();
}
static void mergeKSortedArrays( int [][] arr, int K)
{
MinHeapNode[] hArr = new MinHeapNode[K];
int resultSize = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
MinHeapNode node
= new MinHeapNode(arr[i][ 0 ], i, 1 );
hArr[i] = node;
resultSize += arr[i].length;
}
MinHeap mh = new MinHeap(hArr, K);
int [] result
= new int [resultSize];
for ( int i = 0 ; i < resultSize; i++) {
MinHeapNode root = mh.getMin();
result[i] = root.element;
if (root.j < arr[root.i].length)
root.element = arr[root.i][root.j++];
else
root.element = Integer.MAX_VALUE;
mh.replaceMin(root);
}
printArray(result);
}
public static void main(String args[])
{
int [][] arr = { { 2 , 6 , 12 , 34 },
{ 1 , 9 , 20 , 1000 },
{ 23 , 34 , 90 , 2000 } };
System.out.println( "Merged array is :" );
mergeKSortedArrays(arr, arr.length);
}
};
|
Python3
import sys
class MinHeapNode :
element = 0
i = 0
j = 0
def __init__( self , element, i, j) :
self .element = element
self .i = i
self .j = j
class MinHeap :
harr = None
heap_size = 0
def __init__( self , a, size) :
self .heap_size = size
self .harr = a
i = int (( self .heap_size - 1 ) / 2 )
while (i > = 0 ) :
self .MinHeapify(i)
i - = 1
def MinHeapify( self , i) :
l = self .left(i)
r = self .right(i)
smallest = i
if (l < self .heap_size and self .harr[l].element < self .harr[i].element) :
smallest = l
if (r < self .heap_size and self .harr[r].element < self .harr[smallest].element) :
smallest = r
if (smallest ! = i) :
self .swap( self .harr, i, smallest)
self .MinHeapify(smallest)
def left( self , i) :
return ( 2 * i + 1 )
def right( self , i) :
return ( 2 * i + 2 )
def getMin( self ) :
if ( self .heap_size < = 0 ) :
print ( "Heap underflow" )
return None
return self .harr[ 0 ]
def replaceMin( self , root) :
self .harr[ 0 ] = root
self .MinHeapify( 0 )
def swap( self , arr, i, j) :
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
@staticmethod
def printArray( arr) :
for i in arr :
print ( str (i) + " " , end = "")
print ()
@staticmethod
def mergeKSortedArrays( arr, K) :
hArr = [ None ] * (K)
resultSize = 0
i = 0
while (i < len (arr)) :
node = MinHeapNode(arr[i][ 0 ], i, 1 )
hArr[i] = node
resultSize + = len (arr[i])
i + = 1
mh = MinHeap(hArr, K)
result = [ 0 ] * (resultSize)
i = 0
while (i < resultSize) :
root = mh.getMin()
result[i] = root.element
if (root.j < len (arr[root.i])) :
root.element = arr[root.i][root.j]
root.j + = 1
else :
root.element = sys.maxsize
mh.replaceMin(root)
i + = 1
MinHeap.printArray(result)
@staticmethod
def main( args) :
arr = [[ 2 , 6 , 12 , 34 ], [ 1 , 9 , 20 , 1000 ], [ 23 , 34 , 90 , 2000 ]]
print ( "Merged array is :" )
MinHeap.mergeKSortedArrays(arr, len (arr))
if __name__ = = "__main__" :
MinHeap.main([])
|
C#
using System;
public class MinHeapNode {
public int element;
public int i;
public int j;
public MinHeapNode( int element, int i, int j)
{
this .element = element;
this .i = i;
this .j = j;
}
};
public class MinHeap {
MinHeapNode[] harr;
int heap_size;
public MinHeap(MinHeapNode[] a, int size)
{
heap_size = size;
harr = a;
int i = (heap_size - 1) / 2;
while (i >= 0) {
MinHeapify(i);
i--;
}
}
void MinHeapify( int i)
{
int l = left(i);
int r = right(i);
int smallest = i;
if (l < heap_size
&& harr[l].element < harr[i].element)
smallest = l;
if (r < heap_size
&& harr[r].element < harr[smallest].element)
smallest = r;
if (smallest != i) {
swap(harr, i, smallest);
MinHeapify(smallest);
}
}
int left( int i) { return (2 * i + 1); }
int right( int i) { return (2 * i + 2); }
MinHeapNode getMin()
{
if (heap_size <= 0) {
Console.WriteLine( "Heap underflow" );
return null ;
}
return harr[0];
}
void replaceMin(MinHeapNode root)
{
harr[0] = root;
MinHeapify(0);
}
void swap(MinHeapNode[] arr, int i, int j)
{
MinHeapNode temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void printArray( int [] arr)
{
foreach ( int i in arr) Console.Write(i + " " );
Console.WriteLine();
}
static void mergeKSortedArrays( int [, ] arr, int K)
{
MinHeapNode[] hArr = new MinHeapNode[K];
int resultSize = 0;
for ( int i = 0; i < arr.GetLength(0); i++) {
MinHeapNode node
= new MinHeapNode(arr[i, 0], i, 1);
hArr[i] = node;
resultSize += arr.GetLength(1);
}
MinHeap mh = new MinHeap(hArr, K);
int [] result
= new int [resultSize];
for ( int i = 0; i < resultSize; i++) {
MinHeapNode root = mh.getMin();
result[i] = root.element;
if (root.j < arr.GetLength(1))
root.element = arr[root.i, root.j++];
else
root.element = int .MaxValue;
mh.replaceMin(root);
}
printArray(result);
}
public static void Main(String[] args)
{
int [, ] arr = { { 2, 6, 12, 34 },
{ 1, 9, 20, 1000 },
{ 23, 34, 90, 2000 } };
Console.WriteLine( "Merged array is :" );
mergeKSortedArrays(arr, arr.GetLength(0));
}
};
|
Javascript
function countPaths(n, m, k, a) {
let cnt1 = Math.floor((n + m - 2) / 2);
let cnt2 = (n + m - 2) - cnt1;
if (n === 1 && m === 1 && a[0][0] === k) {
return 1;
}
const mp1 = {};
const mp2 = {};
function part1(i, j, cnt, zor) {
if (cnt <= 0) {
if (!mp1[zor]) {
mp1[zor] = {};
}
if (!mp1[zor][i]) {
mp1[zor][i] = {};
}
if (!mp1[zor][i][j]) {
mp1[zor][i][j] = 0;
}
mp1[zor][i][j]++;
return ;
}
if (j + 1 < m) {
part1(i, j + 1, cnt - 1, zor ^ a[i][j + 1]);
}
if (i + 1 < n) {
part1(i + 1, j, cnt - 1, zor ^ a[i + 1][j]);
}
}
function part2(i, j, cnt, zor) {
if (cnt <= 0) {
if (!mp2[zor]) {
mp2[zor] = {};
}
if (!mp2[zor][i]) {
mp2[zor][i] = {};
}
if (!mp2[zor][i][j]) {
mp2[zor][i][j] = 0;
}
mp2[zor][i][j]++;
return ;
}
if (j - 1 >= 0) {
part2(i, j - 1, cnt - 1, zor ^ a[i][j - 1]);
}
if (i - 1 >= 0) {
part2(i - 1, j, cnt - 1, zor ^ a[i - 1][j]);
}
}
part1(0, 0, cnt1, a[0][0]);
part2(n - 1, m - 1, cnt2 - 1, a[n - 1][m - 1]);
let ans = 0;
for (const zor in mp2) {
for (const i in mp
|
Output
Merged array is
1 2 6 9 12 20 23 34 34 90 1000 2000
Time Complexity: O(N * K * log K), Insertion and deletion in a Min Heap requires log K time.
Auxiliary Space: O(K), If Output is not stored then the only space required is the Min-Heap of K elements.
Merge k sorted arrays | Set 2 (Different Sized Arrays)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Feb, 2023
Like Article
Save Article