We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
First, let's assume when a number of rows is just 1.
If we have 1 base, we will require 1 drop.
If We have 2 bases.
[][]
We can drop in between them. Hence we will need only 1 drop.
[]-[]
Now if we have 3 base
[]-[]-[]
then we will require 2 drop to cover all the bases.
If we have 4 base
[]-[][]-[]
only 2 drops can cover all the bases.
We can see if the number of the base is n then we need (n+1)/2 in integer numbers of drops.
Since (1+1)/2 = 1, (2+1)/2 = 1, etc.
So, we have the formula for 1 row i.e. (m+1)/2
now if we have 2 rows, then we can shift down the drops to place it in the corner of 4 bases and it will be the same as it was for 1 row
And if we notice as we did for columns, we will find out it follows the same pattern. Hence we just need to multiply the output for coulmn with (n+1)/2
So, result = ((m+1)/2)*((n+1)/2);
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Army Game
You are viewing a single comment's thread. Return to all comments →
[] denotes base and - denotes drop
First, let's assume when a number of rows is just 1.
If we have 1 base, we will require 1 drop.
If We have 2 bases. [][] We can drop in between them. Hence we will need only 1 drop. []-[]
Now if we have 3 base []-[]-[] then we will require 2 drop to cover all the bases.
If we have 4 base []-[][]-[] only 2 drops can cover all the bases.
We can see if the number of the base is n then we need (n+1)/2 in integer numbers of drops. Since (1+1)/2 = 1, (2+1)/2 = 1, etc.
So, we have the formula for 1 row i.e. (m+1)/2
now if we have 2 rows, then we can shift down the drops to place it in the corner of 4 bases and it will be the same as it was for 1 row
And if we notice as we did for columns, we will find out it follows the same pattern. Hence we just need to multiply the output for coulmn with (n+1)/2
So, result = ((m+1)/2)*((n+1)/2);