- 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.

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...

suv_codemode + 2 comments Hi guys, my code is passing the first 3, failing the next 3 and passing the rest of them. Is there anything special about testcase #4,#5,#6? It keeps saying I have wrong answer. Rest of the test case pass though.

anmoluppal + 4 comments I dont think there is any special thing in those test cases, However you can always download the test cases at the cost of 5 hackos , Try finding it by yourself and have a look at the given constraints in the problem statement, It may be helpful... :)

suv_codemode + 0 comments Yes the constraints. :) Thank you.

jamesw129 + 0 comments [deleted]avnish30jn + 0 comments Hi Anmol,

what contraints r u talking about.....can u help ?

leopragi + 2 comments yep me too having same prob......i downloaded the testcase and i run it in custom input.....it shows me input cannot exceed 50 kb

vatsalchanana + 0 comments You can run it on your own machine. You cannot run custom tests for inputs with size > 50KB on the site.

arnav_kumar903 + 0 comments use unsigned long long int in all variables.

jaykishan + 2 comments same problem... did u found solution??

vatsalchanana + 0 comments Please refer to the editorial for the explanation of the solution.

arnav_kumar903 + 0 comments Yes, use unsigned long long int for all variables, it will work.

hackboy21121996 + 0 comments import java.io.

*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*;public class Solution {

`// Complete the solve function below. static BigInteger solve(int n, int[][] operations) { long arr[]=new long[n]; BigInteger temp=new BigInteger("0"); BigInteger sum=new BigInteger("0"); for(int i=0;i<operations.length;i++) { long a=operations[i][0]-1; long b=operations[i][1]-1; long c=operations[i][2]; long num=b-a+1; temp=BigInteger.valueOf(num); temp=temp.multiply(BigInteger.valueOf(c)); sum=sum.add(temp); } /* for(int i=0;i<arr.length;i++) sum+=arr[i];*/ BigInteger avg=sum.divide(BigInteger.valueOf(n)); return avg; } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); String[] nm = scanner.nextLine().split(" "); int n = Integer.parseInt(nm[0]); int m = Integer.parseInt(nm[1]); int[][] operations = new int[m][3]; for (int operationsRowItr = 0; operationsRowItr < m; operationsRowItr++) { String[] operationsRowItems = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int operationsColumnItr = 0; operationsColumnItr < 3; operationsColumnItr++) { int operationsItem = Integer.parseInt(operationsRowItems[operationsColumnItr]); operations[operationsRowItr][operationsColumnItr] = operationsItem; } } BigInteger result = solve(n, operations); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); bufferedWriter.close(); scanner.close(); }`

} //Just changed return type to BigInteger and all test cases passed.

qwrtyuiuytres + 0 comments #include <bits/stdc++.h> using namespace std; int main(){ long n,m,a,b,c; cin>>n>>m; long int count=0; for(int i=0; i<m; i++){ cin>>a>>b>>c; count += (b - a+1) * c; } cout<< count/n; return 0; }

iamlazycoder + 0 comments #include<iostream> using namespace std; int main(){ long n,q,sum=0; cin>>n>>q; for(int i=0;i<q;i++){ long a,b,c; cin>>a>>b>>c; sum+=(b-a+1)*c; } cout<<sum/n; return 0; }

TheCodeHere + 0 comments Here's my code in C++. there's no need to use arrays. I hope you find it useful.

int main() { int n,m; cin >> n >> m; int a,b; long long k,result = 0; while(m--) { cin >> a >> b >> k; result += (b-a+1)*k; } cout << result/n << endl; return 0; }

delamath + 1 comment Crazy 2-liner in Python 3. :D

n, m = map(int, input().split()) print(sum(map(lambda x, y, t: t * (y - x + 1), *zip(*(map(int, input().split()) for _ in range(m))))) // n)

brianmvance + 0 comments N,M = map(int,input().split()) candies = 0 for _ in range(M): start, end, candies_per = map(int,input().split()) candies += (end-start+1)*candies_per print(candies//N)

Sort 135 Discussions, By:

Please Login in order to post a comment