- Prepare
- Mathematics
- Fundamentals
- Filling Jars
- Discussions

# Filling Jars

# Filling Jars

+ 0 comments If you are solving this problem in C, The main function code and the function declaration are wrong. they define the return of the "solve" function as "int" and store it in "int" variable. however test cases 4,5 and 6 inputs makes the return of the "solve" function larger than "unsigned int". so if you are trying to solve this problem, fix the return type of the function to be long long so you can pass all the test cases.

+ 0 comments What we need is Total Candy / n. We have n so just focus on getting Total Candy. There is no need to put the candy into jars first, Don't need the array and computing power. If it helps uppack your values and times the number of jars by how much candy is in each. do that for each go around and Total Candy is found.

+ 0 comments Simple java code with out loops

import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in ); long n,t,sum=0; n=sc.nextLong(); t=sc.nextLong(); while(t-->0) { long a,b,k; a=sc.nextLong(); b=sc.nextLong(); k=sc.nextLong(); sum+=(b-a+1)*k; } System.out.println(sum/n); } }

+ 0 comments Java, O(m):

import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(); double sum = 0; for (int i = 0; i < m; i++) { int a = sc.nextInt(), b = sc.nextInt(), k = sc.nextInt(); sum += (double) k * (b - a + 1); } System.out.println((long) Math.floor(sum / n)); } }

+ 0 comments My python solution

def solve(n, operations): return int(sum(list(map(lambda x:(x[1]-x[0]+1)*x[2],operations))) / n)

Another Python Solution

def solve(n, operations): sum=0; for e in operations: l=list(e) sum+=(l[1]-l[0]+1) * l[2] return int(sum/n)

Sort 183 Discussions, By:

Please Login in order to post a comment