google india

975
Oct 30, 2024

You are given a pyramid of blocks with total_levels levels. The pyramid is structured such that:

The topmost level contains 1 block.
The second level contains 3 blocks.
The third level contains 5 blocks.
and so on ...
For example, a pyramid with 4 levels appears like this:

    ______
   |      |
 ______  ______  ______
|      ||      ||      |

| || || || || |


| || || || || || || |
Initially, distinct integers from 1 to 2 * total_levels - 1 is written into the blocks of the bottom-most level. All higher levels are filled according to the following rule:

The value of each block at any higher level must be the median of the three blocks directly below it (the block directly beneath it, the block to the left, and the block to the right).
determine the number written in the block at the topmost level of the pyramid.

Example:

    ______
   |      |
 ______  ______  ______
|      ||      ||      |

| || || || || |


| 1 || 6 || 3 || 7 || 4 || 5 || 2 |

return value: 4

because:

    ______
   |  4   |
 ______  ______  ______
|  4   ||  5   ||  4   |

| 3 || 6 || 4 || 5 || 4 |


| 1 || 6 || 3 || 7 || 4 || 5 || 2 |

function find_top_block(bottom_level):

examples:
1 2 3
return value: 2

1 2 3 6 5 4 7
return value: 5

7 3 6 2 4 5 1
return value: 2

1 6 2 7 4 5 3
return value: 4

[ 16, 3, 2, 7, 4 ,5, 10 ,8, 9, 11, 12, 13, 1 ]
return value: 8

[ 8, 4, 1, 9, 5, 7, 3, 6, 2 ]
return value: 5

[ 2, 4, 3, 5, 9, 7, 6, 8, 1 ]
return value: 7

Comments (5)