Minimum moves taken to move coin of each cell to any one cell of Matrix | 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 ; Initializing count to 0 ; Number of layers that are around the centre element ; Iterating over ranger of layers ; Increase the value of count by 8 * k * k ; Driver code
function calculateMoves ( n ) { var count = 0 ; var layers = parseInt ( n / 2 ) ; for ( var k = 1 ; k < layers + 1 ; k ++ ) { count += 8 * k * k ; } return count ; } var N = 5 ; document . write ( calculateMoves ( N ) ) ;