Given an odd number N which represents a grid of size N x N which is initially filled by coins, the task is to find the minimum number of moves required for all the coins to move to any cell of the grid such that in each step, some arbitrary coin in the middle of the grid can move to any of the surrounding eight cells.
Input: N = 3
There are a total of 9 cells in a 3 x 3 grid. On assuming that all the coins have to be brought to the central cell, 8 coins should be moved and the number of steps is 8.
Input: N = 5
Approach: The minimum number of steps is obtained only when all the coins are moved to the centre of the grid. Therefore, the idea is to divide the entire grid into multiple layers.
- Each layer of the grid K takes K moves to reach to the centre. That is:
- The coins at layer 1 take one step to move to the centre.
- The coins at layer 2 take two steps to move to the centre and so on.
- For example, let N = 5. Then, the grid is divided into the layers as follows:
- In the above illustration, the coins which are marked in the red is at layer 1 and the coins which are marked blue is at layer 2.
- Similarly, for a grid of size N x N, we can divide the coins into N//2 layers.
- In each layer K, the number of coins present is (8 * K). And, the number of steps required is K. Therefore, iterate through all the layers and find the total number of steps as 8 * K2
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find minimum moves to bring all elements in one cell of a matrix
- Minimum number of flips required such that the last cell of matrix can be reached from any other cell
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Make a fair coin from a biased coin
- Count ways to distribute exactly one coin to each worker
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- Total number of days taken to complete the task if after certain days one person leaves
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Minimum element of each row and each column in a matrix
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Count the total number of squares that can be visited by Bishop in one move
- Speed of boat in still water from speed of stream and times taken
- Permutations of n things taken r at a time with k things together
- Time taken by two persons to meet on a circular track
- Time taken per hour for stoppage of Car
- Permutations of n things taken all at a time with m things never come together
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.