# Minimum moves taken to move coin of each cell to any one cell of Matrix

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.

**Examples:**

Input:N = 3Output:8Explanation:

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 = 5Output:40

**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 * K**^{2}

Below is the implementation of the above approach:

## C++

`// C++ program to find the minimum number` `// of moves taken to move the element of` `// each cell to any one cell of the` `// square matrix of odd length` `#include <iostream>` `using` `namespace` `std;` `// Function to find the minimum number` `// of moves taken to move the element ` `// of each cell to any one cell of the` `// square matrix of odd length` `int` `calculateMoves(` `int` `n)` `{` ` ` ` ` `// Initializing count to 0` ` ` `int` `count = 0;` ` ` `// Number of layers that are` ` ` `// around the centre element` ` ` `int` `layers = n / 2;` ` ` `// Iterating over ranger of layers` ` ` `for` `(` `int` `k = 1; k < layers + 1; k++)` ` ` `{` ` ` `// Increase the value of count ` ` ` `// by 8 * k * k` ` ` `count += 8 * k * k;` ` ` `}` ` ` `return` `count;` `}` `// Driver code` `int` `main()` `{` ` ` `int` `N = 5;` ` ` ` ` `cout << calculateMoves(N);` `}` `// This code is contributed by coder001` |

## Java

`// Java program to find the minimum number` `// of moves taken to move the element of` `// each cell to any one cell of the` `// square matrix of odd length` `class` `GFG{` ` ` `// Function to find the minimum number` `// of moves taken to move the element` `// of each cell to any one cell of the` `// square matrix of odd length` `public` `static` `int` `calculateMoves(` `int` `n)` `{` ` ` ` ` `// Initializing count to 0` ` ` `int` `count = ` `0` `;` ` ` ` ` `// Number of layers that are` ` ` `// around the centre element` ` ` `int` `layers = n / ` `2` `;` ` ` ` ` `// Iterating over ranger of layers` ` ` `for` `(` `int` `k = ` `1` `; k < layers + ` `1` `; k++)` ` ` `{` ` ` ` ` `// Increase the value of count` ` ` `// by 8 * k * k` ` ` `count += ` `8` `* k * k;` ` ` `}` ` ` `return` `count;` `}` `// Driver code ` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `5` `;` ` ` ` ` `System.out.println(calculateMoves(N));` `}` `}` `// This code is contributed by divyeshrabadiya07 ` |

## Python3

`# Python3 program to find the minimum number` `# of moves taken to move the element of` `# each cell to any one cell of the` `# square matrix of odd length` `# Function to find the minimum number` `# of moves taken to move the element of` `# each cell to any one cell of the` `# square matrix of odd length` `def` `calculateMoves(n):` ` ` `# Initializing count to 0` ` ` `count ` `=` `0` ` ` `# Number of layers that are` ` ` `# around the centre element` ` ` `layers ` `=` `n` `/` `/` `2` ` ` `# Iterating over ranger of layers` ` ` `for` `k ` `in` `range` `(` `1` `, layers ` `+` `1` `):` ` ` `# Increase the value of count by` ` ` `# 8 * k * k` ` ` `count` `+` `=` `8` `*` `k` `*` `k` ` ` `return` `count` `# Driver code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `N ` `=` `5` ` ` ` ` `print` `(calculateMoves(N))` |

## C#

`// C# program to find the minimum number` `// of moves taken to move the element of` `// each cell to any one cell of the` `// square matrix of odd length` `using` `System;` `class` `GFG{` ` ` `// Function to find the minimum number` `// of moves taken to move the element` `// of each cell to any one cell of the` `// square matrix of odd length` `public` `static` `int` `calculateMoves(` `int` `n)` `{` ` ` ` ` `// Initializing count to 0` ` ` `int` `count = 0;` ` ` ` ` `// Number of layers that are` ` ` `// around the centre element` ` ` `int` `layers = n / 2;` ` ` ` ` `// Iterating over ranger of layers` ` ` `for` `(` `int` `k = 1; k < layers + 1; k++)` ` ` `{` ` ` ` ` `// Increase the value of count` ` ` `// by 8 * k * k` ` ` `count += 8 * k * k;` ` ` `}` ` ` `return` `count;` `}` `// Driver code` `public` `static` `void` `Main()` `{` ` ` `int` `N = 5;` ` ` ` ` `Console.Write(calculateMoves(N));` `}` `}` `// This code is contributed by Code_Mech` |

## Javascript

`<script>` `// Javascript program to find the minimum number` `// of moves taken to move the element of` `// each cell to any one cell of the` `// square matrix of odd length` `// Function to find the minimum number` `// of moves taken to move the element` `// of each cell to any one cell of the` `// square matrix of odd length` `function` `calculateMoves(n)` `{` ` ` ` ` `// Initializing count to 0` ` ` `var` `count = 0;` ` ` `// Number of layers that are` ` ` `// around the centre element` ` ` `var` `layers = parseInt(n / 2);` ` ` `// Iterating over ranger of layers` ` ` `for` `(` `var` `k = 1; k < layers + 1; k++)` ` ` `{` ` ` ` ` `// Increase the value of count` ` ` `// by 8 * k * k` ` ` `count += 8 * k * k;` ` ` `}` ` ` `return` `count;` `}` `// Driver code` `var` `N = 5;` `document.write(calculateMoves(N));` `// This code is contributed by noob2000` `</script>` |

**Output:**

40

Attention reader! Don’t stop learning now. Participate in the **Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students**.