|
| 1 | +<h1 align='center'>Merge - K Sorted - Arrays</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Merge K Sorted Arrays](https://www.geeksforgeeks.org/problems/merge-k-sorted-arrays/1) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Problem Explanation |
| 10 | +We are given `K` sorted arrays. Our goal is to merge all these sorted arrays into a single sorted array. |
| 11 | + |
| 12 | +**Example**: |
| 13 | +Let’s say we have `K = 3` arrays as follows: |
| 14 | +- `arr[0] = [1, 4, 7]` |
| 15 | +- `arr[1] = [2, 5, 8]` |
| 16 | +- `arr[2] = [3, 6, 9]` |
| 17 | + |
| 18 | +We need to merge them into one sorted array: `[1, 2, 3, 4, 5, 6, 7, 8, 9]`. |
| 19 | + |
| 20 | +### Approach: |
| 21 | + |
| 22 | +To merge these arrays, we can use a **min-heap** (or priority queue) to efficiently find the smallest element among the arrays. The approach is as follows: |
| 23 | + |
| 24 | +1. **Initialize a min-heap** and push the first element of each array into it. Each element in the heap will store: |
| 25 | + - The value of the element |
| 26 | + - The index of the array it came from |
| 27 | + - The index of the element within that array |
| 28 | + |
| 29 | +2. **Extract the smallest element from the min-heap** (top of the heap) and add it to the final merged array. |
| 30 | + |
| 31 | +3. **Push the next element** from the array of the extracted element into the min-heap. |
| 32 | + |
| 33 | +4. **Repeat** this process until the min-heap is empty, at which point we have our fully sorted merged array. |
| 34 | + |
| 35 | +## Problem Solution |
| 36 | +```cpp |
| 37 | + |
| 38 | +class Node{ |
| 39 | + public: |
| 40 | + int data; |
| 41 | + int row; |
| 42 | + int col; |
| 43 | + |
| 44 | + Node(int data, int row, int col){ |
| 45 | + this -> data = data; |
| 46 | + this -> row = row; |
| 47 | + this -> col = col; |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +class compare{ |
| 52 | + public: |
| 53 | + bool operator()(Node* a, Node* b){ |
| 54 | + return a -> data > b -> data; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +class Solution |
| 59 | +{ |
| 60 | + public: |
| 61 | + //Function to merge k sorted arrays. |
| 62 | + vector<int> mergeKArrays(vector<vector<int>> arr, int K) |
| 63 | + { |
| 64 | + priority_queue<Node*, vector<Node*>, compare> pq; |
| 65 | + |
| 66 | + for(int i = 0; i < K; i++){ |
| 67 | + Node* temp = new Node(arr[i][0], i, 0); |
| 68 | + pq.push(temp); |
| 69 | + } |
| 70 | + |
| 71 | + vector<int> answer; |
| 72 | + |
| 73 | + while(pq.size() > 0){ |
| 74 | + Node* temp = pq.top(); |
| 75 | + answer.push_back(temp -> data); |
| 76 | + pq.pop(); |
| 77 | + |
| 78 | + int i = temp -> row; |
| 79 | + int j = temp -> col; |
| 80 | + |
| 81 | + if(j+1 < arr[i].size()){ |
| 82 | + Node* next = new Node(arr[i][j+1], i, j+1); |
| 83 | + pq.push(next); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return answer; |
| 88 | + } |
| 89 | +}; |
| 90 | +``` |
| 91 | +
|
| 92 | +## Problem Solution Explanation |
| 93 | +
|
| 94 | +```cpp |
| 95 | +class Node{ |
| 96 | + public: |
| 97 | + int data; |
| 98 | + int row; |
| 99 | + int col; |
| 100 | + |
| 101 | + Node(int data, int row, int col){ |
| 102 | + this -> data = data; |
| 103 | + this -> row = row; |
| 104 | + this -> col = col; |
| 105 | + } |
| 106 | +}; |
| 107 | +``` |
| 108 | + |
| 109 | +1. **Explanation**: |
| 110 | + - We define a `Node` class to store each element we push into the min-heap. |
| 111 | + - Each `Node` stores: |
| 112 | + - `data`: the value of the element. |
| 113 | + - `row`: the index of the array the element belongs to. |
| 114 | + - `col`: the index of the element within that array. |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +```cpp |
| 119 | +class compare{ |
| 120 | + public: |
| 121 | + bool operator()(Node* a, Node* b){ |
| 122 | + return a -> data > b -> data; |
| 123 | + } |
| 124 | +}; |
| 125 | +``` |
| 126 | + |
| 127 | +2. **Explanation**: |
| 128 | + - We define a comparator class `compare` for the min-heap. |
| 129 | + - The comparator function is used by the heap to prioritize nodes based on their `data` value. |
| 130 | + - This comparator makes the min-heap place the smallest element at the top. |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +```cpp |
| 135 | +class Solution |
| 136 | +{ |
| 137 | + public: |
| 138 | + //Function to merge k sorted arrays. |
| 139 | + vector<int> mergeKArrays(vector<vector<int>> arr, int K) |
| 140 | + { |
| 141 | +``` |
| 142 | +
|
| 143 | +3. **Explanation**: |
| 144 | + - The `mergeKArrays` function is part of the `Solution` class. It takes: |
| 145 | + - `arr`: a 2D vector where each row is a sorted array. |
| 146 | + - `K`: the number of sorted arrays. |
| 147 | + - It returns a sorted, merged array. |
| 148 | +
|
| 149 | +
|
| 150 | +
|
| 151 | +```cpp |
| 152 | + priority_queue<Node*, vector<Node*>, compare> pq; |
| 153 | +``` |
| 154 | + |
| 155 | +4. **Explanation**: |
| 156 | + - We declare a min-heap `pq` using the `compare` class as the comparator. |
| 157 | + - This min-heap will help us find the smallest element among the arrays efficiently. |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | +```cpp |
| 162 | + for(int i = 0; i < K; i++){ |
| 163 | + Node* temp = new Node(arr[i][0], i, 0); |
| 164 | + pq.push(temp); |
| 165 | + } |
| 166 | +``` |
| 167 | +
|
| 168 | +5. **Explanation**: |
| 169 | + - We iterate through each of the `K` arrays and push the first element of each array into the min-heap `pq`. |
| 170 | + - We create a `Node` for each element, storing its value, the array index (`row`), and its index in the array (`col`). |
| 171 | + - This initializes the heap with the first element of each array. |
| 172 | +
|
| 173 | +
|
| 174 | +
|
| 175 | +```cpp |
| 176 | + vector<int> answer; |
| 177 | +``` |
| 178 | + |
| 179 | +6. **Explanation**: |
| 180 | + - We declare a `vector<int> answer` to store the merged sorted array. |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +```cpp |
| 185 | + while(pq.size() > 0){ |
| 186 | +``` |
| 187 | +
|
| 188 | +7. **Explanation**: |
| 189 | + - We continue processing while there are elements in the min-heap. |
| 190 | +
|
| 191 | +
|
| 192 | +
|
| 193 | +```cpp |
| 194 | + Node* temp = pq.top(); |
| 195 | + answer.push_back(temp -> data); |
| 196 | + pq.pop(); |
| 197 | +``` |
| 198 | + |
| 199 | +8. **Explanation**: |
| 200 | + - We get the smallest element (`temp`) from the heap (`pq.top()`) and add its `data` to `answer`. |
| 201 | + - Then, we remove it from the heap using `pq.pop()`. |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | +```cpp |
| 206 | + int i = temp -> row; |
| 207 | + int j = temp -> col; |
| 208 | +``` |
| 209 | + |
| 210 | +9. **Explanation**: |
| 211 | + - We retrieve the `row` (`i`) and `col` (`j`) of the array that `temp` belongs to. |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | +```cpp |
| 216 | + if(j+1 < arr[i].size()){ |
| 217 | + Node* next = new Node(arr[i][j+1], i, j+1); |
| 218 | + pq.push(next); |
| 219 | + } |
| 220 | +``` |
| 221 | +
|
| 222 | +10. **Explanation**: |
| 223 | + - If there is a next element in the current array (`j+1 < arr[i].size()`), we push it into the heap. |
| 224 | + - We create a new `Node` with this next element, storing its value, the row (`i`), and the new column (`j+1`). |
| 225 | +
|
| 226 | +
|
| 227 | +
|
| 228 | +```cpp |
| 229 | + return answer; |
| 230 | + } |
| 231 | +}; |
| 232 | +``` |
| 233 | + |
| 234 | +11. **Explanation**: |
| 235 | + - Finally, we return `answer`, which contains the merged sorted array. |
| 236 | + |
| 237 | + |
| 238 | + |
| 239 | +### Step 3: Example Walkthrough |
| 240 | + |
| 241 | +**Example**: |
| 242 | +Input: |
| 243 | +```cpp |
| 244 | +arr = [ |
| 245 | + [1, 4, 7], |
| 246 | + [2, 5, 8], |
| 247 | + [3, 6, 9] |
| 248 | +] |
| 249 | +K = 3 |
| 250 | +``` |
| 251 | + |
| 252 | +Process: |
| 253 | +1. Initialize the min-heap with the first element of each array: `[1, 2, 3]`. |
| 254 | +2. Extract `1` (smallest), add it to `answer`, and push `4` from the first array. |
| 255 | +3. Heap becomes `[2, 3, 4]`. Extract `2`, add to `answer`, push `5`. |
| 256 | +4. Continue this process, and `answer` will eventually be `[1, 2, 3, 4, 5, 6, 7, 8, 9]`. |
| 257 | + |
| 258 | +Output: `[1, 2, 3, 4, 5, 6, 7, 8, 9]` |
| 259 | + |
| 260 | +### Step 4: Time and Space Complexity |
| 261 | + |
| 262 | +1. **Time Complexity**: |
| 263 | + - We push and pop each element from the min-heap once, and there are `N * K` elements (where `N` is the average length of each array). |
| 264 | + - Each push/pop operation in the min-heap takes `O(log K)` time. |
| 265 | + - Overall, the time complexity is `O(N * K * log K)`. |
| 266 | + |
| 267 | +2. **Space Complexity**: |
| 268 | + - The space complexity is `O(K)` for the heap, as we store up to `K` elements at any time. |
| 269 | + |
| 270 | +### Step 5: Additional Recommendations |
| 271 | + |
| 272 | +For students: |
| 273 | +- Understand how a min-heap works, as it is crucial for handling sorted data efficiently. |
| 274 | +- Practice similar merging problems, like merging two sorted lists, before tackling `K` sorted arrays. |
| 275 | +- Trace through the code with different inputs to become comfortable with the flow and logic. |
| 276 | + |
| 277 | +This explanation should give you a clear understanding of how the solution works, along with step-by-step guidance on each line and example-driven insights. |
0 commit comments