Java Program to Find Minimum Perimeter of n Blocks

In the previous article, we have seen Java Program to Check Whether a Given Point Lies Inside a Rectangle or Not

In this article we will discuss about Java Program to Find minimum possible Perimeter of n Blocks.

Java Program to Find Minimum Perimeter of n Blocks

Before jumping into the program directly, let’s see how we can find possible minimum perimeter with ‘n’ blocks.

Explanation:

Given 'n' blocks of size 1*1.

When number of blocks is a perfect square then perimeter = 4*sqrt(n)

When number of blocks is not a perfect square then perimeter = Calculate number of rows and column closest to nearest square root and 2 as 2 extra side will be left after arranging box in rectangular.

 Example:

When n = 4

Minimum possible perimeter = 8

When n = 15

Minimum possible perimeter = 16

Let’s see different ways minimum possible perimeter of n blocks.

Method-1: Java Program to Find Minimum Perimeter of n Blocks By Using Static Value

Approach:

  • Declare number of block.
  • Then call the minPerimeter() method to calculate minimum possible perimeter.
  • Inside the method based on the formula minimum possible perimeter is calculated.
  • Then print the result.

Program:

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

Minimum Perimeter Possible : 8

Method-2: Java Program to Find Minimum Perimeter of n Blocks By User Input Value

Approach:

  • Take user input of number of block.
  • Then call the minPerimeter() method to calculate minimum possible perimeter.
  • Inside the method based on the formula minimum possible perimeter is calculated.
  • Then print the result.

Program:

// JAVA Code to find minimum  
// perimeter using n blocks 
import java.util.*;
public class Main 
{ 
       
    // Driver code 
    public static void main(String[] args)  
    { 
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking user input of number of blocks
        System.out.println("Enter number of Blocks : ");
        int block = sc.nextInt(); 
        //Calling minPerimeter() method
        System.out.println("Minimum Perimeter Possible : "+minPerimeter(block)); 
    } 
    
    //minPerimeter() method
    public static long minPerimeter(int data) 
    { 
        int l = (int) Math.sqrt(data); 
        int sq = l * l; 
      
        // if data is a perfect square 
        if (sq == data)  
            return l * 4; 
        else
        { 
            // Number of rows  
            long row = data / l;  
      
            // perimeter of the  
            // rectangular grid  
            long perimeter  
                  = 2 * (l + row);  
      
            // if there are blocks left  
            if (data % l != 0)  
                perimeter += 2; 
            return perimeter; 
        } 
    } 
}

Output:

Enter number of Blocks : 14
Minimum Perimeter Possible : 16

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs: