From c2a8b26b210b47a472f5f2f2722e6d1156c5d6f2 Mon Sep 17 00:00:00 2001 From: Yuvraj Singh <131570406+yuvrajsingh08@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:36:57 +0530 Subject: [PATCH] Create ksortedarray.cpp Leetcode Problem no. 23 difficulty hard --- C++/ksortedarray.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 C++/ksortedarray.cpp diff --git a/C++/ksortedarray.cpp b/C++/ksortedarray.cpp new file mode 100644 index 000000000..e13306d5a --- /dev/null +++ b/C++/ksortedarray.cpp @@ -0,0 +1,78 @@ +#include +#include +using namespace std; + +class Info{ + public: + int data; + int rowIndex; + int colIndex; + + Info(int a, int b, int c) { + this->data = a; + this->rowIndex = b; + this->colIndex = c; + } + ~Info(){ + cout<data<<" "<rowIndex<<" "<colIndex<<" is deleted"<data > b->data; + } +}; + +void mergeKSortedArrays(int arr[][4], int n, int k , vector &ans) { + + priority_queue, compare> pq; + + //1 step : process firsk k elements + for(int row =0; rowdata; + int topRow = temp->rowIndex; + int topCol = temp->colIndex; + + //store in ans vector + ans.push_back(topData); + + //next element for the same row, jisme se pop kia h just abhi + //use insert bhi toh karna hai + if(topCol + 1 < n) { + //iska matlab row me or element abhi present hai + Info* newInfo = new Info(arr[topRow][topCol+1], topRow, topCol+1); + pq.push(newInfo); + + } + delete temp; + } +} + +int main() { + int arr[3][4] = { + {1,4,8,11}, + {2,3,6,10}, + {5,7,12,14} + }; + int n = 4; + int k = 3; + + vector ans; + mergeKSortedArrays(arr, n, k , ans); + + cout << "printing Ans array: " << endl; + for(int i=0; i