The gravitational force is a force that attracts any two objects with mass. In this challenge our objects will be Numbers and their mass will be their value. To do so, we don't care about the strength of the force but the direction of it.
Imagine this set of numbers
[1 6 9 4 6 9 7 6 4 4 9 8 7]
Each of them creates a force between itself and it's adjacent numbers. Under some conditions, this will cause another number to be attracted (moved) toward a number. When the number is bigger than the adjacent, it attracts it. Lets take a look at our previous example:
[1 → 6 → 9 ← 4 6 → 9 ← 7 ← 6 ← 4 4 → 9 ← 8 ← 7]
The number 1
is not big enough to move 6
, but the number 6
is, etc... Basically, numbers are moved to the biggest adjacent number (also bigger than the number itself). If both of the adjacent numbers are equal it is not attracted then. It also happens when the number and it adjacent are equal.
This is only to show the attraction, but what happens after? Numbers that collide due to attraction are summed up:
[20 32 28]
So basically the challenge is, Given a set of numbers, output the result of the attracted set of numbers.
Example 1
Input => [10 15 20 10 20 10 10]
[10 → 15 → 20 10 20 ← 10 10]
Output => [45 10 30 10]
Example 2
Input => [9 9 9 9 8 1 8]
[9 9 9 9 ← 8 1 8]
Output => [9 9 9 17 1 8]
Example 3
Input => [1 6 9 4 6 9 7 6 4 4 9 8 7]
[1 → 6 → 9 ← 4 6 → 9 ← 7 ← 6 ← 4 4 → 9 ← 8 ← 7]
Output => [20 32 28]
Example 4
Input => [1 2 3 2 1]
[1 → 2 → 3 ← 2 ← 1]
Output => [9]
Example 5
Input => [1]
Output => [1]
Example 6
Input => [1 1]
Output => [1 1]
Example 7
Input => [2 1 4]
Output => [2 5]
Notes
- Attraction only happens once
- Numbers are not attracted to non-adjacent Numbers
- The set of numbers will only contain positive integers
[1 3 5 4 2]
= 15? \$\endgroup\$G*M*m / r^2
, is equal for both bodies. The lighter one moves more than the heavier one because of momentum, not because of a lack of attraction. Maybe say "1 is not big enough to move 6". \$\endgroup\$