# Merge transactions in bank sheets in the order of their occurrence such that their sum remains positive

• Difficulty Level : Hard
• Last Updated : 01 Jul, 2021

Given an array arr[][] consisting of N lists 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 → -500
Explanation: 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:

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 pq`   `            ``= ``new` `PriorityQueue<>(` `                ``new` `Comparator() {`   `                    ``// 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)

My Personal Notes arrow_drop_up
Recommended Articles
Page :