
1. Ninja starts eating Pizzas on day 0.
2.Ninja must eat at least one Pizza per day as he loves Pizza until he has eaten all the Pizzas.
3.If Ninja has eaten all the pizzas of 'i' - 1 type, then only he can eat a pizza of the ‘i’ type.
If there are two challenges [0, 1, 1] and [3, 3, 2]. For challenge 1, Ninja has to eat at least one pizza of type 0 on day 1 while eating at most 1 pizza per day. Similarly, for challenge 2, Ninja has to eat at least one pizza of type 3 on day 3 while eating at most 2 pizzas per day.
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line of each test case contains two space-separated integers ‘N’ and ‘M’, which denotes the number of elements in the array ‘arr’ and the number of challenges Emma gave to Ninja.
The second line of each test case contains 'N' space-separated integers denoting the elements of the array ‘arr’.
The next ‘M’ lines contain 3 space-separated integers ‘currentType’, ‘currentDay’, ‘maxConsumption’ denoting that Ninja has to eat the pizza of type currentType on day currentDay without eating more than maxConsumption number of pizzas on any day respectively.
For each test case, print the answer for each challenge separated by spaces(1 denoting Ninja can complete the challenge and 0 if he can not complete the challenge).
Print the output of each test case in a separate line.
You are not required to print the expected output, it has already been taken care of. Just implement the function and return the answer for each challenge in an array.
1<= T <= 100
1 <= N <= 10^4
1 <= arr[i] <= 10^5
1 <= mat.length = 10^4
mat[i].length = 3
0 <= type < arr.length
0 <= day < 10^5
1 <= maxConsumption < 10^5
Where 'arr[i]' denotes the element at index ‘i’ of the array 'arr', type’, ‘day’, ‘maxConsumption’ denotes that Ninja has to eat the pizza of type ‘type’ on day ‘day’ without eating more than ‘maxConsumption’ number of pizzas on any day respectively.
Time Limit: 1 sec
For a given challenge, we have three integers currentType, currentDay, maxConsumption, which denotes that Ninja has to eat the pizza of type currentType on day currentDay without eating more than maxConsumption number of pizzas on any day respectively.
Here are two conditions:
Now, we will count the total number of pizzas from type 0 to type currentType - 1 and store it in a variable totalPizzaBeforeDay. So, from point 1, we get that that the number of days given in the challenge should be greater than or equal to the sum of all the pizzas from type 0 to type currentType - 1 divided by maxConsumption, i.e.,
From point 2, we get that at least one pizza should be remaining, i.e., the sum of pizzas from type 0 to type currentType - 1 should be greater than or equal to the number of days given in the challenge, i.e., totalPizzaAtDay - 1 >= currentDay.
Algorithm
From the above approach, we are taking at most O(N) time in each challenge.
Can we improve it?
We will make a prefix array in which index i will store the sum of all the pizzas from type 0 to type currentType. For any challenge, we won’t have to iterate and find the sum of pizzas from type 0 to type currentType, it is already present in the array prefixSum.
The rest of the steps are the same as the previous approach.
Algorithm
Pair Product Div by K
Pair Product Div by K
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
First Digit One
Special Digit Numbers