codeparrot/xlcost-text-to-code huggingface dataset:
Find Nth term ( A matrix exponentiation example ) | Java program to find n - th term of a recursive function using matrix exponentiation . ; This power function returns first row of { Transformation Matrix } ^ n - 1 * Initial Vector ; This is an identity matrix . ; this is Transformation matrix . ; Matrix exponentiation to calculate power of { tMat } ^ n - 1 store res in " res " matrix . ; res store { Transformation matrix } ^ n - 1 hence wiint be first row of res * Initial Vector . ; Driver code
class GfG { static int MAX = 100 ; static int MOD = 1000000009 ; static int power ( int n ) { if ( n <= 1 ) { return 1 ; } n -- ; int res [ ] [ ] = { { 1 , 0 } , { 0 , 1 } } ; int tMat [ ] [ ] = { { 2 , 3 } , { 1 , 0 } } ; while ( n > 0 ) { if ( n % 2 == 1 ) { int tmp [ ] [ ] = new int [ 2 ] [ 2 ] ; tmp [ 0 ] [ 0 ] = ( res [ 0 ] [ 0 ] * tMat [ 0 ] [ 0 ] + res [ 0 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 0 ] [ 1 ] = ( res [ 0 ] [ 0 ] * tMat [ 0 ] [ 1 ] + res [ 0 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; tmp [ 1 ] [ 0 ] = ( res [ 1 ] [ 0 ] * tMat [ 0 ] [ 0 ] + res [ 1 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 1 ] [ 1 ] = ( res [ 1 ] [ 0 ] * tMat [ 0 ] [ 1 ] + res [ 1 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; res [ 0 ] [ 0 ] = tmp [ 0 ] [ 0 ] ; res [ 0 ] [ 1 ] = tmp [ 0 ] [ 1 ] ; res [ 1 ] [ 0 ] = tmp [ 1 ] [ 0 ] ; res [ 1 ] [ 1 ] = tmp [ 1 ] [ 1 ] ; } n = n / 2 ; int tmp [ ] [ ] = new int [ 2 ] [ 2 ] ; tmp [ 0 ] [ 0 ] = ( tMat [ 0 ] [ 0 ] * tMat [ 0 ] [ 0 ] + tMat [ 0 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 0 ] [ 1 ] = ( tMat [ 0 ] [ 0 ] * tMat [ 0 ] [ 1 ] + tMat [ 0 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; tmp [ 1 ] [ 0 ] = ( tMat [ 1 ] [ 0 ] * tMat [ 0 ] [ 0 ] + tMat [ 1 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 1 ] [ 1 ] = ( tMat [ 1 ] [ 0 ] * tMat [ 0 ] [ 1 ] + tMat [ 1 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; tMat [ 0 ] [ 0 ] = tmp [ 0 ] [ 0 ] ; tMat [ 0 ] [ 1 ] = tmp [ 0 ] [ 1 ] ; tMat [ 1 ] [ 0 ] = tmp [ 1 ] [ 0 ] ; tMat [ 1 ] [ 1 ] = tmp [ 1 ] [ 1 ] ; } return ( res [ 0 ] [ 0 ] * 1 + res [ 0 ] [ 1 ] * 1 ) % MOD ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( power ( n ) ) ; } }
Shuffle 2 n integers in format { a1 , b1 , a2 , b2 , a3 , b3 , ... ... , an , bn } without using extra space | Java Naive program to shuffle an array of size 2 n ; method to shuffle an array of size 2 n ; Rotate the element to the left ; swap a [ j - 1 ] , a [ j ] ; Driver Method
import java . util . Arrays ; public class GFG { static void shuffleArray ( int a [ ] , int n ) { for ( int i = 0 , q = 1 , k = n ; i < n ; i ++ , k ++ , q ++ ) for ( int j = k ; j > i + q ; j -- ) { int temp = a [ j - 1 ] ; a [ j - 1 ] = a [ j ] ; a [ j ] = temp ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 5 , 7 , 2 , 4 , 6 , 8 } ; shuffleArray ( a , a . length / 2 ) ; System . out . println ( Arrays . toString ( a ) ) ; } }
Place k elements such that minimum distance is maximized | Java program to find largest minimum distance among k points . ; Returns true if it is possible to arrange k elements of arr [ 0. . n - 1 ] with minimum distance given as mid . ; Place first element at arr [ 0 ] position ; Initialize count of elements placed . ; Try placing k elements with minimum distance mid . ; Place next element if its distance from the previously placed element is greater than current mid ; Return if all elements are placed successfully ; Returns largest minimum distance for k elements in arr [ 0. . n - 1 ] . If elements can 't be placed, returns -1. ; Sort the positions ; Initialize result . ; Consider the maximum possible distance ; Do binary search for largest minimum distance ; If it is possible to place k elements with minimum distance mid , search for higher distance . ; Change value of variable max to mid if all elements can be successfully placed ; If not possible to place k elements , search for lower distance ; driver code
import java . util . Arrays ; class GFG { static boolean isFeasible ( int mid , int arr [ ] , int n , int k ) { int pos = arr [ 0 ] ; int elements = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - pos >= mid ) { pos = arr [ i ] ; elements ++ ; if ( elements == k ) return true ; } } return false ; } static int largestMinDist ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int res = - 1 ; int left = 1 , right = arr [ n - 1 ] ; while ( left < right ) { int mid = ( left + right ) / 2 ; if ( isFeasible ( mid , arr , n , k ) ) { res = Math . max ( res , mid ) ; left = mid + 1 ; } else right = mid ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 8 , 4 , 9 } ; int n = arr . length ; int k = 3 ; System . out . print ( largestMinDist ( arr , n , k ) ) ; } }
Find frequency of each element in a limited range array in less than O ( n ) time | Java program to count number of occurrences of each element in the array ; It prints number of occurrences of each element in the array . ; HashMap to store frequencies ; traverse the array ; update the frequency ; traverse the hashmap ; Driver function
import java . io . * ; import java . util . * ; class GFG { static void findFrequency ( int [ ] arr , int n ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . containsKey ( arr [ i ] ) ) mp . put ( arr [ i ] , 0 ) ; mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } for ( Map . Entry < Integer , Integer > kvp : mp . entrySet ( ) ) { System . out . println ( " Element ▁ " + kvp . getKey ( ) + " ▁ occurs ▁ " + kvp . getValue ( ) + " ▁ times " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int n = arr . length ; findFrequency ( arr , n ) ; } }
Square root of an integer | A Java program to find floor ( sqrt ( x ) ; Base Cases ; Do Binary Search for floor ( sqrt ( x ) ) ; If x is a perfect square ; Since we need floor , we update answer when mid * mid is smaller than x , and move closer to sqrt ( x ) ; else If mid * mid is greater than x ; Driver Method
public class Test { public static int floorSqrt ( int x ) { if ( x == 0 x == 1 ) return x ; long start = 1 , end = x , ans = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( mid * mid == x ) return ( int ) mid ; if ( mid * mid < x ) { start = mid + 1 ; ans = mid ; } end = mid - 1 ; } return ( int ) ans ; } public static void main ( String args [ ] ) { int x = 11 ; System . out . println ( floorSqrt ( x ) ) ; } }
Sine Rule with Derivation , Example and Implementation | Java program for the above approach ; Function to calculate remaining two sides ; Calculate angle B ; Convert angles to their respective radians for using trigonometric functions ; Sine rule ; Print the answer ; Driver code ; Input ; Function Call
class GFG { static void findSides ( double A , double C , double c ) { double B = 180 - ( A + C ) ; A = A * ( 3.14159 / 180 ) ; C = C * ( 3.14159 / 180 ) ; B = B * ( 3.14159 / 180 ) ; double a = ( c / Math . sin ( C ) ) * Math . sin ( A ) ; double b = ( c / Math . sin ( C ) ) * Math . sin ( B ) ; System . out . println ( String . format ( " % .2f " , a ) ) ; System . out . println ( String . format ( " % .2f " , b ) ) ; } public static void main ( String [ ] args ) { double A = 45.0 ; double C = 35.0 ; double c = 23 ; findSides ( A , C , c ) ; } }
Equation of a normal to a Circle from a given point | Java program for the above approach ; Function to calculate the slope ; Store the coordinates the center of the circle ; If slope becomes infinity ; Stores the slope ; If slope is zero ; Return the result ; Function to find the equation of the normal to a circle from a given point ; Stores the slope of the normal ; If slope becomes infinity ; If slope is zero ; Otherwise , print the equation of the normal ; Driver Code ; Given Input ; Function Call
import java . util . * ; class GFG { static double normal_slope ( double a , double b , double x1 , double y1 ) { double g = a / 2 ; double f = b / 2 ; if ( g - x1 == 0 ) return ( - 1 ) ; double slope = ( f - y1 ) / ( g - x1 ) ; if ( slope == 0 ) return ( - 2 ) ; return slope ; } static void normal_equation ( double a , double b , double x1 , double y1 ) { double slope = normal_slope ( a , b , x1 , y1 ) ; if ( slope == - 1 ) { System . out . print ( " x ▁ = ▁ " + x1 ) ; } if ( slope == - 2 ) { System . out . print ( " y ▁ = ▁ " + y1 ) ; } if ( slope != - 1 && slope != - 2 ) { x1 *= - slope ; x1 += y1 ; if ( x1 > 0 ) System . out . print ( " y ▁ = ▁ " + slope + " x ▁ + ▁ " + x1 ) ; else System . out . print ( " y ▁ = ▁ " + slope + " x ▁ " + x1 ) ; } } public static void main ( String [ ] args ) { int a = 4 , b = 6 ; int x1 = 12 , y1 = 14 ; normal_equation ( a , b , x1 , y1 ) ; } }
Sum of squares of distances between all pairs from given points | Java program for the above approach ; Function to find the sum of squares of distance between all distinct pairs ; Stores final answer ; Traverse the array ; Adding the effect of this point for all the previous x - points ; Temporarily add the square of x - coordinate ; Add the effect of this point for all the previous y - points ; Print the desired answer ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void findSquareSum ( int Coordinates [ ] [ ] , int N ) { long xq = 0 , yq = 0 ; long xs = 0 , ys = 0 ; long res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int a , b ; a = Coordinates [ i ] [ 0 ] ; b = Coordinates [ i ] [ 1 ] ; res += xq ; res -= 2 * xs * a ; res += i * ( long ) ( a * a ) ; xq += a * a ; xs += a ; res += yq ; res -= 2 * ys * b ; res += i * ( long ) b * b ; yq += b * b ; ys += b ; } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 1 } , { - 1 , - 1 } , { 1 , - 1 } , { - 1 , 1 } } ; int N = arr . length ; findSquareSum ( arr , N ) ; } }
Count points from an array that lies inside a semi | Java program for above approach ; Traverse the array ; Stores if a point lies above the diameter or not ; Stores if the R is less than or equal to the distance between center and point ; Driver code ;
import java . io . * ; class Gfg { public static int getPointsIns ( int x1 , int y1 , int radius , int x2 , int y2 , pair points [ ] ) { int ans = 0 ; for ( int i = 0 ; i < points . length ; i ++ ) { boolean condOne = false , condTwo = false ; if ( ( points [ i ] . b - y2 ) * ( x2 - x1 ) - ( y2 - y1 ) * ( points [ i ] . a - x2 ) >= 0 ) { condOne = true ; } if ( radius >= ( int ) Math . sqrt ( Math . pow ( ( y1 - points [ i ] . b ) , 2 ) + Math . pow ( x1 - points [ i ] . a , 2 ) ) ) { condTwo = true ; } if ( condOne && condTwo ) { ans += 1 ; } } return ans ; } public static void main ( String [ ] args ) { int X = 0 ; int Y = 0 ; int R = 5 ; int P = 5 ; int Q = 0 ; pair arr [ ] = { new pair ( 2 , 3 ) , new pair ( 5 , 6 ) , new pair ( - 1 , 4 ) , new pair ( 5 , 5 ) } ; System . out . print ( getPointsIns ( X , Y , R , P , Q , arr ) ) ; } } class pair { int a ; int b ; pair ( int a , int b ) { this . a = a ; this . b = b ; } }
Count pairs of points having distance between them equal to integral values in a K | Java program for the above approach ; Function to find pairs whose distance between the points of is an integer value . ; Stores count of pairs whose distance between points is an integer ; Traverse the array , points [ ] ; Stores distance between points ( i , j ) ; Traverse all the points of current pair ; Update temp ; Update dist ; If dist is a perfect square ; Update ans ; Driver Code ; Given value of K ; Given points ; Given value of N ; Function Call
class GFG { static void cntPairs ( int [ ] [ ] points , int n , int K ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int dist = 0 ; for ( int k = 0 ; k < K ; k ++ ) { int temp = ( points [ i ] [ k ] - points [ j ] [ k ] ) ; dist += temp * temp ; } if ( Math . sqrt ( dist ) * Math . sqrt ( dist ) == dist ) { ans += 1 ; } } } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int K = 2 ; int [ ] [ ] points = { { 1 , 2 } , { 5 , 5 } , { - 2 , 8 } } ; int n = points . length ; cntPairs ( points , n , K ) ; } }
Circumradius of a Cyclic Quadrilateral using the length of Sides | Java program to find circumradius of a cyclic quadrilateral using sides ; Function to return the circumradius of a cyclic quadrilateral using sides ; Find semiperimeter ; Calculate the radius ; Driver Code ; Function call ; Print the radius
import java . util . * ; class GFG { static double Circumradius ( int a , int b , int c , int d ) { double s = ( a + b + c + d ) / 2.0 ; double radius = Math . sqrt ( ( ( a * b ) + ( c * d ) ) * ( ( a * c ) + ( b * d ) ) * ( ( a * d ) + ( b * c ) ) / ( ( s - a ) * ( s - b ) * ( s - c ) * ( s - d ) ) ) ; return radius / 4 ; } public static void main ( String [ ] args ) { int A = 3 ; int B = 4 ; int C = 5 ; int D = 6 ; double ans = Circumradius ( A , B , C , D ) ; System . out . format ( " % .2f " , ans ) ; } }
Area of Triangle using Side | Java program to calculate the area of a triangle when the length of two adjacent sides and the angle between them is provided ; Function to return the area of triangle using Side - Angle - Side formula ; Driver Code ; Function Call ; Print the final answer
class GFG { static float Area_of_Triangle ( int a , int b , int k ) { float area = ( float ) ( ( 1 / 2.0 ) * a * b * Math . sin ( k ) ) ; return area ; } public static void main ( String [ ] args ) { int a = 9 ; int b = 12 ; int k = 2 ; float ans = Area_of_Triangle ( a , b , k ) ; System . out . printf ( " % .1f " , ans ) ; } }
Find the maximum angle at which we can tilt the bottle without spilling any water | Java program to find the maximum angle at which we can tilt the bottle without spilling any water ; Now we have the volume of rectangular prism a * a * b ; Now we have 2 cases ! ; Taking the tangent inverse of value d As we want to take out the required angle ; Taking the tangent inverse of value d As we want to take out the required angle ; As of now the angle is in radian . So we have to convert it in degree . ; Driver Code ; Enter the Base square side length ; Enter the Height of rectangular prism ; Enter the Base square side length
class GFG { static float find_angle ( int x , int y , int z ) { int volume = x * x * y ; float ans = 0 ; if ( z < volume / 2 ) { float d = ( float ) ( ( x * y * y ) / ( 2.0 * z ) ) ; ans = ( float ) Math . atan ( d ) ; } else { z = volume - z ; float d = ( 2 * z ) / ( float ) ( x * x * x ) ; ans = ( float ) Math . atan ( d ) ; } ans = ( float ) ( ( ans * 180 ) / 3.14159265 ) ; return ans ; } public static void main ( String [ ] args ) { int x = 12 ; int y = 21 ; int z = 10 ; System . out . print ( find_angle ( x , y , z ) + "NEW_LINE"); } }
Count of distinct rectangles inscribed in an equilateral triangle | Java implementation of the approach ; Function to return the count of rectangles when n is odd ; Calculating number of dots in vertical level ; Calculating number of ways to select two points in the horizontal level i ; Multiply both to obtain the number of rectangles formed at that level ; Calculating number of dots in vertical level ; Calculating number of ways to select two points in the horizontal level i ; Multiply both to obtain the number of rectangles formed at that level ; Function to return the count of rectangles when n is even ; Driver code ; If n is odd
import java . io . * ; class GFG { static int countOdd ( int n ) { int coun = 0 , m , j , i ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( i >= 1 ) { m = ( n - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } else { m = ( ( n - 1 ) - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } } return coun ; } static int countEven ( int n ) { int coun = 0 , m , j , i ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( i >= 1 ) { m = ( ( n - 1 ) - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } else { m = ( n - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } } return coun ; } public static void main ( String [ ] args ) { int n = 5 ; if ( n >= 1 ) System . out . println ( countOdd ( n ) ) ; else System . out . println ( countEven ( n ) ) ; } }
Area of the biggest ellipse inscribed within a rectangle | Java Program to find the biggest rectangle which can be inscribed within the ellipse ; Function to find the area of the rectangle ; The sides cannot be negative ; Area of the ellipse ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static float ellipse ( float l , float b ) { if ( l < 0 b < 0 ) return - 1 ; float x = ( float ) ( 3.14 * l * b ) / 4 ; return x ; } public static void main ( String args [ ] ) { float a = 5 , b = 3 ; System . out . println ( ellipse ( a , b ) ) ; } }
Determine the number of squares of unit area that a given line will pass through . | Java program to determine the number of squares that line will pass through ; Function to return the required position ; Driver Code
class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static void noOfSquares ( int x1 , int y1 , int x2 , int y2 ) { int dx = Math . abs ( x2 - x1 ) ; int dy = Math . abs ( y2 - y1 ) ; int ans = dx + dy - __gcd ( dx , dy ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int x1 = 1 , y1 = 1 , x2 = 4 , y2 = 3 ; noOfSquares ( x1 , y1 , x2 , y2 ) ; } }
Count paths with distance equal to Manhattan distance | Java implementation of the approach ; Function to return the value of nCk ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Function to return the number of paths ; Difference between the ' x ' coordinates of the given points ; Difference between the ' y ' coordinates of the given points ; Driver code
class GfG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long countPaths ( int x1 , int y1 , int x2 , int y2 ) { int m = Math . abs ( x1 - x2 ) ; int n = Math . abs ( y1 - y2 ) ; return ( binomialCoeff ( m + n , n ) ) ; } public static void main ( String [ ] args ) { int x1 = 2 , y1 = 3 , x2 = 4 , y2 = 5 ; System . out . println ( countPaths ( x1 , y1 , x2 , y2 ) ) ; } }
Find the area of largest circle inscribed in ellipse | Java Program to find the area of circle ; Area of the Reuleaux triangle ; Driver code
class GFG { static double areaCircle ( double b ) { double area = ( double ) 3.1415926 * b * b ; return area ; } public static void main ( String args [ ] ) { float a = 10 , b = 8 ; System . out . println ( areaCircle ( b ) ) ; } }
Section formula for 3 D | Java program to find point that divides given line in given ratio in 3D . ; Function to find the section of the line ; Applying section formula ; Printing result ; Driver code
import java . util . * ; class solution { static void section ( double x1 , double x2 , double y1 , double y2 , double z1 , double z2 , double m , double n ) { double x = ( ( m * x2 ) + ( n * x1 ) ) / ( m + n ) ; double y = ( ( m * y2 ) + ( n * y1 ) ) / ( m + n ) ; double z = ( ( m * z2 ) + ( n * z1 ) ) / ( m + n ) ; System . out . print ( " ( " + x + " , ▁ " ) ; System . out . print ( y + " , ▁ " ) ; System . out . println ( z + " ) " ) ; } public static void main ( String arr [ ] ) { double x1 = 2 , x2 = 4 , y1 = - 1 , y2 = 3 , z1 = 4 , z2 = 2 , m = 2 , n = 3 ; section ( x1 , x2 , y1 , y2 , z1 , z2 , m , n ) ; } }
Program to find the Circumcircle of any regular polygon | Java Program to find the radius of the circumcircle of the given polygon ; Function to find the radius of the circumcircle ; these cannot be negative ; Radius of the circumcircle ; Return the radius ; Driver code ; Find the radius of the circumcircle
import java . io . * ; class GFG { static float findRadiusOfcircumcircle ( float n , float a ) { if ( n < 0 a < 0 ) return - 1 ; float radius = ( float ) ( a / Math . sqrt ( 2 - ( 2 * Math . cos ( 360 / n ) ) ) ) ; return radius ; } public static void main ( String [ ] args ) { float n = 5 , a = 6 ; System . out . println ( findRadiusOfcircumcircle ( n , a ) ) ; } }
Program to find the Radius of the incircle of the triangle | Java Program to find the radius of the incircle of the given triangle ; Function to find the radius of the incircle ; the sides cannot be negative ; semi - perimeter of the circle ; area of the triangle ; Radius of the incircle ; Return the radius ; Driver code ; Get the sides of the triangle ; Find the radius of the incircle
import java . io . * ; class GFG { static float findRadiusOfIncircle ( float a , float b , float c ) { if ( a < 0 b < 0 c < 0 ) return - 1 ; float p = ( a + b + c ) / 2 ; float area = ( float ) Math . sqrt ( p * ( p - a ) * ( p - b ) * ( p - c ) ) ; float radius = area / p ; return radius ; } public static void main ( String [ ] args ) { float a = 2 , b = 2 , c = 3 ; System . out . println ( findRadiusOfIncircle ( a , b , c ) ) ; } }
Find area of triangle if two vectors of two adjacent sides are given | Java program to calculate area of triangle if vectors of 2 adjacent sides are given ; function to calculate area of triangle ; Driver program
import java . util . * ; class solution { static float area ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 ) { double a = Math . pow ( ( y1 * z2 - y2 * z1 ) , 2 ) + Math . pow ( ( x1 * z2 - x2 * z1 ) , 2 ) + Math . pow ( ( x1 * y2 - x2 * y1 ) , 2 ) ; float area = ( float ) Math . sqrt ( a ) ; area = area / 2 ; return area ; } public static void main ( String arr [ ] ) { int x1 = - 2 ; int y1 = 0 ; int z1 = - 5 ; int x2 = 1 ; int y2 = - 2 ; int z2 = - 1 ; float a = area ( x1 , y1 , z1 , x2 , y2 , z2 ) ; System . out . println ( " Area = ▁ " + a ) ; } }
Largest trapezoid that can be inscribed in a semicircle | Java Program to find the biggest trapezoid which can be inscribed within the semicircle ; Function to find the area of the biggest trapezoid ; the radius cannot be negative ; area of the trapezoid ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static float trapezoidarea ( float r ) { if ( r < 0 ) return - 1 ; float a = ( 3 * ( float ) Math . sqrt ( 3 ) * ( float ) Math . pow ( r , 2 ) ) / 4 ; return a ; } public static void main ( String args [ ] ) { float r = 5 ; System . out . printf ( " % .3f " , trapezoidarea ( r ) ) ; } }
Largest rectangle that can be inscribed in a semicircle | Java Program to find the the biggest rectangle which can be inscribed within the semicircle ; Function to find the area of the biggest rectangle ; the radius cannot be negative ; area of the rectangle ; Driver code
class GFG { static float rectanglearea ( float r ) { if ( r < 0 ) return - 1 ; float a = r * r ; return a ; } public static void main ( String [ ] args ) { float r = 5 ; System . out . println ( ( int ) rectanglearea ( r ) ) ; } }
Maximum distinct lines passing through a single point | Java program to find maximum number of lines which can pass through a single point ; function to find maximum lines which passes through a single point ; Driver program
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int maxLines ( int n , int x1 [ ] , int y1 [ ] , int x2 [ ] , int y2 [ ] ) { Set < Double > s = new HashSet < Double > ( ) ; double slope ; for ( int i = 0 ; i < n ; ++ i ) { if ( x1 [ i ] == x2 [ i ] ) slope = Integer . MAX_VALUE ; else slope = ( y2 [ i ] - y1 [ i ] ) * 1.0 / ( x2 [ i ] - x1 [ i ] ) * 1.0 ; s . add ( slope ) ; } return s . size ( ) ; } public static void main ( String args [ ] ) { int n = 2 , x1 [ ] = { 1 , 2 } , y1 [ ] = { 1 , 2 } , x2 [ ] = { 2 , 4 } , y2 [ ] = { 2 , 10 } ; System . out . print ( maxLines ( n , x1 , y1 , x2 , y2 ) ) ; } }