- Practice
- Mathematics
- Fundamentals
- Filling Jars
- Discussions

# Filling Jars

# Filling Jars

foulglory + 9 comments If you're getting timeout:

The whole thing can actually be solved in one loop. Initialize sumtotal to 0. Run the loop m times/while input exists whatever you choose, take input one by one in list/array whatever you use. Input is of the form:

a,b,candy

do sumtotal = sumtotal + (b-a+1)x(candy) for each input

average = sumtotal/n

dejava + 1 comment Correct! No array of jars needed. No looping of filling needed. Test cases have huge numbers.

trendsetter37 + 0 comments Yea came here to say this as well.

RSTHW + 2 comments I did that, but it just works in tests 1-4 :( Im using java. I dont know what is wrong.

ellecdwt + 2 comments Try using BigInteger.

RSTHW + 3 comments I already did it, my problem was that I was using nextInt instead of nextLong. lol

vamsigp + 0 comments dont use nextInt, instead read them as string and use BigInteger

edumor + 1 comment You don't need to use BigInteger, the maximum value for this challenge according to the constraints is 1,00E+18 while a long can hold a number up to 9,22E+18, more than nine times of what you need.

Note that an int will only give the correct answer for sums that don't go over 2^{31}-1.spacebutterfly + 0 comments I used long and double and it worked.

DevikaShanbhag + 0 comments According to the input constraints, input can be an integer so nextInt() should be fine.

Just typecast appropriately to (long) where an operation might result in an overflow.

eg: (long)(b - a + 1) * k

vigneshmv + 0 comments You can use long or int64 , it will work fine and you wont get any wrong answer message

mayurnagdev123 + 0 comments In the problem it is specified that a and b will be in the 10^7 range .However ,the test cases do not follow this and have enormous values that exceed this range.That's why using 'int' instead of 'long' fails most of the test cases.The problem needs to be updated.

codechef23 + 0 comments [deleted]nanthak + 2 comments I don't understand ,please explain in detail

abhiranjan + 0 comments Hi, please refer to editorial to have an alternative explanation.

Mridul20rawat + 0 comments Look dude the only thing required is knowing how many jars are to be filled and multiplying them with capacity given. And then keep on adding the result for as many operations is given. At last find the average.

Hope it helps.

while(o>0)

{ scanf("%ld%ld%ld",&st,&en,&cap);

`res = res + ((en-st)+1)*cap; o--; } fin = floor(res/n);`

jaykishan + 0 comments [deleted]john_canessa + 0 comments Good suggestion. Tried the array. Timeouts. Removed array and switched to BigInteger. Thanks.

runcy + 0 comments Brilliant!

arnav_kumar903 + 0 comments Thanks dude,you're awesome. Was struggling from past 3days.

#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main(){ unsigned long long int n,m; cin>>n>>m; unsigned long long int jar[n]; unsigned long long int sum=0; while(m--){ unsigned long long int a,b,k; cin>>a>>b>>k; sum+=(b-a+1)*k; } unsigned long long int avg=sum/n; cout<<avg<<endl; return 0; }

sarathy_v_krish1 + 0 comments C++ solution :

long long solve(int n, vector<vector<long>> operations) { long long sum=0; for (int i=0;i<operations.size();i++) sum+=operations[i][2]*(operations[i][1]-operations[i][0]+1); sum/=n; return sum; }

Hamada14 + 5 comments Someone Please Help :( All Test Cases are working except for #4 #5 #6

j_singh_logan + 1 comment me too, did you use a moving average formula?

aayush_break + 0 comments [deleted]

asifm37 + 0 comments use Long int.

priyatham01 + 0 comments use long() for average

X816626 + 0 comments Remove the default code (in case of C++). If you use the default code given, eventhough you use long integers, it won't work. The reason could be that the file size exceeds 50kb

robertdyke + 0 comments Make sure that you change the return value from you function from int to long long. That was my problem.

victor_cooke + 1 comment My oneliner Python:

def solve(n, operations): return int(sum([(o[1]-o[0]+1)*o[2] for o in operations])/n)

HaltingProblem + 0 comments Your comment is not only short, but it let me figure out how the input was being passed. I was trying to do the same but was getting EOF errors. Thanks!

Masters_Abh + 0 comments Sweet!

All the basic maths knowledge helps here!

No array means No timeouts.

Using long integers for avoiding overflows.

Reference: C++ Code

neverloseks + 0 comments don't need to use array ... it's just consuming memory...

Sort 149 Discussions, By:

Please Login in order to post a comment