Find Sum Weight
Stacey is coordinating a beach clean-up project with her university's Women in STEM Outreach branch. The beach is covered with tin cans of varying weights arranged in a single line, indexed from 0 to n-1.
Stacey uses a scooper that can pick up three adjacent cans at a time. For each selection:
- She identifies the three adjacent cans with weight
w - She uses the scooper to pick up the three cans with
w - She removes them from the beach
- She continues this process until there are no cans left on the beach
For each selection, all cans have the lightest weight. Stacey selects the one with the smallest index that contains three adjacent cans with the lightest weight.
Determine the sum of the weights of the three cans she picks in each selection.
Complete the function findSumWeight in the editor with the following parameter:
cans: array of the cans on the beach
1Example 1
If there be n=5 cans on the beach with weights represented by cans = [4, 5, 3, 2]:
- First, observe the minimum weight is 2, and 3 are removed. The array of cans is [4, 5]
- Then, observe the minimum weight is 4, and 4 are removed, and there are no more items then.
Hence, the sum is 2 + 3 + 4 = 9.