Given an array **arr[]** consisting of **N** integers representing **N** transactions, the task is to merge the given lists of transactions in the order of their occurrences, such that at any point of time, the sum of already performed transactions is non-negative. If found to negative, then print **“-1”**. Otherwise, print the merged list of transactions.

**Examples:**

Input:arr[][] = {{100 → 400 → -1000 → -500}, {-300 → 2000 → -500}}Output:100 → 400 → -300 → 2000 → -500 → -1000 → -500Explanation:The sum at every instant of the above list of transactions is given by {100, 500, 200, 2200, 1700, 700, 200}, which has no negative values.

Input:arr[][] = [[100 → 400]]Output:100 400

**Approach:** The given problem can be visualized as a variation of merge **K**-sorted linked lists with criteria that the sum of the merged list of transactions at any instant should be non-negative.

Follow the steps below to solve the problem:

- Initialize a new node, say
**P**, that denotes the head of the constructed Linked List. - Initialize a priority queue, say
**PQ**, of size**N**to implement a**Max Heap**. - Insert the first
**N**transactions as nodes into**PQ**. - Iterate until
**PQ**is non-empty and perform the following steps:- Pop the top node of the
**priority queue**and insert that node at the end of the list, having head**P**. - If the next node of the popped node exists, then insert the next node of the popped node in
**PQ**.

- Pop the top node of the
- After completing the above steps, print the
**linked list**formed having head**P**.

Below is the implementation of the above approach:-

## Java

`// Java program for the above approach` ` ` `import` `java.util.*;` ` ` `// Structure of a Node` `// in the Linked List` `class` `Node {` ` ` ` ` `int` `val;` ` ` `Node next;` ` ` ` ` `// Constructor` ` ` `Node(` `int` `val)` ` ` `{` ` ` `this` `.val = val;` ` ` `this` `.next = ` `null` `;` ` ` `}` `}` ` ` `class` `GFG {` ` ` ` ` `// Function to merge the Bank sheets` ` ` `public` `static` `void` `mergeSheets(` ` ` `Node lists[])` ` ` `{` ` ` `// Initialize Max_Heap` ` ` `PriorityQueue<Node> pq` ` ` ` ` `= ` `new` `PriorityQueue<>(` ` ` `new` `Comparator<Node>() {` ` ` ` ` `// Comparator Function` ` ` `// to make it maxHeap` ` ` `public` `int` `compare(Node a, Node b)` ` ` `{` ` ` `return` `b.val - a.val;` ` ` `}` ` ` ` ` `});` ` ` ` ` `// Stores the output list` ` ` `Node p, head = ` `new` `Node(` `0` `);` ` ` `p = head;` ` ` ` ` `// Insert the first element` ` ` `// of each list` ` ` `for` `(` `int` `i = ` `0` `;` ` ` `i < lists.length; i++) {` ` ` ` ` `// If the list is not NULL` ` ` `if` `(lists[i] != ` `null` `) {` ` ` ` ` `// Insert element in` ` ` `// the priority queue` ` ` `pq.add(lists[i]);` ` ` `}` ` ` `}` ` ` ` ` `// Iterate until PQ is non-empty` ` ` `while` `(!pq.isEmpty()) {` ` ` `p.next = pq.poll();` ` ` `p = p.next;` ` ` ` ` `if` `(p.next != ` `null` `)` ` ` `pq.add(p.next);` ` ` `}` ` ` ` ` `p = head.next;` ` ` ` ` `// Print the output list` ` ` `while` `(p.next != ` `null` `) {` ` ` `System.out.print(p.val + ` `" "` `);` ` ` `p = p.next;` ` ` `}` ` ` ` ` `System.out.print(p.val);` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `N = ` `2` `;` ` ` ` ` `Node arr[] = ` `new` `Node[N];` ` ` ` ` `arr[` `0` `] = ` `new` `Node(` `100` `);` ` ` `arr[` `0` `].next = ` `new` `Node(` `400` `);` ` ` `arr[` `0` `].next.next = ` `new` `Node(-` `1000` `);` ` ` `arr[` `0` `].next.next.next = ` `new` `Node(-` `500` `);` ` ` ` ` `arr[` `1` `] = ` `new` `Node(-` `300` `);` ` ` `arr[` `1` `].next = ` `new` `Node(` `2000` `);` ` ` `arr[` `1` `].next.next = ` `new` `Node(-` `500` `);` ` ` ` ` `// Function Call` ` ` `mergeSheets(arr);` ` ` `}` `}` |

**Output:**

100 400 -300 2000 -500 -1000 -500

**Time Complexity:** O(N * log K)**Auxiliary Space:** O(K)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.