Hackerrank - Filling Jars Solution
1 min read

Hackerrank - Filling Jars Solution

Hackerrank - Filling Jars Solution

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

5 3
1 2 100
2 5 100
3 4 100

Sample Output

160

Explanation

Initially each of the jars contains 0 candies

0 0 0 0 0  

First operation:

100 100 0 0 0  

Second operation:

100 200 100 100 100  

Third operation:

100 200 200 200 100  

Total = 800, Average = 800/5 = 160

Solution in 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)

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×