Find the Side of the smallest Square that can contain given 4 Big Squares | Java program to Find the Side of the smallest Square that can contain given 4 Big Squares Java implementation of the approach ; Function to find the maximum of two values ; Function to find the smallest side of the suitable suitcase ; sort array to find the smallest and largest side of suitcases ; side of the suitcase will be smallest if they arranged in 2 x 2 way so find all possible sides of that arrangement ; since suitcase should be square so find maximum of all four side ; now find greatest side and that will be the smallest square ; return the result ; Driver program ; Get the side of the 4 small squares ; Find the smallest side ; Get the side of the 4 small squares ; Find the smallest side
import java . util . Arrays ; class GFG { static long max ( long a , long b ) { if ( a > b ) return a ; else return b ; } static long smallestSide ( long a [ ] ) { Arrays . sort ( a ) ; long side1 , side2 , side3 , side4 , side11 , side12 , sideOfSquare ; side1 = a [ 0 ] + a [ 3 ] ; side2 = a [ 1 ] + a [ 2 ] ; side3 = a [ 0 ] + a [ 1 ] ; side4 = a [ 2 ] + a [ 3 ] ; side11 = max ( side1 , side2 ) ; side12 = max ( side3 , side4 ) ; sideOfSquare = max ( side11 , side12 ) ; return sideOfSquare ; } public static void main ( String [ ] args ) { long side [ ] = new long [ 4 ] ; System . out . println ( " Test ▁ Case ▁ 1" ) ; side [ 0 ] = 2 ; side [ 1 ] = 2 ; side [ 2 ] = 2 ; side [ 3 ] = 2 ; System . out . println ( smallestSide ( side ) ) ; System . out . println ( " Test Case 2 "); side [ 0 ] = 100000000000000L ; side [ 1 ] = 123450000000000L ; side [ 2 ] = 987650000000000L ; side [ 3 ] = 987654321000000L ; System . out . println ( smallestSide ( side ) ) ; } }