Official algorithmic problem description and constraints.
π ships set out to explore the depths of the ocean. The ships are numbered from 1
1 to π and follow each other in ascending order; the π-th ship has a durability of ππ.
The Kraken attacked the ships π times in a specific order. First, it attacks the first of the ships, then the last, then the first again, and so on.
Each attack by the Kraken reduces the durability of the ship by 1
1. When the durability of the ship drops to 0
0, it sinks and is no longer subjected to attacks (thus the ship ceases to be the first or last, and the Kraken only attacks the ships that have not yet sunk). If all the ships have sunk, the Kraken has nothing to attack and it swims away.
Detailed video explanations, mathematical intuition, and clean C++ implementation code.