Merge K Sorted Arrays

Please help me debug what is wrong in my code.

#include<iostream>
#include<queue>
#define ll long long
using namespace std;

class node{
public:
	ll data;
	ll x;	// arr index
	ll y;	// ele index
	// constructor
	node(ll d, ll x, ll y){
		data = d;
		this->x = x;
		this->y = y;
	}
};

vector<ll> mergeKSortedArrays(vector<vector<ll>> a){
	vector<ll> result;
	priority_queue<node,vector<node>,greater<node>> pq;

	// insert 0th element of all K-arrays into min-heap/priority queue
	for(ll i=0; i<a.size(); i++){
		pq.push(node(a[i][0], i, 0));
	}

	while(!pq.empty()){
		node current = pq.top();
		pq.pop();

		ll ele_value = current.data;
		ll X = current.x;	// arr index
		ll Y = current.y;	// ele index

		result.push_back(ele_value);

		// push next element(to current element) in pq, which will be at position (X,Y+1)
		if(Y+1 < a[X].size()){
			pq.push(node(a[X][Y+1], X, Y+1));
		}
	}

	return result;
}



int main() {

	ll K,N;
	cin>>K>>N;

	vector<vector<ll>> v;

	for(ll i=0; i<K; i++){
		for(ll j=0; j<N; j++){
			cin>>v[i][j];
		}
	}

	vector<ll> ans = mergeKSortedArrays(v);

	for(auto x:ans){
        cout<<x<<" ";
    }


	return 0;
}

@anuragdeol2017
there are 2 errors in this code

  1. when we declare a vector and we want to take input at its indices then we also have to define its length
    for example vector v and if we use cin>>v[2] it will not run we need to define its legth vetor v(10) cin>>v[9] it will work now same for vector<vector> v
    we use vector<vector> v(n,vector (m))
    it will work fine
  2. declare the < operator into your class function becuase it do not know how to decide which object is smaller and which one is larger it is just like learning the syntax and also type priority queue syntax like in this code
    Coding Blocks IDE
    hope it clears your dout it clear please mark it as resolved and rate my experience

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

thanks for great explanation.
but i still have some doubt.

  1. I did not understand what and why you did from line 18-21. Is that for creating min heap? I have not seen anything like this, till now in the course (I know it is operator overloading, but i did not understand it wrt priority queue), please help me understand that part.

  2. You simply created priority_queue. Will that not lead to formation of max heap by default?

Overall please help me understand how exactly to create priority_queue in such case?

please clear my doubt

@anuragdeol2017 the pq is of type node, while making it the compiler doesnt know what basis to use for comparison, hence overloading the < operator will act as a comparator.
2. by default max heap is created generally, but again to make any heap you’ll need a value on whose basis the sorting should be done. here heap is sorted on the basis of data

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.