Minimum Perimeter of n blocks | CPP program 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minPerimeter ( int n ) { int l = sqrt ( n ) ; int sq = l * l ; if ( sq == n ) return l * 4 ; else { long long int row = n / l ; long long int perimeter = 2 * ( l + row ) ; if ( n % l != 0 ) perimeter += 2 ; return perimeter ; } } int main ( ) { int n = 10 ; cout << minPerimeter ( n ) ; return 0 ; }