TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

ExpectedCalculation

Problem statement, definition, constraints, and public examples.

Problem Statement

You have written a small piece of code that will help you finish your long homework assignment. Before actually using it on your homework, you want to test your program's speed. You will be given a int[] times containing running times in seconds. You will also get a int[] frequencies. Element i of frequencies tells you how many tests took times[i] seconds each. For example,
times        =  {10,2,5}
frequencies  =  {3,1,4}
means that 3 tests took 10 seconds, 1 test took 2 seconds, and 4 tests took 5 seconds. Return the average number of seconds taken by all of the tests. Here the average would be:
	(5+5+5+5+2+10+10+10)/(3+1+4) = 52/8 = 6.5

Definition

Class:
ExpectedCalculation
Method:
getExpectation
Parameters:
int[], int[]
Returns:
double
Method signature:
double getExpectation(int[] times, int[] frequencies)
(be sure your method is public)

Notes

  • Solutions must be accurate to 1e-9 absolute or relative. In other words, standard double arithmetic provides enough precision.

Constraints

  • times will contain between 2 and 50 elements inclusive.
  • frequencies will contain the same number of elements as times.
  • Each element of times will be between 1 and 1000 inclusive.
  • Each element of frequencies will be between 1 and 1000 inclusive.
  • times will not contain any duplicate values.

Examples

  1. {10,2,5}
    {3,1,4}
    Returns: 6.5
    The example in the problem statement.
  2. {1,2,3,4,5}
    {1,1,1,1,1}
    Returns: 3.0
    5 tests were conducted. The average time is: (1+2+3+4+5)/5 = 3
  3. {15,3,5,7,9,11,13}
    {22,13,44,93,13,2,1}
    Returns: 7.404255319148936
  4. {20,40,60,80,100,120,140,160,180,200,220,240,260,
     280,300,320,340,360,380,400,420,440,460,480,500,
     520,540,560,580,600,620,640,660,680,700,720,740,
     760,780,800,820,840,860,880,900,920,940,960,980,1000}
    {1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,
     1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,
     1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,
     1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,
     1000,1000,1000,1000,1000,1000,1000,1000,1000,1000}
    Returns: 510.0
    Many tests were conducted here.
  5. {1,2}
    {1,1}
    Returns: 1.5
    Only 2 tests here.
Back to all problems