Iron, Magnet and Wall Codechef November Long challenge 2020 solution with code explanation -
Iron, Magnet and Wall Codechef November Long challenge 2020 solution with code explanation
Let's read problem statement,
2 4 5 I::M 9 10 MIM_XII:M
To solve this problem you need to think some logic that .
1-none of iron and magnet can be paired if any wall is situated between them.
2- A iron/magnet can pair only with one magnet/iron.
3- to maximize the final answer you need need to pair a iron with a magnet which is farthest from iron. similarly you have to for magnet.
Actually implementation part is a little bit more tricky/ tough then logic.
store index of iron, magnet, and wall.
and store count of the metal sheet between 0 to the current index.
and start pairing iron with the farthest magnet and magnet with the farthest iron.
If the wall encounters a clear index of iron magnet and wall.
And repeat the same process after the wall.
Note- This question can be done by another approach.
Follow this video for another approach.
Must Join Telegram channel for Editorial -