Minimum Perimeter of n blocks | JAVA Code to find minimum perimeter using n blocks ; if n is a perfect square ; Number of rows ; perimeter of the rectangular grid ; if there are blocks left ; Driver code
import java . util . * ; class GFG { public static long minPerimeter ( int n ) { int l = ( int ) Math . sqrt ( n ) ; int sq = l * l ; if ( sq == n ) return l * 4 ; else { long row = n / l ; long perimeter = 2 * ( l + row ) ; if ( n % l != 0 ) perimeter += 2 ; return perimeter ; } } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( minPerimeter ( n ) ) ; } }