Thursday 27 June 2019

Army game Hacker rank solution in c

If a supply drop can cover 4 blocks. Therefore if the grid is 2x2, 1 suppy drop is enough. But what if the grid is 3x2?
1 suppy drop will cover 2x2 portion of the grid but still there is 1x2 part and you need a supply drop there anyway. Same as how many you would need in a 4x2 grid.
Similarly if the region was 3x3 you would need as much as drops as you would need in a 4x4 grid. Otherwise some blocks would be left empty.
So for simplicity of calculation we increment the odd integer to it's nearest bigger even (yes, that means odd+1) and then multiply n and m and divide it by 4. That's how many supply drops we must need to cover all the blocks.  This explanation is given by user sojal in hacker rank discussions

https://www.hackerrank.com/challenges/game-with-cells/problem

Program:

 #include<stdio.h>
int main()
{
int n,m;
scanf("%d%d",&n,&m);
if(n%2 != 0)
    n++;
if(m%2 != 0)
    m++;
printf("%d",(m*n)/4);
return 0;
}

No comments:

Post a Comment