Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Ravi and Coins
MEDIUM
30 mins
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Ravi and Coins

Contributed by
Jaypal Mudaliyar
Medium
yellow-spark
0/80
Avg time to solve 30 mins
Success Rate 60 %
Share
0 upvotes

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, Constraints, Images )
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
Reset Code
Full screen
Auto
copy-code
Console