Skip to content
the poor coder

Hackerrank - Filling Jars Solution

Poor Man
Published
Updated
Reading time
1 min read

Animesh has N empty candy jars, numbered from 1 to N, with infinite capacity. He performs M operations. Each operation is described by 3 integers, a, b, and k. Here, a and b are indices of the jars, and k is the number of candies to be added inside each jar whose index lies between a and b (both inclusive). Can you tell the average number of candies after  operations?

Input Format

The first line contains two integers,  and , separated by a single space.
lines follow; each of them contains three integers, , , and , separated by spaces.

Constraints



Output Format

A single line containing the average number of candies across  jars, rounded down to the nearest integer.

Note: Rounded down means finding the greatest integer which is less than or equal to the given number. E.g. 13.65 and 13.23 are rounded down to 13, while 12.98 is rounded down to 12.

Sample Input

Text
5 3
1 2 100
2 5 100
3 4 100

Sample Output

Text
160

Explanation

Initially each of the jars contains 0 candies

Text
0 0 0 0 0  

First operation:

Text
100 100 0 0 0  

Second operation:

Text
100 200 100 100 100  

Third operation:

Text
100 200 200 200 100  

Total = 800, Average = 800/5 = 160

Solution in Python#

Python
M,N = list(map(int,input().split()))

sum = 0
for _ in range(N):
    a,b,k = list(map(int,input().split()))
    sum += (b-a+1)*k
print(sum//M)

Keep going

2020

Hackerrank - Bus Station Solution

There are groups of friends, and each group is numbered from 1 to . The th group contains people. They live near a bus stop, and only a single bus operates on this route. An empty bus arrives at the bus stop and all the…

2020

Hackerrank - K Candy Store Solution

Jim enters a candy shop which has N different types of candies, each candy is of the same price. Jim has enough money to buy K candies. In how many different ways can he purchase K candies if there are infinite candies…

2020

Hackerrank - Diwali Lights Solution

On the eve of Diwali, Hari is decorating his house with a serial light bulb set. The serial light bulb set has N bulbs placed sequentially on a string which is programmed to change patterns every second. If at least one…

ESC