codeparrot/xlcost-text-to-code huggingface dataset:
Generate a random permutation of elements from range [ L , R ] ( Divide and Conquer ) | Java implementation of the approach ; To store the random permutation ; Utility function to print the generated permutation ; Function to return a random number between x and y ; Recursive function to generate the random permutation ; Base condition ; Random number returned from the function ; Inserting random number in vector ; Recursion call for [ l , n - 1 ] ; Recursion call for [ n + 1 , r ] ; Driver code ; Generate permutation ; Print the generated permutation
import java . util . Vector ; class GFG { static Vector < Integer > permutation = new Vector < > ( ) ; static void printPermutation ( ) { permutation . stream ( ) . forEach ( ( i ) -> { System . out . print ( i + " ▁ " ) ; } ) ; } static int give_random_number ( int l , int r ) { int x = ( int ) ( Math . random ( ) % ( r - l + 1 ) + l ) ; return x ; } static void generate_random_permutation ( int l , int r ) { if ( l > r ) return ; int n = give_random_number ( l , r ) ; permutation . add ( n ) ; generate_random_permutation ( l , n - 1 ) ; generate_random_permutation ( n + 1 , r ) ; } public static void main ( String [ ] args ) { int l = 5 ; int r = 15 ; generate_random_permutation ( l , r ) ; printPermutation ( ) ; } }
Minimum number N such that total set bits of all numbers from 1 to N is at | Java implementation of the above approach ; Function to count sum of set bits of all numbers till N ; Function to find the minimum number ; Binary search for the lowest number ; Find mid number ; Check if it is atleast x ; Driver Code
import java . util . * ; class solution { static int INF = 99999 ; static int size = 10 ; static int getSetBitsFromOneToN ( int N ) { int two = 2 , ans = 0 ; int n = N ; while ( n != 0 ) { ans += ( N / two ) * ( two >> 1 ) ; if ( ( N & ( two - 1 ) ) > ( two >> 1 ) - 1 ) ans += ( N & ( two - 1 ) ) - ( two >> 1 ) + 1 ; two <<= 1 ; n >>= 1 ; } return ans ; } static int findMinimum ( int x ) { int low = 0 , high = 100000 ; int ans = high ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( getSetBitsFromOneToN ( mid ) >= x ) { ans = Math . min ( ans , mid ) ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } public static void main ( String args [ ] ) { int x = 20 ; System . out . println ( findMinimum ( x ) ) ; } }
Numbers whose factorials end with n zeros | Binary search based Java program to find numbers with n trailing zeros . ; Function to calculate trailing zeros ; range of numbers ; binary search for first number with n trailing zeros ; Print all numbers after low with n trailing zeros . ; Print result ; Driver code
import java . io . * ; class GFG { static int trailingZeroes ( int n ) { int cnt = 0 ; while ( n > 0 ) { n /= 5 ; cnt += n ; } return cnt ; } static void binarySearch ( int n ) { int low = 0 ; int high = 1000000 ; while ( low < high ) { int mid = ( low + high ) / 2 ; int count = trailingZeroes ( mid ) ; if ( count < n ) low = mid + 1 ; else high = mid ; } int result [ ] = new int [ 1000 ] ; int k = 0 ; while ( trailingZeroes ( low ) == n ) { result [ k ] = low ; k ++ ; low ++ ; } for ( int i = 0 ; i < k ; i ++ ) System . out . print ( result [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 3 ; binarySearch ( n ) ; } }
Number of days after which tank will become empty | Java code to find number of days after which tank will become empty ; Utility method to get sum of first n numbers ; Method returns minimum number of days after which tank will become empty ; if water filling is more than capacity then after C days only tank will become empty ; initialize binary search variable ; loop until low is less than high ; if cumulate sum is greater than ( C - l ) then search on left side ; if ( C - l ) is more then search on right side ; final answer will be obtained by adding l to binary search result ; Driver code to test above methods
public class Tank_Empty { static int getCumulateSum ( int n ) { return ( n * ( n + 1 ) ) / 2 ; } static int minDaysToEmpty ( int C , int l ) { if ( C <= l ) return C ; int lo = 0 ; int hi = ( int ) 1e4 ; int mid ; while ( lo < hi ) { mid = ( lo + hi ) / 2 ; if ( getCumulateSum ( mid ) >= ( C - l ) ) hi = mid ; else lo = mid + 1 ; } return ( l + lo ) ; } public static void main ( String args [ ] ) { int C = 5 ; int l = 2 ; System . out . println ( minDaysToEmpty ( C , l ) ) ; } }
Number of days after which tank will become empty | Java code to find number of days after which tank will become empty ; Method returns minimum number of days after which tank will become empty ; Driver code
import java . lang . * ; class GFG { static int minDaysToEmpty ( int C , int l ) { if ( l >= C ) return C ; double eq_root = ( Math . sqrt ( 1 + 8 * ( C - l ) ) - 1 ) / 2 ; return ( int ) ( Math . ceil ( eq_root ) + l ) ; } public static void main ( String [ ] args ) { System . out . println ( minDaysToEmpty ( 5 , 2 ) ) ; System . out . println ( minDaysToEmpty ( 6514683 , 4965 ) ) ; } }
K | Java Program to find kth element from two sorted arrays ; Driver Code
class Main { static int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) { int [ ] sorted1 = new int [ m + n ] ; int i = 0 , j = 0 , d = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; else sorted1 [ d ++ ] = arr2 [ j ++ ] ; } while ( i < m ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; while ( j < n ) sorted1 [ d ++ ] = arr2 [ j ++ ] ; return sorted1 [ k - 1 ] ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; System . out . print ( kth ( arr1 , arr2 , 5 , 4 , k ) ) ; } }
K | Java Program to find kth element from two sorted arrays ; In case we have reached end of array 1 ; In case we have reached end of array 2 ; k should never reach 0 or exceed sizes of arrays ; Compare first elements of arrays and return ; Size of array 1 is less than k / 2 ; Last element of array 1 is not kth We can directly return the ( k - m ) th element in array 2 ; Size of array 2 is less than k / 2 ; Normal comparison , move starting index of one array k / 2 to the right ; Driver code
class GFG { static int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k , int st1 , int st2 ) { if ( st1 == m ) { return arr2 [ st2 + k - 1 ] ; } if ( st2 == n ) { return arr1 [ st1 + k - 1 ] ; } if ( k == 0 || k > ( m - st1 ) + ( n - st2 ) ) { return - 1 ; } if ( k == 1 ) { return ( arr1 [ st1 ] < arr2 [ st2 ] ) ? arr1 [ st1 ] : arr2 [ st2 ] ; } int curr = k / 2 ; if ( curr - 1 >= m - st1 ) { if ( arr1 [ m - 1 ] < arr2 [ st2 + curr - 1 ] ) { return arr2 [ st2 + ( k - ( m - st1 ) - 1 ) ] ; } else { return kth ( arr1 , arr2 , m , n , k - curr , st1 , st2 + curr ) ; } } if ( curr - 1 >= n - st2 ) { if ( arr2 [ n - 1 ] < arr1 [ st1 + curr - 1 ] ) { return arr1 [ st1 + ( k - ( n - st2 ) - 1 ) ] ; } else { return kth ( arr1 , arr2 , m , n , k - curr , st1 + curr , st2 ) ; } } else if ( arr1 [ curr + st1 - 1 ] < arr2 [ curr + st2 - 1 ] ) { return kth ( arr1 , arr2 , m , n , k - curr , st1 + curr , st2 ) ; } else { return kth ( arr1 , arr2 , m , n , k - curr , st1 , st2 + curr ) ; } } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; int st1 = 0 , st2 = 0 ; System . out . println ( kth ( arr1 , arr2 , 5 , 4 , k , st1 , st2 ) ) ; } }
K | Java Program to find kth element from two sorted arrays Time Complexity : O ( log k ) ; let m > n ; Check if arr1 is empty returning k - th element of arr2 ; Check if k = 1 return minimum of first two elements of both arrays ; Now the divide and conquer part ; Now we need to find only k - j th element since we have found out the lowest j ; Now we need to find only k - i th element since we have found out the lowest i ; Driver code
import java . util . Arrays ; class Gfg { static int kth ( int arr1 [ ] , int m , int arr2 [ ] , int n , int k ) { if ( k > ( m + n ) k < 1 ) return - 1 ; if ( m > n ) return kth ( arr2 , n , arr1 , m , k ) ; if ( m == 0 ) return arr2 [ k - 1 ] ; if ( k == 1 ) return Math . min ( arr1 [ 0 ] , arr2 [ 0 ] ) ; int i = Math . min ( m , k / 2 ) ; int j = Math . min ( n , k / 2 ) ; if ( arr1 [ i - 1 ] > arr2 [ j - 1 ] ) { int temp [ ] = Arrays . copyOfRange ( arr2 , j , n ) ; return kth ( arr1 , m , temp , n - j , k - j ) ; } int temp [ ] = Arrays . copyOfRange ( arr1 , i , m ) ; return kth ( temp , m - i , arr2 , n , k - i ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ ] = { 1 , 4 , 8 , 10 } ; int m = arr1 . length ; int n = arr2 . length ; int k = 5 ; int ans = kth ( arr1 , m , arr2 , n , k ) ; if ( ans == - 1 ) System . out . println ( " Invalid ▁ query " ) ; else System . out . println ( ans ) ; } }
K | Java Program to find kth element from two sorted arrays ; Function to find K - th min ; Declaring a min heap ; Pushing elements for array a to min - heap ; Pushing elements for array b to min - heap ; Poping - out K - 1 elements ; Driver Code
import java . util . * ; class GFG { static int kth ( int a [ ] , int b [ ] , int n , int m , int k ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { pq . offer ( a [ i ] ) ; } for ( int i = 0 ; i < m ; i ++ ) { pq . offer ( b [ i ] ) ; } while ( k -- > 1 ) { pq . remove ( ) ; } return pq . peek ( ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; System . out . print ( kth ( arr1 , arr2 , 5 , 4 , k ) ) ; } }
Search element in a sorted matrix | java implementation to search an element in a sorted matrix ; This function does Binary search for x in i - th row . It does the search from mat [ i ] [ j_low ] to mat [ i ] [ j_high ] ; Element found ; element not found ; Function to perform binary search on the mid values of row to get the desired pair of rows where the element can be found ; Single row matrix ; Do binary search in middle column . Condition to terminate the loop when the 2 desired rows are found ; element found ; If element is present on the mid of the two rows ; search element on 1 st half of 1 st row ; Search element on 2 nd half of 1 st row ; Search element on 1 st half of 2 nd row ; search element on 2 nd half of 2 nd row ; Driver program
import java . io . * ; class GFG { static int MAX = 100 ; static void binarySearch ( int mat [ ] [ ] , int i , int j_low , int j_high , int x ) { while ( j_low <= j_high ) { int j_mid = ( j_low + j_high ) / 2 ; if ( mat [ i ] [ j_mid ] == x ) { System . out . println ( " Found ▁ at ▁ ( " + i + " , ▁ " + j_mid + " ) " ) ; return ; } else if ( mat [ i ] [ j_mid ] > x ) j_high = j_mid - 1 ; else j_low = j_mid + 1 ; } System . out . println ( " Element ▁ no ▁ found " ) ; } static void sortedMatrixSearch ( int mat [ ] [ ] , int n , int m , int x ) { if ( n == 1 ) { binarySearch ( mat , 0 , 0 , m - 1 , x ) ; return ; } int i_low = 0 ; int i_high = n - 1 ; int j_mid = m / 2 ; while ( ( i_low + 1 ) < i_high ) { int i_mid = ( i_low + i_high ) / 2 ; if ( mat [ i_mid ] [ j_mid ] == x ) { System . out . println ( " Found ▁ at ▁ ( " + i_mid + " , ▁ " + j_mid + " ) " ) ; return ; } else if ( mat [ i_mid ] [ j_mid ] > x ) i_high = i_mid ; else i_low = i_mid ; } if ( mat [ i_low ] [ j_mid ] == x ) System . out . println ( " Found ▁ at ▁ ( " + i_low + " , " + j_mid + " ) " ) ; else if ( mat [ i_low + 1 ] [ j_mid ] == x ) System . out . println ( " Found ▁ at ▁ ( " + ( i_low + 1 ) + " , ▁ " + j_mid + " ) " ) ; else if ( x <= mat [ i_low ] [ j_mid - 1 ] ) binarySearch ( mat , i_low , 0 , j_mid - 1 , x ) ; else if ( x >= mat [ i_low ] [ j_mid + 1 ] && x <= mat [ i_low ] [ m - 1 ] ) binarySearch ( mat , i_low , j_mid + 1 , m - 1 , x ) ; else if ( x <= mat [ i_low + 1 ] [ j_mid - 1 ] ) binarySearch ( mat , i_low + 1 , 0 , j_mid - 1 , x ) ; else binarySearch ( mat , i_low + 1 , j_mid + 1 , m - 1 , x ) ; } public static void main ( String [ ] args ) { int n = 4 , m = 5 , x = 8 ; int mat [ ] [ ] = { { 0 , 6 , 8 , 9 , 11 } , { 20 , 22 , 28 , 29 , 31 } , { 36 , 38 , 50 , 61 , 63 } , { 64 , 66 , 100 , 122 , 128 } } ; sortedMatrixSearch ( mat , n , m , x ) ; } }
Minimum difference between adjacent elements of array which contain elements from each row of a matrix | Java program to find the minimum absolute difference between any of the adjacent elements of an array which is created by picking one element from each row of the matrix ; Return smallest element greater than or equal to the current element . ; Return the minimum absolute difference adjacent elements of array ; Sort each row of the matrix . ; For each matrix element ; Search smallest element in the next row which is greater than or equal to the current element ; largest element which is smaller than the current element in the next row must be just before smallest element which is greater than or equal to the current element because rows are sorted . ; Driver code
import java . util . Arrays ; class GFG { static final int R = 2 ; static final int C = 2 ; static int bsearch ( int low , int high , int n , int arr [ ] ) { int mid = ( low + high ) / 2 ; if ( low <= high ) { if ( arr [ mid ] < n ) return bsearch ( mid + 1 , high , n , arr ) ; return bsearch ( low , mid - 1 , n , arr ) ; } return low ; } static int mindiff ( int arr [ ] [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) Arrays . sort ( arr [ i ] ) ; int ans = + 2147483647 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int p = bsearch ( 0 , m - 1 , arr [ i ] [ j ] , arr [ i + 1 ] ) ; ans = Math . min ( ans , Math . abs ( arr [ i + 1 ] [ p ] - arr [ i ] [ j ] ) ) ; if ( p - 1 >= 0 ) ans = Math . min ( ans , Math . abs ( arr [ i + 1 ] [ p - 1 ] - arr [ i ] [ j ] ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int m [ ] [ ] = { { 8 , 5 } , { 6 , 8 } } ; System . out . println ( mindiff ( m , R , C ) ) ; } }
Find bitonic point in given bitonic sequence | Java program to find bitonic point in a bitonic array . ; Function to find bitonic point using binary search ; base condition to check if arr [ mid ] is bitonic point or not ; We assume that sequence is bitonic . We go to right subarray if middle point is part of increasing subsequence . Else we go to left subarray . ; Driver program
import java . io . * ; class GFG { static int binarySearch ( int arr [ ] , int left , int right ) { if ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr [ mid - 1 ] < arr [ mid ] && arr [ mid ] > arr [ mid + 1 ] ) return mid ; if ( arr [ mid ] < arr [ mid + 1 ] ) return binarySearch ( arr , mid + 1 , right ) ; else return binarySearch ( arr , left , mid - 1 ) ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 } ; int n = arr . length ; int index = binarySearch ( arr , 1 , n - 2 ) ; if ( index != - 1 ) System . out . println ( arr [ index ] ) ; } }
Find the only repeating element in a sorted array of size n | Java program to find the only repeating element in an array of size n and elements from range 1 to n - 1. ; Returns index of second appearance of a repeating element The function assumes that array elements are in range from 1 to n - 1. ; low = 0 , high = n - 1 ; ; Check if the mid element is the repeating one ; If mid element is not at its position that means the repeated element is in left ; If mid is at proper position then repeated one is in right . ; Driver method
class Test { static int findRepeatingElement ( int arr [ ] , int low , int high ) { if ( low > high ) return - 1 ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] != mid + 1 ) { if ( mid > 0 && arr [ mid ] == arr [ mid - 1 ] ) return mid ; return findRepeatingElement ( arr , low , mid - 1 ) ; } return findRepeatingElement ( arr , mid + 1 , high ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 4 , 5 } ; int index = findRepeatingElement ( arr , 0 , arr . length - 1 ) ; if ( index != - 1 ) System . out . println ( arr [ index ] ) ; } }
Find cubic root of a number | Java program to find cubic root of a number using Binary Search ; Returns the absolute value of n - mid * mid * mid ; Returns cube root of a no n ; Set start and end for binary search ; Set precision ; If error is less than e then mid is our answer so return mid ; If mid * mid * mid is greater than n set end = mid ; If mid * mid * mid is less than n set start = mid ; Driver program to test above function
import java . io . * ; class GFG { static double diff ( double n , double mid ) { if ( n > ( mid * mid * mid ) ) return ( n - ( mid * mid * mid ) ) ; else return ( ( mid * mid * mid ) - n ) ; } static double cubicRoot ( double n ) { double start = 0 , end = n ; double e = 0.0000001 ; while ( true ) { double mid = ( start + end ) / 2 ; double error = diff ( n , mid ) ; if ( error <= e ) return mid ; if ( ( mid * mid * mid ) > n ) end = mid ; else start = mid ; } } public static void main ( String [ ] args ) { double n = 3 ; System . out . println ( " Cube ▁ root ▁ of ▁ " + n + " ▁ is ▁ " + cubicRoot ( n ) ) ; } }
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 in less than O ( n ) time ; A recursive function to count number of occurrences for each element in the array without traversing the whole array ; If element at index low is equal to element at index high in the array ; increment the frequency of the element by count of elements between high and low ; Find mid and recurse for left and right subarray ; A wrapper over recursive function findFrequencyUtil ( ) . It print number of occurrences of each element in the array . ; create a empty vector to store frequencies and initialize it by 0. Size of vector is maximum value ( which is last value in sorted array ) plus 1. ; Fill the vector with frequency ; Print the frequencies ; Driver Code
import java . util . * ; class GFG { static void findFrequencyUtil ( int arr [ ] , int low , int high , int [ ] freq ) { if ( arr [ low ] == arr [ high ] ) { freq [ arr [ low ] ] += high - low + 1 ; } else { int mid = ( low + high ) / 2 ; findFrequencyUtil ( arr , low , mid , freq ) ; findFrequencyUtil ( arr , mid + 1 , high , freq ) ; } } static void findFrequency ( int arr [ ] , int n ) { int [ ] freq = new int [ arr [ n - 1 ] + 1 ] ; findFrequencyUtil ( arr , 0 , n - 1 , freq ) ; for ( int i = 0 ; i <= arr [ n - 1 ] ; i ++ ) if ( freq [ i ] != 0 ) System . out . println ( " Element ▁ " + i + " ▁ occurs ▁ " + freq [ i ] + " ▁ 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 ) ) ; Returns floor of square root of x ; Base cases ; Starting from 1 , try all numbers until i * i is greater than or equal to x . ; Driver program
class GFG { static int floorSqrt ( int x ) { if ( x == 0 x == 1 ) return x ; int i = 1 , result = 1 ; while ( result <= x ) { i ++ ; result = i * i ; } return i - 1 ; } public static void main ( String [ ] args ) { int x = 11 ; System . out . print ( floorSqrt ( x ) ) ; } }
Check if it is possible to reach the point ( X , Y ) using distances given in an array | C # program for the above approach ; Function to check if the point ( X , Y ) is reachable from ( 0 , 0 ) or not ; Find the Euclidian Distance ; Calculate the maximum distance ; Case 1. ; Case 2. ; Otherwise , check for the polygon condition for each side ; Otherwise , print Yes ; Driver Code
import java . io . * ; class GFG { static int isPossibleToReach ( int [ ] A , int N , int X , int Y ) { double distance = Math . sqrt ( ( X * X + Y * Y ) ) ; double mx = 0 ; for ( int i = 0 ; i < N ; i ++ ) { mx += ( A [ i ] ) ; } if ( mx < distance ) { System . out . print ( " NO " ) ; return 0 ; } if ( ( mx - distance ) < 0.000001 ) { System . out . print ( " YES " ) ; return 0 ; } for ( int i = 0 ; i < N ; i ++ ) { if ( distance + mx < 2 * A [ i ] ) { System . out . print ( " No " ) ; return 0 ; } } System . out . print ( " Yes " ) ; return 0 ; } public static void main ( String [ ] args ) { int [ ] A = { 2 , 5 } ; int X = 5 , Y = 4 ; int N = A . length ; isPossibleToReach ( A , N , X , Y ) ; } }
Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | Java program for the above approach ; Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Termination Condition ; Otherwise , recursively call by decrementing 3 ^ i at each step ; Driver Code
import java . util . * ; class GFG { static boolean canReach ( int X , int Y , int steps ) { if ( X == 0 && Y == 0 ) { return true ; } if ( X < 0 Y < 0 ) { return false ; } return ( canReach ( X - ( int ) Math . pow ( 3 , steps ) , Y , steps + 1 ) | canReach ( X , Y - ( int ) Math . pow ( 3 , steps ) , steps + 1 ) ) ; } public static void main ( String [ ] args ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y , 0 ) ) { System . out . print ( " YES " + "NEW_LINE"); } else System . out . print ( " NO " + "NEW_LINE"); } }
Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | Java program for the above approach ; Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Stores the number of steps performed to reach ( X , Y ) ; Value of X in base 3 ; Value of Y in base 3 ; Check if any has value 2 ; If both have value 1 ; If both have value 0 ; Otherwise , return true ; Driver Code
import java . io . * ; class GFG { static boolean canReach ( int X , int Y ) { int steps = 0 ; while ( X != 0 Y != 0 ) { int pos1 = X % 3 ; int pos2 = Y % 3 ; if ( pos1 == 2 pos2 == 2 ) { return false ; } if ( pos1 == 1 && pos2 == 1 ) { return false ; } if ( pos1 == 0 && pos2 == 0 ) { return false ; } X /= 3 ; Y /= 3 ; steps ++ ; } return true ; } public static void main ( String [ ] args ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
Program to calculate Surface Area of Ellipsoid | Java program of the above approach ; Function to find the surface area of the given Ellipsoid ; Formula to find surface area of an Ellipsoid ; Print the area ; Driver Code
import java . util . * ; class GFG { static void findArea ( double a , double b , double c ) { double area = 4 * 3.141592653 * Math . pow ( ( Math . pow ( a * b , 1.6 ) + Math . pow ( a * c , 1.6 ) + Math . pow ( b * c , 1.6 ) ) / 3 , 1 / 1.6 ) ; System . out . print ( String . format ( " % .2f " , area ) ) ; } public static void main ( String [ ] args ) { double A = 11 , B = 12 , C = 13 ; findArea ( A , B , C ) ; } }
Descartes ' Circle Theorem with implementation | package whatever do not write package name here Java implementation of the above formulae ; Function to find the fourth circle 's  when three radius are given ; Driver code ; Radius of three circles ; Calculation of r4 using formula given above
import java . io . * ; class GFG { static double findRadius ( double r1 , double r2 , double r3 ) { double r4 = ( r1 * r2 * r3 ) / ( r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * Math . sqrt ( r1 * r2 * r3 * ( r1 + r2 + r3 ) ) ) ; return r4 ; } public static void main ( String [ ] args ) { double r1 = 1 ; double r2 = 1 ; double r3 = 1 ; double r4 = findRadius ( r1 , r2 , r3 ) ; System . out . println ( " The ▁ radius ▁ of ▁ fourth ▁ circle : ▁ " + r4 ) ; } }