Skip to content

Commit 69aacc8

Browse files
Merge K Sorted Array problem from gfg is added
1 parent fb2f32f commit 69aacc8

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
id: merge-k-sorted-arrays
3+
title: Merge K Sorted Arrays
4+
sidebar_label: Merge-K-Sorted-Arrays
5+
tags:
6+
- Sorting
7+
- Algorithms
8+
description: "This tutorial covers the solution to the Merge K Sorted Arrays problem from the GeeksforGeeks."
9+
---
10+
## Problem Description
11+
Given `k` sorted arrays arranged in the form of a matrix of size `k * k`. The task is to merge them into one sorted array. Return the merged sorted array
12+
13+
## Examples
14+
15+
**Example 1:**
16+
17+
```
18+
Input: k = 3, arr[][] = {{1,2,3},{4,5,6},{7,8,9}}
19+
Output: 1 2 3 4 5 6 7 8 9
20+
Explanation: Above test case has 3 sorted arrays of size 3, 3, 3 arr[][] = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]. The merged list will be [1, 2, 3, 4, 5, 6, 7, 8, 9].
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: k = 4, arr[][]={{1,2,3,4},{2,2,3,4},{5,5,6,6},{7,8,9,9}}
27+
Output: 1 2 2 2 3 3 4 4 5 5 6 6 7 8 9 9
28+
Explanation: Above test case has 4 sorted arrays of size 4, 4, 4, 4 arr[][] = [[1, 2, 2, 2], [3, 3, 4, 4], [5, 5, 6, 6], [7, 8, 9, 9 ]]. The merged list will be [1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 9, 9].
29+
```
30+
31+
## Constraints
32+
33+
* `1 <= k <= 100`
34+
35+
## Problem Explanation
36+
Given k sorted arrays arranged in the form of a matrix of size k * k. The task is to merge them into one sorted array. Return the merged sorted array
37+
38+
## Code Implementation
39+
40+
### C++ Solution
41+
42+
```cpp
43+
#include <iostream>
44+
#include <vector>
45+
#include <algorithm>
46+
47+
std::vector<int> mergeSortedArrays(std::vector<std::vector<int>> arrays) {
48+
std::vector<int> result;
49+
for (const auto& array : arrays) {
50+
result.insert(result.end(), array.begin(), array.end());
51+
}
52+
std::sort(result.begin(), result.end());
53+
return result;
54+
}
55+
56+
int main() {
57+
std::vector<std::vector<int>> arrays = { { 1, 3, 5 }, { 2, 4, 6 }, { 0, 7, 8 } };
58+
std::vector<int> result = mergeSortedArrays(arrays);
59+
for (int num : result) {
60+
std::cout << num << " ";
61+
}
62+
return 0;
63+
}
64+
65+
66+
```
67+
68+
```java
69+
70+
71+
import java.util.Arrays;
72+
73+
public class Main {
74+
public static void main(String[] args) {
75+
int[][] arrays = { { 1, 3, 5 }, { 2, 4, 6 }, { 0, 7, 8 } };
76+
int[] result = mergeSortedArrays(arrays);
77+
System.out.println(Arrays.toString(result));
78+
}
79+
80+
public static int[] mergeSortedArrays(int[][] arrays) {
81+
int[] result = new int[arrays.length * arrays[0].length];
82+
int index = 0;
83+
for (int[] array : arrays) {
84+
for (int num : array) {
85+
result[index++] = num;
86+
}
87+
}
88+
Arrays.sort(result);
89+
return result;
90+
}
91+
}
92+
93+
94+
```
95+
96+
```python
97+
import itertools
98+
99+
def merge_sorted_arrays(arrays):
100+
return sorted(itertools.chain(*arrays))
101+
102+
```
103+
104+
```javascript
105+
106+
function mergeSortedArrays(arrays) {
107+
return arrays.flat().sort((a, b) => a - b);
108+
}
109+
110+
111+
```
112+
113+
## Solution Logic:
114+
115+
116+
1. Flatten the 2D array:
117+
- Iterate through each subarray in the 2D array.
118+
- Concatenate each subarray to a single array.
119+
2. Sort the 1D array:
120+
- Use a sorting algorithm (e.g. quicksort, mergesort) to sort the array in ascending order.
121+
3. Return the sorted 1D array:
122+
- Return the sorted array as the final result.
123+
124+
## Time Complexity
125+
126+
* The time complexity is $O(n*log(n))$ where n is the total number of elements in the arrays.
127+
128+
## Space Complexity
129+
130+
* $O(n)$ where n is the total number of elements in the arrays.

0 commit comments

Comments
 (0)