0
$\begingroup$

I have two arrays.

seconds = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, ..., 2680]

totalMeters =  [0, 3.2, 7.1, 10.6, 14.1, 17.9, 21.5, 24.2, 27.8, 32.5, 36.9, ..., 5000]

The totalMeters array is the total meters that I have run at this specific point of time. The sampling is 2 seconds.

I would like to find where in the array is the fastest 100 meters I have run, as well as its time.

$\endgroup$
  • $\begingroup$ What did you try? Where did you get stuck? We're happy to help you understand the concepts but just solving homework-style exercises for you is unlikely to really do that, and it's hard to pitch an answer at the right level without knowing what you've done. $\endgroup$ – David Richerby Sep 7 '18 at 12:19
0
$\begingroup$

You cannot generally get time for exactly $100$ meters with this data. The better approach would be to search for the longest run within some multiple of $2$ seconds.

Find the longest distance run in $2\cdot k$ seconds:

  • Set desired k.
  • Set i = 0 and j = k-1.
  • Set currentSum = maxSum = totalMeters[j].
  • While j < totalMeters.length - 1 do
    • i++; j++;
    • currentSum = totalMeters[j] - totalMeters[i]
    • if currentSum > maxSum then maxSum = currentSum

This should also give you an idea how to build other similar algorithms for this problem, if you don't change your approach.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.