codeparrot/xlcost-text-to-code huggingface dataset:
Check if given polygon is a convex polygon or not | Java program to implement the above approach ; Utility function to find cross product of two vectors ; Stores coefficient of X direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of X direction of vector A [ 2 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 2 ] A [ 0 ] ; Return cross product ; Function to check if the polygon is convex polygon or not ; Stores count of edges in polygon ; Stores direction of cross product of previous traversed edges ; Stores direction of cross product of current traversed edges ; Traverse the array ; Stores three adjacent edges of the polygon ; Update curr ; If curr is not equal to 0 ; If direction of cross product of all adjacent edges are not same ; Update curr ; Driver code
class GFG { static int CrossProduct ( int A [ ] [ ] ) { int X1 = ( A [ 1 ] [ 0 ] - A [ 0 ] [ 0 ] ) ; int Y1 = ( A [ 1 ] [ 1 ] - A [ 0 ] [ 1 ] ) ; int X2 = ( A [ 2 ] [ 0 ] - A [ 0 ] [ 0 ] ) ; int Y2 = ( A [ 2 ] [ 1 ] - A [ 0 ] [ 1 ] ) ; return ( X1 * Y2 - Y1 * X2 ) ; } static boolean isConvex ( int points [ ] [ ] ) { int N = points . length ; int prev = 0 ; int curr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int temp [ ] [ ] = { points [ i ] , points [ ( i + 1 ) % N ] , points [ ( i + 2 ) % N ] } ; curr = CrossProduct ( temp ) ; if ( curr != 0 ) { if ( curr * prev < 0 ) { return false ; } else { prev = curr ; } } } return true ; } public static void main ( String [ ] args ) { int points [ ] [ ] = { { 0 , 0 } , { 0 , 1 } , { 1 , 1 } , { 1 , 0 } } ; if ( isConvex ( points ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Count number of triangles cut by the given horizontal and vertical line segments | Java program for the above approach ; Store the minimum and maximum X and Y coordinates ; Function to convert String to int ; Function to print the number of triangles cut by each line segment ; Initialize Structure ; Find maximum and minimum X and Y coordinates for each triangle ; Minimum X ; Maximum X ; Minimum Y ; Maximum Y ; Traverse each cut from 0 to M - 1 ; Store number of triangles cut ; Extract value from the line segment String ; If cut is made on X - axis ; Check for each triangle if x lies b / w max and min X coordinates ; If cut is made on Y - axis ; Check for each triangle if y lies b / w max and min Y coordinates ; Print answer for ith cut ; Driver Code ; Given coordinates of triangles ; Given cuts of lines ; Function Call
import java . util . * ; class GFG { static class Tri { int MinX , MaxX , MinY , MaxY ; } ; static int StringtoInt ( String s ) { return Integer . valueOf ( s ) ; } static int min ( int a , int b , int c ) { return Math . min ( a , Math . min ( b , c ) ) ; } static int max ( int a , int b , int c ) { return Math . max ( a , Math . max ( b , c ) ) ; } static void TriangleCuts ( int [ ] [ ] Triangle , String Cuts [ ] , int N , int M , int COL ) { Tri [ ] Minimized = new Tri [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { Minimized [ i ] = new Tri ( ) ; Minimized [ i ] . MaxX = 0 ; Minimized [ i ] . MaxY = 0 ; Minimized [ i ] . MinX = 0 ; Minimized [ i ] . MinY = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { int x1 = Triangle [ i ] [ 0 ] ; int y1 = Triangle [ i ] [ 1 ] ; int x2 = Triangle [ i ] [ 2 ] ; int y2 = Triangle [ i ] [ 3 ] ; int x3 = Triangle [ i ] [ 4 ] ; int y3 = Triangle [ i ] [ 5 ] ; Minimized [ i ] . MinX = min ( x1 , x2 , x3 ) ; Minimized [ i ] . MaxX = max ( x1 , x2 , x3 ) ; Minimized [ i ] . MinY = min ( y1 , y2 , y3 ) ; Minimized [ i ] . MaxY = max ( y1 , y2 , y3 ) ; } for ( int i = 0 ; i < M ; i ++ ) { String Cut = Cuts [ i ] ; int CutCount = 0 ; int CutVal = StringtoInt ( Cut . substring ( 2 , Cut . length ( ) ) ) ; if ( Cut . charAt ( 0 ) == ' X ' ) { for ( int j = 0 ; j < N ; j ++ ) { if ( ( Minimized [ j ] . MinX ) < ( CutVal ) && ( Minimized [ j ] . MaxX ) > ( CutVal ) ) { CutCount ++ ; } } } else if ( Cut . charAt ( 0 ) == ' Y ' ) { for ( int j = 0 ; j < N ; j ++ ) { if ( ( Minimized [ j ] . MinY ) < ( CutVal ) && ( Minimized [ j ] . MaxY ) > ( CutVal ) ) { CutCount ++ ; } } } System . out . print ( CutCount + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] Triangle = { { 0 , 2 , 2 , 9 , 8 , 5 } , { 5 , 0 , 6 , 3 , 7 , 0 } } ; int N = Triangle . length ; int COL = 6 ; String Cuts [ ] = { " X = 2" , " Y = 2" , " Y = 9" } ; int M = Cuts . length ; TriangleCuts ( Triangle , Cuts , N , M , COL ) ; } }
Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points | Java program to implement the above approach ; Function to get the count of ractangles ; Store distinct horizontal lines ; Store distinct Vertical lines ; Insert horizontal line passing through 0 ; Insert vertical line passing through 0. ; Insert horizontal line passing through rectangle [ 3 ] [ 0 ] ; Insert vertical line passing through rectangle [ 3 ] [ 1 ] ; Insert all horizontal and vertical lines passing through the given array ; Insert all horizontal lines ; Insert all vertical lines ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static int cntRect ( int points [ ] [ ] , int N , int rectangle [ ] [ ] ) { HashSet < Integer > cntHor = new HashSet < > ( ) ; HashSet < Integer > cntVer = new HashSet < > ( ) ; cntHor . add ( 0 ) ; cntVer . add ( 0 ) ; cntHor . add ( rectangle [ 3 ] [ 0 ] ) ; cntVer . add ( rectangle [ 3 ] [ 1 ] ) ; for ( int i = 0 ; i < N ; i ++ ) { cntHor . add ( points [ i ] [ 0 ] ) ; cntVer . add ( points [ i ] [ 1 ] ) ; } return ( cntHor . size ( ) - 1 ) * ( cntVer . size ( ) - 1 ) ; } public static void main ( String args [ ] ) { int rectangle [ ] [ ] = { { 0 , 0 } , { 0 , 5 } , { 5 , 0 } , { 5 , 5 } } ; int points [ ] [ ] = { { 1 , 2 } , { 3 , 4 } } ; int N = points . length ; System . out . println ( cntRect ( points , N , rectangle ) ) ; } }
Count squares possible from M and N straight lines parallel to X and Y axis respectively | Java program for the above approach ; Function to count all the possible squares with given lines parallel to both the X and Y axis ; Stores the count of all possible distances in X [ ] & Y [ ] respectively ; Find distance between all pairs in the array X [ ] ; Add the count to m1 ; Find distance between all pairs in the array Y [ ] ; Add the count to m2 ; Find sum of m1 [ i ] * m2 [ i ] for same distance ; Find current count in m2 ; Add to the total count ; Return the final count ; Driver Code ; Given lines ; Function call
import java . io . * ; import java . util . * ; class GFG { static int numberOfSquares ( int [ ] X , int [ ] Y , int N , int M ) { HashMap < Integer , Integer > m1 = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > m2 = new HashMap < Integer , Integer > ( ) ; int i , j , ans = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { int dist = Math . abs ( X [ i ] - X [ j ] ) ; m1 . put ( dist , m1 . getOrDefault ( dist , 0 ) + 1 ) ; } } for ( i = 0 ; i < M ; i ++ ) { for ( j = i + 1 ; j < M ; j ++ ) { int dist = Math . abs ( Y [ i ] - Y [ j ] ) ; m2 . put ( dist , m2 . getOrDefault ( dist , 0 ) + 1 ) ; } } for ( Map . Entry < Integer , Integer > entry : m1 . entrySet ( ) ) { if ( m2 . containsKey ( entry . getKey ( ) ) ) { ans += ( entry . getValue ( ) * m2 . get ( entry . getKey ( ) ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int X [ ] = { 1 , 3 , 7 } ; int Y [ ] = { 2 , 4 , 6 , 1 } ; int N = X . length ; int M = Y . length ; System . out . println ( numberOfSquares ( X , Y , N , M ) ) ; } }
Program to calculate area of a parallelogram | Java program for above approach ; Function to return the area of parallelogram using sides and angle at the intersection of diagonal ; Calculate area of parallelogram ; Return the answer ; Function to return the area of parallelogram using sides and angle at the intersection of sides ; Calculate area of parallelogram ; Return the answer ; Function to return the area of parallelogram using diagonals and angle at the intersection of diagonals ; Calculate area of parallelogram ; Return the answer ; Driver code ; Given diagonal and angle ; Function call ; Print the area
import java . io . * ; class GFG { static double Area_Parallelogram1 ( int a , int b , int theta ) { double area = ( Math . abs ( Math . tan ( Math . toRadians ( theta ) ) ) / 2 ) * Math . abs ( a * a - b * b ) ; return area ; } static double Area_Parallelogram2 ( int a , int b , int gamma ) { double area = ( Math . abs ( Math . sin ( Math . toRadians ( gamma ) ) ) ) * Math . abs ( a * b ) ; return area ; } static double Area_Parallelogram3 ( int d1 , int d2 , int theta ) { double area = ( Math . abs ( Math . sin ( Math . toRadians ( theta ) ) ) / 2 ) * Math . abs ( d1 * d2 ) ; return area ; } public static void main ( String [ ] args ) { int d1 = 3 ; int d2 = 5 ; int theta = 90 ; double area = Area_Parallelogram3 ( d1 , d2 , theta ) ; System . out . format ( " % .2f " , area ) ; } }
Count squares of size K inscribed in a square of size N | Java implementation of the above approach ; Function to calculate the number of squares of size K in a square of size N ; Stores the number of squares ; Driver Code ; Size of the bigger square ; Size of smaller square
import java . util . * ; class GFG { static int No_of_squares ( int N , int K ) { int no_of_squares = 0 ; no_of_squares = ( N - K + 1 ) * ( N - K + 1 ) ; return no_of_squares ; } public static void main ( String [ ] args ) { int N = 5 ; int K = 3 ; System . out . print ( No_of_squares ( N , K ) ) ; } }
Count cubes of size K inscribed in a cube of size N | Java implementation of the above approach ; Function to find the number of the cubes of the size K ; Stores the number of cubes ; Stores the number of cubes of size k ; Driver Code ; Size of the bigger cube ; Size of the smaller cube
class GFG { static int No_of_cubes ( int N , int K ) { int No = 0 ; No = ( N - K + 1 ) ; No = ( int ) Math . pow ( No , 3 ) ; return No ; } public static void main ( String [ ] args ) { int N = 5 ; int K = 2 ; System . out . print ( No_of_cubes ( N , K ) ) ; } }
Count of smaller rectangles that can be placed inside a bigger rectangle | Java program for the above approach ; Function to count smaller rectangles within the larger rectangle ; If the dimension of the smaller rectangle is greater than the bigger one ; Return the number of smaller rectangles possible ; Driver Code ; Dimension of bigger rectangle ; Dimension of smaller rectangle ; Function call
import java . util . * ; class GFG { static int No_of_rectangles ( int L , int B , int l , int b ) { if ( ( l > L ) || ( b > B ) ) { return - 1 ; } else { return ( L - l + 1 ) * ( B - b + 1 ) ; } } public static void main ( String [ ] args ) { int L = 5 , B = 3 ; int l = 4 , b = 1 ; System . out . println ( No_of_rectangles ( L , B , l , b ) ) ; } }
Program To Check whether a Triangle is Equilateral , Isosceles or Scalene | Java program for the above approach ; Function to check if the triangle is equilateral or isosceles or scalene ; Check for equilateral triangle ; Check for isosceles triangle ; Otherwise scalene triangle ; Driver Code ; Given sides of triangle ; Function call
class GFG { static void checkTriangle ( int x , int y , int z ) { if ( x == y && y == z ) System . out . println ( " Equilateral ▁ Triangle " ) ; else if ( x == y y == z z == x ) System . out . println ( " Isosceles ▁ Triangle " ) ; else System . out . println ( " Scalene ▁ Triangle " ) ; } public static void main ( String [ ] args ) { int x = 8 , y = 7 , z = 9 ; checkTriangle ( x , y , z ) ; } }
Find the remaining vertices of a square from two given vertices | Java program for the above approach ; Function to find the remaining vertices of a square ; Check if the x - coordinates are equal ; Check if the y - coordinates are equal ; If the the given coordinates forms a diagonal of the square ; Otherwise ; Square does not exist ; Driver Code ; Given two vertices
import java . util . * ; class GFG { static void findVertices ( int x1 , int y1 , int x2 , int y2 ) { if ( x1 == x2 ) { System . out . print ( ( x1 + y2 - y1 ) + " , ▁ " + y1 + "NEW_LINE"); System . out . print ( ( x2 + y2 - y1 ) + " , ▁ " + y2 ) ; } else if ( y1 == y2 ) { System . out . print ( x1 + " , ▁ " + ( y1 + x2 - x1 ) + "NEW_LINE"); System . out . print ( x2 + " , ▁ " + ( y2 + x2 - x1 ) ) ; } else if ( Math . abs ( x2 - x1 ) == Math . abs ( y2 - y1 ) ) { System . out . print ( x1 + " , ▁ " + y2 + "NEW_LINE"); System . out . print ( x2 + " , ▁ " + y1 ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int x1 = 1 , y1 = 2 ; int x2 = 3 , y2 = 4 ; findVertices ( x1 , y1 , x2 , y2 ) ; } }
Find the area of rhombus from given Angle and Side length | Java Program to calculate area of rhombus from given angle and side length ; Function to return the area of rhombus using one angle and side . ; Driver Code ; Function Call ; Print the final answer
class GFG { static final double RADIAN = 0.01745329252 ; static double Area_of_Rhombus ( int a , int theta ) { double area = ( a * a ) * Math . sin ( ( RADIAN * theta ) ) ; return area ; } public static void main ( String [ ] args ) { int a = 4 ; int theta = 60 ; double ans = Area_of_Rhombus ( a , theta ) ; System . out . printf ( " % .2f " , ans ) ; } }
Count of Equilateral Triangles of unit length possible from a given Hexagon | Java program to implement the above approach ; Function to calculate the the number of Triangles possible ; Driver Code ; Regular Hexagon ; Irregular Hexagon
import java . util . * ; class GFG { static int calculateTriangles ( int sides [ ] ) { double count = Math . pow ( sides [ 0 ] + sides [ 1 ] + sides [ 2 ] , 2 ) ; count -= Math . pow ( sides [ 0 ] , 2 ) ; count -= Math . pow ( sides [ 2 ] , 2 ) ; count -= Math . pow ( sides [ 4 ] , 2 ) ; return ( int ) ( count ) ; } public static void main ( String [ ] args ) { int sides [ ] = { 1 , 1 , 1 , 1 , 1 , 1 } ; System . out . print ( ( calculateTriangles ( sides ) ) + "NEW_LINE"); int sides1 [ ] = { 2 , 2 , 1 , 3 , 1 , 2 } ; System . out . print ( ( calculateTriangles ( sides1 ) ) + "NEW_LINE"); } }
Length of diagonal of a parallelogram using adjacent sides and angle between them | Java program to find length Of diagonal of a parallelogram Using sides and angle between them . ; Function to return the length Of diagonal of a parallelogram using sides and angle between them . ; Driver Code ; Given sides ; Given angle ; Function call ; Print the final answer
class GFG { static double Length_Diagonal ( int a , int b , double theta ) { double diagonal = Math . sqrt ( ( Math . pow ( a , 2 ) + Math . pow ( b , 2 ) ) - 2 * a * b * Math . cos ( theta * ( Math . PI / 180 ) ) ) ; return diagonal ; } public static void main ( String [ ] args ) { int a = 3 ; int b = 5 ; double theta = 45 ; double ans = Length_Diagonal ( a , b , theta ) ; System . out . printf ( " % .2f " , ans ) ; } }
Maximum number of tiles required to cover the floor of given size using 2 x1 size tiles | Java program for the above approach ; Function to find the maximum number of tiles required to cover the floor of size m x n using 2 x 1 size tiles ; Print the answer ; Driver code ; Given M and N ; Function call
import java . util . * ; class GFG { static void maximumTiles ( int n , int m ) { System . out . println ( ( m * n ) / 2 ) ; } public static void main ( String [ ] args ) { int M = 3 ; int N = 4 ; maximumTiles ( N , M ) ; } }
Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal | Java Program to implement the above approach ; Function to calculate the length of the diagonal of a parallelogram using two sides and other diagonal ; Driver Code ; Function Call ; Print the final answer
class GFG { static float Length_Diagonal ( int a , int b , int d ) { float diagonal = ( float ) Math . sqrt ( 2 * ( ( a * a ) + ( b * b ) ) - ( d * d ) ) ; return diagonal ; } public static void main ( String [ ] args ) { int A = 10 ; int B = 30 ; int D = 20 ; float ans = Length_Diagonal ( A , B , D ) ; System . out . printf ( " % .1f " , ans ) ; } }
Length of Diagonals of a Cyclic Quadrilateral using the length of Sides . | Java Program to implement the above approach ; Function to calculate the length of diagonals of a cyclic quadrilateral ; Driver Code ; Function Call ; Print the final answer
import java . util . * ; class GFG { static Vector < Float > Diagonals ( int a , int b , int c , int d ) { Vector < Float > ans = new Vector < Float > ( ) ; ans . add ( ( float ) Math . sqrt ( ( ( a * c ) + ( b * d ) ) * ( ( a * d ) + ( b * c ) ) / ( ( a * b ) + ( c * d ) ) ) ) ; ans . add ( ( float ) Math . sqrt ( ( ( a * c ) + ( b * d ) ) * ( ( a * b ) + ( c * d ) ) / ( ( a * d ) + ( b * c ) ) ) ) ; return ans ; } public static void main ( String [ ] args ) { int A = 10 ; int B = 15 ; int C = 20 ; int D = 25 ; Vector < Float > ans = Diagonals ( A , B , C , D ) ; System . out . printf ( " % .2f ▁ % .2f " , ( ans . get ( 0 ) ) + .01 , ans . get ( 1 ) + .01 ) ; } }
Minimum Sum of Euclidean Distances to all given Points | Java program to implement the above approach ; Function to calculate Euclidean distance ; Function to calculate the minimum sum of the euclidean distances to all points ; Calculate the centroid ; Calculate distance of all points ; Driver Code ; Initializing the points
class GFG { static double find ( double x , double y , int [ ] [ ] p ) { double mind = 0 ; for ( int i = 0 ; i < p . length ; i ++ ) { double a = p [ i ] [ 0 ] , b = p [ i ] [ 1 ] ; mind += Math . sqrt ( ( x - a ) * ( x - a ) + ( y - b ) * ( y - b ) ) ; } return mind ; } static double getMinDistSum ( int [ ] [ ] p ) { double x = 0 , y = 0 ; for ( int i = 0 ; i < p . length ; i ++ ) { x += p [ i ] [ 0 ] ; y += p [ i ] [ 1 ] ; } x = x / p . length ; y = y / p . length ; double mind = find ( x , y , p ) ; return mind ; } public static void main ( String [ ] args ) { int [ ] [ ] vec = { { 0 , 1 } , { 1 , 0 } , { 1 , 2 } , { 2 , 1 } } ; double d = getMinDistSum ( vec ) ; System . out . print ( d + "NEW_LINE"); } }
Coplanarity of Two Lines in 3D Geometry | Java program implement the above approach ; Function to generate determinant ; Return the sum ; Driver Code ; Position vector of first line ; Direction ratios of line to which first line is parallel ; Position vectors of second line ; Direction ratios of line to which second line is parallel ; Determinant to check coplanarity ; If determinant is zero ; Otherwise
import java . io . * ; class GFG { static int det ( int [ ] [ ] d ) { int Sum = d [ 0 ] [ 0 ] * ( ( d [ 1 ] [ 1 ] * d [ 2 ] [ 2 ] ) - ( d [ 2 ] [ 1 ] * d [ 1 ] [ 2 ] ) ) ; Sum -= d [ 0 ] [ 1 ] * ( ( d [ 1 ] [ 0 ] * d [ 2 ] [ 2 ] ) - ( d [ 1 ] [ 2 ] * d [ 2 ] [ 0 ] ) ) ; Sum += d [ 0 ] [ 2 ] * ( ( d [ 0 ] [ 1 ] * d [ 1 ] [ 2 ] ) - ( d [ 0 ] [ 2 ] * d [ 1 ] [ 1 ] ) ) ; return Sum ; } public static void main ( String [ ] args ) { int x1 = - 3 , y1 = 1 , z1 = 5 ; int a1 = - 3 , b1 = 1 , c1 = 5 ; int x2 = - 1 , y2 = 2 , z2 = 5 ; int a2 = - 1 , b2 = 2 , c2 = 5 ; int [ ] [ ] det_list = { { x2 - x1 , y2 - y1 , z2 - z1 } , { a1 , b1 , c1 } , { a2 , b2 , c2 } } ; if ( det ( det_list ) == 0 ) System . out . print ( " Lines ▁ are ▁ coplanar " ) ; else System . out . print ( " Lines ▁ are ▁ non ▁ coplanar " ) ; } }
Distance between Incenter and Circumcenter of a triangle using Inradius and Circumradius | Java program for the above approach ; Function returns the required distance ; Driver code ; Length of Inradius ; Length of Circumradius
import java . util . * ; class GFG { static double distance ( int r , int R ) { double d = Math . sqrt ( Math . pow ( R , 2 ) - ( 2 * r * R ) ) ; return d ; } public static void main ( String [ ] args ) { int r = 2 ; int R = 5 ; System . out . println ( Math . round ( distance ( r , R ) * 100.0 ) / 100.0 ) ; } }
Find N random points within a Circle | Java program for the above approach ; Return a random double between 0 & 1 ; Function to find the N random points on the given circle ; Result vector ; Get Angle in radians ; Get length from center ; Add point to results . ; Return the N points ; Function to display the content of the vector A ; Iterate over A ; Print the N random points stored ; Driver Code ; Given dimensions ; Function call
import java . util . * ; class GFG { static final double PI = 3.141592653589 ; static class pair { double first , second ; public pair ( double first , double second ) { super ( ) ; this . first = first ; this . second = second ; } } static double uniform ( ) { return Math . random ( ) ; } static Vector < pair > randPoint ( int r , int x , int y , int n ) { Vector < pair > res = new Vector < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { double theta = 2 * PI * uniform ( ) ; double len = Math . sqrt ( uniform ( ) ) * r ; res . add ( new pair ( x + len * Math . cos ( theta ) , y + len * Math . sin ( theta ) ) ) ; } return res ; } static void printVector ( Vector < pair > A ) { for ( pair P : A ) { System . out . printf ( "(%.2f, %.2f)NEW_LINE", P.first, P.second); } } public static void main ( String [ ] args ) { int R = 12 ; int X = 3 ; int Y = 3 ; int N = 5 ; printVector ( randPoint ( R , X , Y , N ) ) ; } }
Check if an N | Java program for the above approach ; Function to check if the polygon exists or not ; Initialize a variable to store the sum of angles ; Loop through the array and calculate the sum of angles ; Check the condition for an N - side polygon ; Driver code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static void checkValidPolygon ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } if ( sum == 180 * ( N - 2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 3 ; int arr [ ] = { 60 , 60 , 60 } ; checkValidPolygon ( arr , N ) ; } }
Find side of Square which makes minimal area to fit two identical rectangles inside it | Java program of the above approach ; If ' a ' and ' b ' same then double ' a ' or ' b ' and return ( 2 * a ) or ( 2 * b ) ; Check if a != b ; If a > b ; Double the smaller value that is ' b ' and store it to ' newB ' ; Find the difference of ' newB ▁ and ▁ ' a ' ; If ' newB ' < a ; Then add the difference of ' newB ' and ' a ' to the ' b ' to make ' b ' and ' a ' as same ; Return side of the square a or b ; If ' newB ' > a then then add the difference of ' newB ' and ' a ' to the ' a ' to make ' a ' and ' newB ' as same ; Return side of the square a or newB ; If a < b ; Double the smaller value that is ' a ' and store it to ' newA ' ; Find the difference of ' newA ▁ and ▁ ' b ' ; If ' newA ' < b ; Then add the difference of ' newA ' and ' b ' to the ' a ' to make ' a ' and ' b ' as same ; Return side of the square a or b ; If ' newA ' > b then then add the difference of ' newA ' and ' b ' to the ' b ' to make ' b ' and ' newA ' as same ; Return side of the square b or newA ; Driver code ; Size of rectangle
class GFG { public static int minimalSquareSide ( int a , int b ) { if ( a == b ) { return 2 * a ; } if ( a != b ) { if ( a > b ) { int newB = b + b ; int diff = Math . abs ( newB - a ) ; if ( newB < a ) { b = newB + diff ; if ( a == b ) return a ; return 0 ; } else { a = a + diff ; if ( a == newB ) return a ; return 0 ; } } else { int newA = a + a ; int diff = Math . abs ( newA - b ) ; if ( newA < b ) { a = diff + newA ; if ( a == b ) return a ; return 0 ; } else { b = b + diff ; if ( b == newA ) return b ; return 0 ; } } } return 0 ; } public static void main ( String [ ] args ) { int H , W ; H = 3 ; W = 1 ; System . out . println ( minimalSquareSide ( H , W ) ) ; } }
Find the angle of Rotational Symmetry of an N | Java program to find the angle of Rotational Symmetry of an N - sided regular polygon ; function to find required minimum angle of rotation ; Store the answer in a double variable ; Calculating the angle of rotation and type - casting the integer N to double type ; Driver code
import java . io . * ; class GFG { static double minAnglRot ( int N ) { double res ; res = 360 / ( double ) N ; return res ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( " Angle ▁ of ▁ Rotational ▁ Symmetry : ▁ " + minAnglRot ( N ) ) ; } }