public static int[] mergeKSortedArrays(int[][] arr, int n){
        int k = arr.length;
        int[] result = new int[k * n];
        if(k == 0) return result;

        Comparator<Element> comparator = new Comparator<Element>() {
            @Override
            public int compare(Element e1, Element e2) {
                int num1 = e1.arr[e1.index];
                int num2 = e2.arr[e2.index];
                return num1 - num2;
            }
        };
        PriorityQueue<Element> queue = new PriorityQueue<Element>(k, comparator);

        for(int i = 0; i < arr.length; i++){
            if(arr[i].length > 0) queue.add(new Element(arr[i], 0));
        }

        int i = 0;
        while(!queue.isEmpty()){
            Element element = queue.poll();
            result[i++] = element.arr[element.index];
            if(element.index + 1< element.len){
                element.index++;
                queue.add(element);
            }
        }
        return result;
    }