Challenge: Filling Jars
Subdomeniu: Fundamentals (fundamentals)
Scor cont: 20.0 / 20
Submission status: Accepted
Submission score: 1.0
Submission ID: 464720885
Limbaj: python3
Link challenge: https://www.hackerrank.com/challenges/filling-jars/problem
Cerinta
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 $m$ operations?
**Example**
$n = 5$
$operations = [[1, 2, 10], [3, 5, 10]]$
The array has $5$ elements that all start at $0$. In the first operation, add $10$ to the first $2$ elements. Now the array is $[10, 10, 0, 0, 0]$. In the second operation, add $10$ to the last $3$ elements (3 - 5). Now the array is $[10, 10, 10, 10, 10]$ and the average is 10. Sincd 10 is already an integer value, it does not need to be rounded.
**Function Description**
Complete the *solve* function in the editor below.
*solve* has the following parameters:
- *int n:* the number of candy jars
- *int operations[m][3]:* a 2-dimensional array of operations
**Returns**
- *int:* the floor of the average number of canidies in all jars
Input Format
The first line contains two integers, $n$ and $m$, separated by a single space.
$m$ lines follow. Each of them contains three integers, $a$, $b$, and $k$, separated by spaces.
Constraints
$3 \le n \le 10^7$
$1 \le m \le 10^5$
$1 \le a \le b \le N$
$0 \le k \le 10^6$
Cod sursa
#!/bin/python3
import math
import os
import random
import re
import sys
def solve(n, operations):
total_candies=0
for i in operations:
initial_index=i[0]
limit_index=i[1]
jars=(limit_index-initial_index)+1
total_candies+=jars*i[2]
return((total_candies//n))
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
m = int(first_multiple_input[1])
operations = []
for _ in range(m):
operations.append(list(map(int, input().rstrip().split())))
result = solve(n, operations)
fptr.write(str(result) + '\n')
fptr.close()
HackerRank Fundamentals – Filling Jars
