New update is available. Click here to update.

Last Updated: 14 Apr, 2021

Difficulty: Moderate

```
‘ORDER_ARR[i] = [RATE[i], AMOUNT[i], TYPE[i]]’ denotes that ‘AMOUNT’ orders have been placed of type ‘TYPE’ at the rate ‘RATE’. The ‘TYPE’ is:
0 represents it as a batch of buy orders, or
1 represents it as a batch of sell orders.
```

```
If the order is a buy order, you look at the sell order with the smallest price in the ‘KHATA’.
If that sell order's price is smaller than or equal to the current buy order's price, they will match and be executed, and that sell order will be removed from the ‘KHATA’.
Else, the buy order is added to the KHATA’.
If the order is a sell order, you look at the buy order with the largest price in the ‘KHATA’.
If that buy order's price is larger than or equal to the current sell order's price, they will match and be executed, and that buy order will be removed from the ‘KHATA’.
Else, the sell order is added to the ‘KHATA’.
```

```
Suppose the given ‘ORDER_ARR’ is [ [ 10, 5, 0 ], [ 15, 2, 1 ], [ 25, 1, 1 ], [ 30, 4, 0 ] ]
Now 5 orders of type buy with price 10 are placed. There are no sell orders, so the 5 orders are added to the ‘KHATA’.
2 orders of type sell with price 15 are placed. There are no buy orders with prices larger than or equal to 15, so the 2 orders are added to the ‘KHATA’.
1 order of type sell with price 25 is placed. There are no buy orders with prices larger than or equal to 25 in the ‘KHATA’, so this order is added to the ‘KHATA’.
4 orders of type buy with price 30 are placed. The first 2 orders are matched with the 2 sell orders of the least price, which is 15 and these 2 sell orders are removed from the ‘KHATA’. The 3rd order is matched with the sell order of the least price, which is 25 and this sell order is removed from the ‘KHATA’. Then, there are no more sell orders in the ‘KHATA’, so the 4th order is added to the ‘KHATA’.
Finally, the ‘KHATA’ has 5 buy orders with a price of 10, and 1 buy order with a price of 30. So the total number of orders in the ‘khata’ is 6.
```

```
1. The answer can be large, so return (ans % 10^9+7)
```

```
The first line of input contains a ‘T’ number of test cases.
The first line of each test case contains an integer ‘N’ denoting the number of rows in array ‘ORDER_ARR’. Then, ‘N’ lines follow.
Each line contains three integers denoting the number of columns in that row and ‘3’ space-separated integers denoting the ‘RATE’, ‘AMOUNT’, and ‘TYPE’ value respectively.
```

```
For each test case, print the total amounts of order in the ‘KHATA’.
```

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

```
1 <= ‘T’ <= 10
1 <= ‘N’ <= 1000
1 <= ‘RATE’, ‘AMOUNT’ <=10 ^ 6
‘TYPE’ = [ ‘0’, ‘1’ ]
Where ‘T’ represents the number of test cases and ‘N’ represents the size of ‘ORDER_ARR’.
Time Limit: 1 sec.
```

Approach: The idea here is to traverse the ‘ORDER_ARR’ and if an order is of type ‘BUY’ we must check our ‘KHATA’ for sell order and find the minimum value order from that using ‘PRIORITY QUEUE’ and compare it. If the order is of type ‘sell’ we have to check our ‘KHATA’ for buy order and find the maximum value using min heap order from that and then compare it. In this way, we traverse our ‘ORDER_ARR’.

Algorithm:

- Firstly we declare two heaps: ‘PRIORITY_QUEUE’ for giving smaller element and one min heap for giving maximum element, one for maintaining buy order ‘KHATA’ and one for maintaining sell order ‘KHATA’ respectively because according to question whenever we encounter sell we want minimum element from buying so ‘PRIORITY_QUEUE’ gives us small element in O(log(n)) time and similarly we need larger element whenever we encounter buy order so min-heap gives us large element in O(log(n)) time
- Now we start iterating ‘ORDER_ARR’ and check
- If the order found is to buy type order we check
- If there is an entry in sell type ‘KHATA’ we find the minimum value order from that and now we check
- If the amount of sell type is greater than the current order amount we subtract the order amount from that sell type and again check if still remains we add into selling ‘KHATA’.
- Else we subtract the amount of selling type from the order and check if still remains we add into buying ‘KHATA’.

- If there is an entry in sell type ‘KHATA’ we find the minimum value order from that and now we check
- If the order found is sell type we check
- If there is an entry in buy type ‘KHATA’ we find the minimum value order from that and now we check
- If the amount of buy type is greater than the current order amount we subtract the order amount from that buy type and again check if still remains we add into buying ‘KHATA’.
- Else we subtract the amount of buying type from the order and check if still remains we add into selling ‘KHATA’.

- If there is an entry in buy type ‘KHATA’ we find the minimum value order from that and now we check

- If the order found is to buy type order we check
- Now after this we count the total no of entries in sell type ‘KHATA’ and buy type ‘KHATA’ and return the ‘ans’ as the total count of them.

SIMILAR PROBLEMS

Split String

Posted: 14 Sep, 2022

Difficulty: Easy

Ninja And The Class Room

Posted: 19 Sep, 2022

Difficulty: Easy

Equal Arrays

Posted: 29 Sep, 2022

Difficulty: Moderate

Ninja And The Strictly Increasing Array

Posted: 27 Nov, 2022

Difficulty: Moderate

Maximize

Posted: 9 Dec, 2022

Difficulty: Easy

Popular Interview Problems: