Ravi and Coins

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
0 upvote
Asked in company
Amazon

Problem statement

Ravi is fond of collecting coins. He has ‘N’ boxes with him numbered from ‘0’ to ‘N-1’, initially, all the boxes are empty.

Now for the next ‘M’ days, Ravi adds one coin to each box from ‘[L, R]’, it is denoted by the 2d-array ‘ARR’. (The range ‘L’ to ‘R’ may or may not be the same for each of the ‘M’ days).

Now Deep was very good at maths and being a friend of Ravi he challenged him to answer ‘Q’ questions, denoted by the array ‘QUERIES’ after the ‘M’ days have passed.

Each question will have a number ‘X’ and Ravi has to find the number of boxes having at least ‘X’ coins in it. (i.e. number of boxes having more than or equal to ‘X’ coins).

Now Ravi has found the answers to the questions but is not sure if they are completely correct, So he has asked you to calculate the answers to these questions. Can you help him?.

EXAMPLE :
Input: ‘N’ = 3, ‘M' = 2, ‘Q’ = 1, ‘ARR’ = [[0, 1], [0, 2]], ‘QUERIES’ = [2]

Output: 2
In this case,
After Day 1 number of coins in the boxes will be: [1, 1, 0]
After Day 2 number of coins in the boxes will be: [2, 2, 1]
Hence the boxes having at least ‘2’ coins are Box 0 and Box 1.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line will contain the integer ‘T’, the number of test cases.

Each test case consists of two lines.

The first line of input contains three integers, ‘N’, ‘M’, and ‘Q’ separated by spaces.

Followed by ‘M’ lines, each line contains two integers, ‘L’ and ‘R’.

Followed by ‘Q’ lines, each line contains one integer, ‘X’.
Output format :
For each test case, print ‘Q’ space separated integers denoting the answers to the ‘Q’ questions asked by Deep.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^5
1 <= ‘M’ <= 10^5
1 <= ‘Q’ <= 10^5
0 <= ‘L’ <= ‘R’ <= ‘N-1’
0 <= ‘X’ <= 10^5
It is guaranteed that sum of ‘N’ over all test cases is <= 10^5
It is guaranteed that sum of ‘M’ over all test cases is <= 10^5
It is guaranteed that sum of ‘Q’ over all test cases is <= 10^5
Time Limit: 1 sec
Sample Input 1 :
2
5 1 1
1 4
1
7 3 2
0 3
2 6
4 6
0
2
Sample Output 1 :
4
7 5
Explanation Of Sample Input 1 :
For the first test case, 
After Day 1 number of coins in the boxes will be: [0, 1, 1, 1, 1]
Hence the boxes having at least ‘1’ coin are Box 1, 2, 3, and 4.

Hence, the output will be: 4

For the second test case,
After Day 1 number of coins in the boxes will be: [1, 1, 1, 1, 0, 0, 0]
After Day 2 number of coins in the boxes will be: [1, 1, 2, 2, 1, 1, 1]
After Day 3 number of coins in the boxes will be: [1, 1, 2, 2, 2, 2, 2]
All the boxes have at least a ‘1’ coin, hence the answer to the ‘1’st question is ‘7’.
The boxes having at least ‘2’ coins are Box 2, 3, 4, 5, and 6.

Hence, the output will be: 7 5
Sample Input 2 :
2
10 3 3
5 9
3 7
2 6
3
0
0
8 4 5
0 0
5 5
4 4
7 7
2
0
4
3
0
Sample Output 2 :
2 10 10 
0 8 0 0 8
Hint

Add the coins naively and answer the questions by finding the boxes having greater than or equal to ‘X’ coins.

Approaches (2)
Brute Force

The idea for this approach is to add the coins for the range ‘L’ to ‘R’ using a loop for every day. And then to answer questions we will iterate through the ‘N’ boxes and keep the count for the boxes having >= ‘X’ coins.

 

To implement this we have to keep an array, let’s call it ‘BOXES’ of length ‘N’ initially filled with ‘0’. And then add ‘1’ to the indices ‘L’ to ‘R’ for every day.

 

Algorithm:

 

// The function will return the answers to the questions denoted by the array ‘QUERY’.

Int[] getAnswers(N, M, Q, ARR, QUERIES)

  • Create an array named ‘BOXES’ of length ‘N’ with the initial value of ‘0’.
  • Run a for loop from ‘0’ to ‘M-1’ with a variable named ‘i’.
    • Run a for loop from ‘ARR[i][0]’ to ‘ARR[i][1]’ with a variable named ‘j’.
      • Increment ‘BOXES[j]’ by ‘1’.
  • Create an array ‘ANS’ of the length ‘Q’.
  • Run a for loop from ‘0’ to ‘Q-1’ with a variable named ‘i’.
    • Initialize the variable named ‘COUNT’ with the value of ‘0’.
    • Run a for loop from ‘0’ to ‘N-1’ with a variable named ‘j’.
      • If ‘BOXES[j] >= QUERY[i]’
        • Increment ‘COUNT’ by ‘1’.
    • Assign ‘ANS[i]’ the value of ‘COUNT’.
  • Return ‘ANS’.
Time Complexity

O( M * N + Q * N ), Where ‘N’, ‘M’, and ‘Q’ are input integers.

In the above algorithm, in the worst case when every day we have to add a coin for all the boxes from ‘0’ to N-1’ i.e. ‘L’ = 1 and ‘R’ = ‘N-1’, there will be in total ‘M*N’ iterations to update the array ‘BOXES’. Also for every question of the array ‘QUERIES’ of length ‘Q’, we iterate through each element of the array ‘BOXES’ of the length ‘N’, which will have total Q*N iterations.

Hence the time complexity will be O( M * N + Q * N )

Space Complexity

O( N + Q ), Where ‘N’ and ‘Q’ are input integers.

 

In the above algorithm, we use an array ‘BOXES’ of length ‘N’ to keep track of the coins added while ‘M’ days and we also use an array ‘ANS’ of length ‘Q’ to return the answers to the questions denoted by the array ‘QUERIES’.

Hence the space complexity will be O( N + Q ).

Code Solution
(100% EXP penalty)
Ravi and Coins
Full screen
Console