codeparrot/xlcost-text-to-code huggingface dataset:
Program to find volume and surface area of pentagonal prism | Java program to find surface area and volume of the Pentagonal Prism ; function for surface area ; function for VOlume ; Driver function
import java . util . * ; class solution { static float surfaceArea ( float a , float b , float h ) { return 5 * a * b + 5 * b * h ; } static float volume ( float b , float h ) { return ( 5 * b * h ) / 2 ; } public static void main ( String arr [ ] ) { float a = 5 ; float b = 3 ; float h = 7 ; System . out . println ( " surface ▁ area = ▁ " + surfaceArea ( a , b , h ) + " , ▁ " ) ; System . out . println ( " volume = ▁ " + volume ( b , h ) ) ; } }
Check if a point is inside , outside or on the parabola | Java Program to check if the point lies within the parabola or not ; Function to check the point ; checking the equation of parabola with the given point ; driver code
class solution { static int checkpoint ( int h , int k , int x , int y , int a ) { int p = ( int ) Math . pow ( ( y - k ) , 2 ) - 4 * a * ( x - h ) ; return p ; } public static void main ( String arr [ ] ) { int h = 0 , k = 0 , x = 2 , y = 1 , a = 4 ; if ( checkpoint ( h , k , x , y , a ) > 0 ) System . out . println ( " Outside " ) ; else if ( checkpoint ( h , k , x , y , a ) == 0 ) System . out . println ( " On ▁ the ▁ parabola " ) ; else System . out . println ( " Inside " ) ; } }
Check if a point is inside , outside or on the ellipse | Java Program to check if the point lies within the ellipse or not ; Function to check the point ; checking the equation of ellipse with the given point ; Driver code
import java . util . * ; class solution { static int checkpoint ( int h , int k , int x , int y , int a , int b ) { int p = ( ( int ) Math . pow ( ( x - h ) , 2 ) / ( int ) Math . pow ( a , 2 ) ) + ( ( int ) Math . pow ( ( y - k ) , 2 ) / ( int ) Math . pow ( b , 2 ) ) ; return p ; } public static void main ( String arr [ ] ) { int h = 0 , k = 0 , x = 2 , y = 1 , a = 4 , b = 5 ; if ( checkpoint ( h , k , x , y , a , b ) > 1 ) System . out . println ( " Outside " ) ; else if ( checkpoint ( h , k , x , y , a , b ) == 1 ) System . out . println ( " On ▁ the ▁ ellipse " ) ; else System . out . println ( " Inside " ) ; } }
Area of circle inscribed within rhombus | Java Program to find the area of the circle which can be inscribed within the rhombus ; Function to find the area of the inscribed circle ; the diagonals cannot be negative ; area of the circle ; Driver code
public class GFG { public static float circlearea ( double a , double b ) { if ( a < 0 b < 0 ) return - 1 ; float A = ( float ) ( ( 3.14 * Math . pow ( a , 2 ) * Math . pow ( b , 2 ) ) / ( 4 * ( Math . pow ( a , 2 ) + Math . pow ( b , 2 ) ) ) ) ; return A ; } public static void main ( String [ ] args ) { float a = 8 , b = 10 ; System . out . println ( circlearea ( a , b ) ) ; } }
The biggest possible circle that can be inscribed in a rectangle | Java Program to find the biggest circle which can be inscribed within the rectangle ; Function to find the area of the biggest circle ; the length and breadth cannot be negative ; area of the circle ; Driver code
class GFG { static float circlearea ( float l , float b ) { if ( l < 0 b < 0 ) return - 1 ; if ( l < b ) return ( float ) ( 3.14 * Math . pow ( l / 2 , 2 ) ) ; else return ( float ) ( 3.14 * Math . pow ( b / 2 , 2 ) ) ; } public static void main ( String [ ] args ) { float l = 4 , b = 8 ; System . out . println ( circlearea ( l , b ) ) ; } }
Centered cube number | Java Program to find nth Centered cube number ; Function to find Centered cube number ; Formula to calculate nth Centered cube number & return it into main function . ; Driver code
import java . io . * ; class GFG { static int centered_cube ( int n ) { return ( 2 * n + 1 ) * ( n * n + n + 1 ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( n + " th ▁ Centered " + " ▁ cube ▁ number : ▁ " ) ; System . out . println ( centered_cube ( n ) ) ; n = 10 ; System . out . print ( n + " th ▁ Centered " + " ▁ cube ▁ number : ▁ " ) ; System . out . println ( centered_cube ( n ) ) ; } }
Find the center of the circle using endpoints of diameter | Java program to find the center of the circle ; function to find the center of the circle ; Driver Program to test above function
class GFG { static void center ( int x1 , int x2 , int y1 , int y2 ) { System . out . print ( ( float ) ( x1 + x2 ) / 2 + " , ▁ " + ( float ) ( y1 + y2 ) / 2 ) ; } public static void main ( String arg [ ] ) { int x1 = - 9 , y1 = 3 , x2 = 5 , y2 = - 7 ; center ( x1 , x2 , y1 , y2 ) ; } }
Program to calculate volume of Octahedron | Java Program to calculate volume of Octahedron ; Driver Function ; utility Function
import java . io . * ; class GFG { public static void main ( String [ ] args ) { double side = 3 ; System . out . print ( " Volume ▁ of ▁ octahedron ▁ = ▁ " ) ; System . out . println ( vol_of_octahedron ( side ) ) ; } static double vol_of_octahedron ( double side ) { return ( ( side * side * side ) * ( Math . sqrt ( 2 ) / 3 ) ) ; } }
Program to calculate volume of Ellipsoid | Java program to find the volume of Ellipsoid . ; Function to find the volume ; Driver Code
import java . util . * ; import java . lang . * ; class GfG { public static float volumeOfEllipsoid ( float r1 , float r2 , float r3 ) { float pi = ( float ) 3.14 ; return ( float ) 1.33 * pi * r1 * r2 * r3 ; } public static void main ( String args [ ] ) { float r1 = ( float ) 2.3 , r2 = ( float ) 3.4 , r3 = ( float ) 5.7 ; System . out . println ( " volume ▁ of ▁ ellipsoid ▁ is ▁ : ▁ " + volumeOfEllipsoid ( r1 , r2 , r3 ) ) ; } }
Program to calculate Area Of Octagon | Java Program to find area of Octagon . ; utility function ; driver code
import java . io . * ; class GFG { static double areaOctagon ( double side ) { return ( float ) ( 2 * ( 1 + Math . sqrt ( 2 ) ) * side * side ) ; } public static void main ( String arg [ ] ) { double side = 4 ; System . out . print ( " Area ▁ of ▁ Regular ▁ Octagon ▁ = ▁ " + areaOctagon ( side ) ) ; } }
Program for Volume and Surface Area of Cube | Java program to find area and total surface area of cube ; utility function ; Driver code
class GFG { static double areaCube ( double a ) { return ( a * a * a ) ; } static double surfaceCube ( double a ) { return ( 6 * a * a ) ; } public static void main ( String [ ] args ) { double a = 5 ; System . out . println ( " Area ▁ = ▁ " + areaCube ( a ) ) ; System . out . println ( " Total ▁ surface ▁ area ▁ = ▁ " + surfaceCube ( a ) ) ; } }
Find mirror image of a point in 2 | Java code to find mirror image ; function which finds coordinates of mirror image . This function return a pair of double ; Driver code
class GFG { static class pair { double first , second ; public pair ( double first , double second ) { this . first = first ; this . second = second ; } } static pair mirrorImage ( double a , double b , double c , double x1 , double y1 ) { double temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) ; double x = temp * a + x1 ; double y = temp * b + y1 ; return new pair ( x , y ) ; } public static void main ( String [ ] args ) { double a = - 1.0 ; double b = 1.0 ; double c = 0.0 ; double x1 = 1.0 ; double y1 = 0.0 ; pair image = mirrorImage ( a , b , c , x1 , y1 ) ; System . out . print ( " Image ▁ of ▁ point ▁ ( " + x1 + " , ▁ " + y1 + " ) ▁ " ) ; System . out . print ( " by ▁ mirror ▁ ( " + a + " ) x ▁ + ▁ ( " + b + " ) y ▁ + ▁ ( " + c + " ) ▁ = ▁ 0 , ▁ is ▁ : " ) ; System . out . println ( " ( " + image . first + " , ▁ " + image . second + " ) " ) ; } }
Program for Point of Intersection of Two Lines | Class used to used to store the X and Y coordinates of a point respectively ; Method used to display X and Y coordinates of a point ; Line AB represented as a1x + b1y = c1 ; Line CD represented as a2x + b2y = c2 ; The lines are parallel . This is simplified by returning a pair of FLT_MAX ; Driver method ; NOTE : Further check can be applied in case of line segments . Here , we have considered AB and CD as lines
class Point { double x , y ; public Point ( double x , double y ) { this . x = x ; this . y = y ; } static void displayPoint ( Point p ) { System . out . println ( " ( " + p . x + " , ▁ " + p . y + " ) " ) ; } } class Test { static Point lineLineIntersection ( Point A , Point B , Point C , Point D ) { double a1 = B . y - A . y ; double b1 = A . x - B . x ; double c1 = a1 * ( A . x ) + b1 * ( A . y ) ; double a2 = D . y - C . y ; double b2 = C . x - D . x ; double c2 = a2 * ( C . x ) + b2 * ( C . y ) ; double determinant = a1 * b2 - a2 * b1 ; if ( determinant == 0 ) { return new Point ( Double . MAX_VALUE , Double . MAX_VALUE ) ; } else { double x = ( b2 * c1 - b1 * c2 ) / determinant ; double y = ( a1 * c2 - a2 * c1 ) / determinant ; return new Point ( x , y ) ; } } public static void main ( String args [ ] ) { Point A = new Point ( 1 , 1 ) ; Point B = new Point ( 4 , 4 ) ; Point C = new Point ( 1 , 8 ) ; Point D = new Point ( 2 , 4 ) ; Point intersection = lineLineIntersection ( A , B , C , D ) ; if ( intersection . x == Double . MAX_VALUE && intersection . y == Double . MAX_VALUE ) { System . out . println ( " The ▁ given ▁ lines ▁ AB ▁ and ▁ CD ▁ are ▁ parallel . " ) ; } else { System . out . print ( " The ▁ intersection ▁ of ▁ the ▁ given ▁ lines ▁ AB ▁ " + " and ▁ CD ▁ is : ▁ " ) ; Point . displayPoint ( intersection ) ; } } }
Minimum revolutions to move center of a circle to a target | Java program to find minimum number of revolutions to reach a target center ; Minimum revolutions to move center from ( x1 , y1 ) to ( x2 , y2 ) ; Driver Program to test above function
class GFG { static double minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) { double d = Math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ; return Math . ceil ( d / ( 2 * r ) ) ; } public static void main ( String arg [ ] ) { int r = 2 , x1 = 0 , y1 = 0 ; int x2 = 0 , y2 = 4 ; System . out . print ( ( int ) minRevolutions ( r , x1 , y1 , x2 , y2 ) ) ; } }
Find all sides of a right angled triangle from given hypotenuse and area | Set 1 | Java program to get right angle triangle , given hypotenuse and area of triangle ; limit for float comparison ; Utility method to get area of right angle triangle , given base and hypotenuse ; Prints base and height of triangle using hypotenuse and area information ; maximum area will be obtained when base and height are equal ( = sqrt ( h * h / 2 ) ) ; if given area itself is larger than maxArea then no solution is possible ; binary search for base ; get height by pythagorean rule ; Driver code to test above methods
public class GFG { final static double eps = ( double ) 1e-6 ; static double getArea ( double base , double hypotenuse ) { double height = Math . sqrt ( hypotenuse * hypotenuse - base * base ) ; return 0.5 * base * height ; } static void printRightAngleTriangle ( int hypotenuse , int area ) { int hsquare = hypotenuse * hypotenuse ; double sideForMaxArea = Math . sqrt ( hsquare / 2.0 ) ; double maxArea = getArea ( sideForMaxArea , hypotenuse ) ; if ( area > maxArea ) { System . out . print ( " Not ▁ possible " ) ; return ; } double low = 0.0 ; double high = sideForMaxArea ; double base = 0 ; while ( Math . abs ( high - low ) > eps ) { base = ( low + high ) / 2.0 ; if ( getArea ( base , hypotenuse ) >= area ) { high = base ; } else { low = base ; } } double height = Math . sqrt ( hsquare - base * base ) ; System . out . println ( Math . round ( base ) + " ▁ " + Math . round ( height ) ) ; } static public void main ( String [ ] args ) { int hypotenuse = 5 ; int area = 6 ; printRightAngleTriangle ( hypotenuse , area ) ; } }
Circle and Lattice Points | Java program to find countLattice points on a circle ; Function to count Lattice points on a circle ; Initialize result as 4 for ( r , 0 ) , ( - r . 0 ) , ( 0 , r ) and ( 0 , - r ) ; Check every value that can be potential x ; Find a potential y ; checking whether square root is an integer or not . Count increments by 4 for four different quadrant values ; Driver code
class GFG { static int countLattice ( int r ) { if ( r <= 0 ) return 0 ; int result = 4 ; for ( int x = 1 ; x < r ; x ++ ) { int ySquare = r * r - x * x ; int y = ( int ) Math . sqrt ( ySquare ) ; if ( y * y == ySquare ) result += 4 ; } return result ; } public static void main ( String arg [ ] ) { int r = 5 ; System . out . println ( countLattice ( r ) ) ; } }
Count of subarrays of size K with average at least M | Java program for the above approach ; Driver Code ; Function to count the subarrays of size K having average at least M ; Stores the resultant count of subarray ; Stores the sum of subarrays of size K ; Add the values of first K elements to the sum ; Increment the count if the current subarray is valid ; Traverse the given array ; Find the updated sum ; Check if current subarray is valid or not ; Return the count of subarrays
import java . util . * ; class GFG { public static void main ( String [ ] args ) { int [ ] arr = { 3 , 6 , 3 , 2 , 1 , 3 , 9 } ; int K = 2 , M = 4 ; System . out . println ( countSubArrays ( arr , K , M ) ) ; } public static int countSubArrays ( int [ ] arr , int K , int M ) { int count = 0 ; int sum = 0 ; for ( int i = 0 ; i < K ; i ++ ) { sum += arr [ i ] ; } if ( sum >= K * M ) count ++ ; for ( int i = K ; i < arr . length ; i ++ ) { sum += ( arr [ i ] - arr [ i - K ] ) ; if ( sum >= K * M ) count ++ ; } return count ; } }
Count of Ks in the Array for a given range of indices after array updates for Q queries | Java program for the above approach ; Function to perform all the queries ; Stores the count of 0 s ; Count the number of 0 s for query of type 1 ; Update the array element for query of type 2 ; Driver Code
import java . util . * ; class GFG { static void performQueries ( int n , int q , int k , int [ ] arr , int [ ] [ ] query ) { for ( int i = 1 ; i <= q ; i ++ ) { int count = 0 ; if ( query [ i - 1 ] [ 0 ] == 1 ) { for ( int j = query [ i - 1 ] [ 1 ] ; j <= query [ i - 1 ] [ 2 ] ; j ++ ) { if ( arr [ j ] == k ) count ++ ; } System . out . print ( count + "NEW_LINE"); } else { arr [ query [ i - 1 ] [ 1 ] ] = query [ i - 1 ] [ 2 ] ; } } } public static void main ( String [ ] args ) { int [ ] arr = { 9 , 5 , 7 , 6 , 9 , 0 , 0 , 0 , 0 , 5 , 6 , 7 , 3 , 9 , 0 , 7 , 0 , 9 , 0 } ; int Q = 5 ; int [ ] [ ] query = { { 1 , 5 , 14 } , { 2 , 6 , 1 } , { 1 , 0 , 8 } , { 2 , 13 , 0 } , { 1 , 6 , 18 } } ; int N = arr . length ; int K = 0 ; performQueries ( N , Q , K , arr , query ) ; } }
Minimum count of consecutive integers till N whose bitwise AND is 0 with N | Java program for the above approach ; Function to count the minimum count of integers such that bitwise AND of that many consecutive elements is equal to 0 ; Stores the binary representation of N ; Stores the MSB bit ; Stores the count of numbers ; Return res ; Driver Code ; Given Input ; Function Call
class GFG { static int count ( int N ) { String a = Integer . toBinaryString ( N ) ; int m = a . length ( ) - 1 ; int res = ( int ) ( N - ( Math . pow ( 2 , m ) - 1 ) ) ; return res ; } public static void main ( String [ ] args ) { int N = 18 ; System . out . println ( count ( N ) ) ; } }
Find smallest value of K such that bitwise AND of numbers in range [ N , N | Java program for the above approach ; Function is to find the largest no which gives the sequence n & ( n - 1 ) & ( n - 2 ) & ... . . & ( n - k ) = 0. ; Since , we need the largest no , we start from n itself , till 0 ; Driver Code
import java . io . * ; class GFG { public static int findSmallestNumK ( int n ) { int cummAnd = n ; int i = n - 1 ; while ( cummAnd != 0 ) { cummAnd = cummAnd & i ; if ( cummAnd == 0 ) { return i ; } i -- ; } return - 1 ; } public static void main ( String [ ] args ) { int N = 17 ; int lastNum = findSmallestNumK ( N ) ; int K = lastNum == - 1 ? lastNum : N - lastNum ; System . out . println ( K ) ; } }
Program to find the value of P ( N + r ) for a polynomial of a degree N such that P ( i ) = 1 for 1 Ã ¢ â €°¤ i Ã ¢ â €°¤ N and P ( N + 1 ) = a | Java program for the above approach ; Function to calculate factorial of N ; Base Case ; Otherwise , recursively calculate the factorial ; Function to find the value of P ( n + r ) for polynomial P ( X ) ; Stores the value of k ; Store the required answer ; Iterate in the range [ 1 , N ] and multiply ( n + r - i ) with answer ; Add the constant value C as 1 ; Return the result ; Driver Code
import java . util . * ; class GFG { static int fact ( int n ) { if ( n == 1 n == 0 ) return 1 ; else return n * fact ( n - 1 ) ; } static int findValue ( int n , int r , int a ) { int k = ( a - 1 ) / fact ( n ) ; int answer = k ; for ( int i = 1 ; i < n + 1 ; i ++ ) answer = answer * ( n + r - i ) ; answer = answer + 1 ; return answer ; } public static void main ( String args [ ] ) { int N = 1 ; int A = 2 ; int R = 3 ; System . out . print ( findValue ( N , R , A ) ) ; } }
Find winner in game of N balls , in which a player can remove any balls in range [ A , B ] in a single move | Java Program for the above approach ; Function to find the winner of the game ; Stores sum of A and B ; If N is of the form m * ( A + B ) + y ; Otherwise , ; Input ; Function call
import java . io . * ; class GFG { public static String NimGame ( int N , int A , int B ) { int sum = A + B ; if ( N % sum <= A - 1 ) return " Bob " ; else return " Alice " ; } public static void main ( String [ ] args ) { int N = 3 , A = 1 , B = 2 ; System . out . println ( NimGame ( N , A , B ) ) ; } }
Find Nth number in a sequence which is not a multiple of a given number | Java program for the above approach ; Function to find Nth number not a multiple of A in range [ L , R ] ; Calculate the Nth no ; Check for the edge case ; Driver Code ; Input parameters ; Function Call
import java . io . * ; class GFG { static void countNo ( int A , int N , int L , int R ) { int ans = L - 1 + N + ( int ) Math . floor ( ( N - 1 ) / ( A - 1 ) ) ; if ( ans % A == 0 ) { ans = ans + 1 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int A = 5 , N = 10 , L = 4 , R = 20 ; countNo ( A , N , L , R ) ; } }
Count of paths in given Binary Tree with odd bitwise AND for Q queries | Java implementation to count paths in Binary Tree with odd bitwise AND ; Function to count number of paths in binary tree such that bitwise AND of all nodes is Odd ; dp to store the count of bitwise odd paths till that vertex ; Precomputing for each value ; Check for odd value ; Number of odd elements will be + 1 till the parent node ; For even case ; Since node is even Number of odd elements will be 0 ; Even value node will not contribute in answer hence dp [ i ] = previous answer ; Printing the answer for each query ; Driver code ; To store queries
class GFG { static void compute ( int [ ] query ) { int [ ] v = new int [ 100001 ] ; int [ ] dp = new int [ 100001 ] ; v [ 1 ] = 1 ; v [ 2 ] = 0 ; dp [ 1 ] = 0 ; dp [ 2 ] = 0 ; for ( int i = 3 ; i < 100001 ; i ++ ) { if ( i % 2 != 0 ) { if ( ( i / 2 ) % 2 == 0 ) { v [ i ] = 1 ; dp [ i ] = dp [ i - 1 ] ; } else { v [ i ] = v [ i / 2 ] + 1 ; dp [ i ] = dp [ i - 1 ] + v [ i ] - 1 ; } } else { v [ i ] = 0 ; dp [ i ] = dp [ i - 1 ] ; } } for ( int x : query ) System . out . print ( dp [ x ] + "NEW_LINE"); } public static void main ( String [ ] args ) { int [ ] query = { 5 , 2 } ; compute ( query ) ; } }
Number of cycles formed by joining vertices of n sided polygon at the center | Java program for the above approach ; Function to calculate number of cycles ; BigInteger is used here if N = 10 ^ 9 then multiply will result into value greater than 10 ^ 18 ; BigInteger multiply function ; Return the final result ; Driver Code ; Given N ; Function Call
import java . util . * ; import java . math . * ; class GFG { static BigInteger findCycles ( int N ) { BigInteger res , finalResult ; long val = 2 * N - 1 ; String st = String . valueOf ( val ) ; BigInteger str = new BigInteger ( st ) ; String n1 = String . valueOf ( ( N - 1 ) ) ; String n2 = String . valueOf ( ( N - 2 ) ) ; BigInteger a = new BigInteger ( n1 ) ; BigInteger b = new BigInteger ( n2 ) ; res = a . multiply ( b ) ; finalResult = res . add ( str ) ; return finalResult ; } public static void main ( String args [ ] ) throws Exception { int N = 5 ; System . out . println ( findCycles ( N ) ) ; } }
Split a given array into K subarrays minimizing the difference between their maximum and minimum | Java program for the above approach ; Function to find the subarray ; Add the difference to vectors ; Sort vector to find minimum k ; Initialize result ; Adding first k - 1 values ; Return the minimized sum ; Driver Code ; Given array arr [ ] ; Given K ; Function Call
import java . util . * ; class GFG { static int find ( int a [ ] , int n , int k ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 1 ; i < n ; ++ i ) { v . add ( a [ i - 1 ] - a [ i ] ) ; } Collections . sort ( v ) ; int res = a [ n - 1 ] - a [ 0 ] ; for ( int i = 0 ; i < k - 1 ; ++ i ) { res += v . get ( i ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 15 , 16 , 23 , 42 } ; int N = arr . length ; int K = 3 ; System . out . print ( find ( arr , N , K ) + "NEW_LINE"); } }
Last digit of a number raised to last digit of N factorial | Java program for the above approach ; Function to find a ^ b using binary exponentiation ; Initialise result ; If b is odd then , multiply result by a ; b must be even now Change b to b / 2 ; Change a = a ^ 2 ; Function to find the last digit of the given equation ; To store cyclicity ; Store cyclicity from 1 - 10 ; Observation 1 ; Observation 3 ; To store the last digits of factorial 2 , 3 , and 4 ; Find the last digit of X ; Step 1 ; Divide a [ N ] by cyclicity of v ; If remainder is 0 ; Step 1.1 ; Step 1.2 ; Step 1.3 ; If r is non - zero , then return ( l ^ r ) % 10 ; Else return 0 ; Else return 1 ; Driver 's Code ; Given Numbers ; Function Call ; Print the result
import java . util . * ; class TestClass { public static long power ( long a , long b , long c ) { long result = 1 ; while ( b > 0 ) { if ( ( b & 1 ) = = 1 ) { result = ( result * a ) % c ; } b / = 2 ; a = ( a * a ) % c ; } return result ; } public static long calculate ( long X , long N ) { int a [ ] = new int [ 10 ] ; int cyclicity [ ] = new int [ 11 ] ; cyclicity [ 1 ] = 1 ; cyclicity [ 2 ] = 4 ; cyclicity [ 3 ] = 4 ; cyclicity [ 4 ] = 2 ; cyclicity [ 5 ] = 1 ; cyclicity [ 6 ] = 1 ; cyclicity [ 7 ] = 4 ; cyclicity [ 8 ] = 4 ; cyclicity [ 9 ] = 2 ; cyclicity [ 10 ] = 1 ; if ( N = = 0 N = = 1 ) { return ( X % 10 ) ; } else if ( N = = 2 N = = 3 N = = 4 ) { long temp = ( long ) 1e18 ; a [ 2 ] = 2 ; a [ 3 ] = 6 ; a [ 4 ] = 4 ; long v = X % 10 ; if ( v ! = 0 ) { int u = cyclicity [ ( int ) v ] ; int r = a [ ( int ) N ] % u ; if ( r = = 0 ) { if ( v = = 2 v = = 4 v = = 6 v = = 8 ) { return 6 ; } else if ( v = = 5 ) { return 5 ; } else if ( v = = 1 v = = 3 v = = 7 v = = 9 ) { return 1 ; } } else { return ( power ( v , r , temp ) % 10 ) ; } } else { return 0 ; } } return 1 ; } public static void main ( String args [ ] ) throws Exception { int X = 18 ; int N = 4 ; long result = calculate ( X , N ) ; System . out . println ( result ) ; } }
Maximize 3 rd element sum in quadruplet sets formed from given Array | Java code to Maximize 3 rd element sum in quadruplet sets formed from given Array ; Function to find the maximum possible value of Y ; pairs contain count of minimum elements that will be utilized at place of Z . it is equal to count of possible pairs that is size of array divided by 4 ; sorting the array in descending order so as to bring values with minimal difference closer to arr [ i ] ; here , i + 2 acts as a pointer that points to the third value of every possible quadruplet ; returning the optimally maximum possible value ; Driver code ; array declaration ; size of array
import java . util . * ; class GFG { static int formQuadruplets ( Integer arr [ ] , int n ) { int ans = 0 , pairs = 0 ; pairs = n / 4 ; Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < n - pairs ; i += 3 ) { ans += arr [ i + 2 ] ; } return ans ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 2 , 1 , 7 , 5 , 5 , 4 , 1 , 1 , 3 , 3 , 2 , 2 } ; int n = arr . length ; System . out . print ( formQuadruplets ( arr , n ) + "NEW_LINE"); } }