codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to select K array elements lying in a given range | Java program to implement the above approach ; Function to calculate factorial of all the numbers up to N ; Factorial of 0 is 1 ; Calculate factorial of all the numbers upto N ; Calculate factorial of i ; Function to find the count of ways to select at least K elements whose values in range [ L , R ] ; Stores count of ways to select at least K elements whose values in range [ L , R ] ; Stores count of numbers having value lies in the range [ L , R ] ; Traverse the array ; Checks if the array elements lie in the given range ; Update cntNum ; Stores factorial of numbers upto N ; Calculate total ways to select at least K elements whose values lies in [ L , R ] ; Update cntWays ; Driver Code
class GFG { static int [ ] calculateFactorial ( int N ) { int [ ] fact = new int [ N + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fact [ i ] = fact [ i - 1 ] * i ; } return fact ; } static int cntWaysSelection ( int arr [ ] , int N , int K , int L , int R ) { int cntWays = 0 ; int cntNum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= L && arr [ i ] <= R ) { cntNum ++ ; } } int [ ] fact = calculateFactorial ( cntNum ) ; for ( int i = K ; i <= cntNum ; i ++ ) { cntWays += fact [ cntNum ] / ( fact [ i ] * fact [ cntNum - i ] ) ; } return cntWays ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 4 , 6 , 13 , 5 , 10 } ; int N = arr . length ; int K = 3 ; int L = 4 ; int R = 10 ; System . out . print ( cntWaysSelection ( arr , N , K , L , R ) ) ; } }
Bitwise AND of all unordered pairs from a given array | Java program to implement the above approach ; Function to calculate bitwise AND of all pairs from the given array ; Stores bitwise AND of all possible pairs ; Generate all possible pairs ; Calculate bitwise AND of each pair ; Driver Code
import java . util . * ; class GFG { static int TotalAndPair ( int arr [ ] , int N ) { int totalAND = ( 1 << 30 ) - 1 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { totalAND &= arr [ i ] & arr [ j ] ; } } return totalAND ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 12 , 15 } ; int N = arr . length ; System . out . print ( TotalAndPair ( arr , N ) ) ; } }
Modify N by adding its smallest positive divisor exactly K times | Java program to implement the above approach ; Function to find the smallest divisor of N greater than 1 ; If i is a divisor of N ; If N is a prime number ; Function to find the value of N by performing the operations K times ; Iterate over the range [ 1 , K ] ; Update N ; Driver Code
class GFG { static int smallestDivisorGr1 ( int N ) { for ( int i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { return i ; } } return N ; } static int findValOfNWithOperat ( int N , int K ) { for ( int i = 1 ; i <= K ; i ++ ) { N += smallestDivisorGr1 ( N ) ; } return N ; } public static void main ( String [ ] args ) { int N = 6 , K = 4 ; System . out . print ( findValOfNWithOperat ( N , K ) ) ; } }
Sum of all possible triplet products from given ranges | Java program to implement the above approach ; Function to find the sum of all possible triplet products ( i * j * k ) ; Stores sum required sum ; Iterate over all possible values of i ; Iterate over all possible values of j ; Iterate over all possible values of k ; Stores the product of ( i * j * k ) ; Update sum ; Driver Code
import java . util . * ; class GFG { static int M = 1000000007 ; static int findTripleSum ( int A , int B , int C ) { int sum = 0 ; for ( int i = 1 ; i <= A ; i ++ ) { for ( int j = 1 ; j <= B ; j ++ ) { for ( int k = 1 ; k <= C ; k ++ ) { int prod = ( ( ( i % M ) * ( j % M ) ) % M * ( k % M ) ) % M ; sum = ( sum + prod ) % M ; } } } return sum ; } public static void main ( String args [ ] ) { int A = 10 ; int B = 100 ; int C = 1000 ; System . out . println ( findTripleSum ( A , B , C ) ) ; } }
Sum of all possible triplet products from given ranges | Java implementation to implement the above approach ; Function to find the value of power ( X , N ) % M ; Stores the value of ( X ^ N ) % M ; Calculate the value of power ( x , N ) % M ; If N is odd ; Update res ; Update x ; Update N ; Function to find modulo multiplicative inverse of X under modulo M ; Function to find the sum of all possible triplet products ( i * j * k ) ; Stores modulo multiplicative inverse of 8 ; Stores the sum of all possible values of ( i * j * k ) ; Update res ; Driver Code
import java . util . * ; class GFG { static final int M = 1000000007 ; static long power ( long x , long N ) { long res = 1 ; while ( N > 0 ) { if ( N % 2 == 1 ) { res = ( res * x ) % M ; } x = ( x * x ) % M ; N = N >> 1 ; } return res ; } static long modinv ( long X ) { return power ( X , M - 2 ) ; } static long findTripleSum ( long A , long B , long C ) { long MMI = modinv ( 8 ) ; long res = 0 ; res = ( ( ( ( A % M * ( A + 1 ) % M ) % M * ( B % M * ( B + 1 ) % M ) % M ) % M * ( C % M * ( C + 1 ) % M ) % M ) % M * MMI ) % M ; return res ; } public static void main ( String [ ] args ) { long A = 10 ; long B = 100 ; long C = 1000 ; System . out . print ( findTripleSum ( A , B , C ) ) ; } }
Maximize minimum of array generated by maximums of same indexed elements of two rows of a given Matrix | Java program for the above approach ; Function to find the maximum of minimum of array constructed from any two rows of the given matrix ; Initialize global max as INT_MIN ; Iterate through the rows ; Iterate through remaining rows ; Initialize row_min as INT_MAX ; Iterate through the column values of two rows ; Find max of two elements ; Update the row_min ; Update the global_max ; Print the global max ; Driver Code ; Given matrix mat [ ] [ ] ; Given number of rows and columns ; Function Call
import java . io . * ; import java . util . * ; class GFG { static int getMaximum ( int N , int M , int [ ] [ ] mat ) { int global_max = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int row_min = Integer . MAX_VALUE ; for ( int k = 0 ; k < M ; k ++ ) { int m = Math . max ( mat [ i ] [ k ] , mat [ j ] [ k ] ) ; row_min = Math . min ( row_min , m ) ; } global_max = Math . max ( global_max , row_min ) ; } } return global_max ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 5 , 0 , 3 , 1 , 2 } , { 1 , 8 , 9 , 1 , 3 } , { 1 , 2 , 3 , 4 , 5 } , { 9 , 1 , 0 , 3 , 7 } , { 2 , 3 , 0 , 6 , 3 } , { 6 , 4 , 1 , 7 , 0 } } ; int N = 6 , M = 5 ; System . out . println ( getMaximum ( N , M , mat ) ) ; } }
Maximize sum of MEX values of each node in an N | Java program for the above approach ; Function to create an N - ary Tree ; Traverse the edges ; Add edges ; Function to get the maximum sum of MEX values of tree rooted at 1 ; Initialize mex ; Iterate through all children of node ; Recursively find maximum sum of MEX values of each node in tree rooted at u ; Store the maximum sum of MEX of among all subtrees ; Increase the size of tree rooted at current node ; Resulting MEX for the current node of the recursive call ; Driver Code ; Given N nodes ; Given N - 1 edges ; Stores the tree ; Generates the tree ; Returns maximum sum of MEX values of each node
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void makeTree ( Vector < Integer > tree [ ] , pair edges [ ] , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int u = edges [ i ] . first ; int v = edges [ i ] . second ; tree [ u ] . add ( v ) ; } } static pair dfs ( int node , Vector < Integer > tree [ ] ) { int mex = 0 ; int size = 1 ; for ( int u : tree [ node ] ) { pair temp = dfs ( u , tree ) ; mex = Math . max ( mex , temp . first ) ; size += temp . second ; } return new pair ( mex + size , size ) ; } public static void main ( String [ ] args ) { int N = 7 ; pair edges [ ] = { new pair ( 1 , 4 ) , new pair ( 1 , 5 ) , new pair ( 5 , 2 ) , new pair ( 5 , 3 ) , new pair ( 4 , 7 ) , new pair ( 7 , 6 ) } ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] tree = new Vector [ N + 1 ] ; for ( int i = 0 ; i < tree . length ; i ++ ) tree [ i ] = new Vector < Integer > ( ) ; makeTree ( tree , edges , N ) ; System . out . print ( ( dfs ( 1 , tree ) . first ) ) ; } }
Minimum value exceeding X whose count of divisors has different parity with count of divisors of X | Java program for the above approach ; Function to count divisors of n ; Function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of X ; Divisor count of x ; Iterate from x + 1 and check for each element ; Driver Code ; Given X ; Function call
import java . util . * ; class GFG { static int divisorCount ( int n ) { int x = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == n / i ) x ++ ; else x += 2 ; } } return x ; } static int minvalue_y ( int x ) { int a = divisorCount ( x ) ; int y = x + 1 ; while ( ( a & 1 ) == ( divisorCount ( y ) & 1 ) ) y ++ ; return y ; } public static void main ( String [ ] args ) { int x = 5 ; System . out . println ( minvalue_y ( x ) ) ; } }
Minimum value exceeding X whose count of divisors has different parity with count of divisors of X | Java program for the above approach ; Function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of X ; Check if x is perfect square ; Driver Code
import java . util . * ; class GFG { static int minvalue_y ( int x ) { int n = ( int ) Math . sqrt ( x ) ; if ( n * n == x ) return x + 1 ; return ( int ) Math . pow ( n + 1 , 2 ) ; } public static void main ( String [ ] args ) { int x = 5 ; System . out . print ( minvalue_y ( x ) ) ; } }
Sum of first N natural numbers with alternate signs | Java program to implement the above approach ; Function to find the sum of first N natural numbers with alternate signs ; Stores sum of alternate sign of first N natural numbers ; If N is an even number ; Update alternateSum ; If N is an odd number ; Update alternateSum ; Driver Code
import java . util . * ; class GFG { static int alternatingSumOfFirst_N ( int N ) { int alternateSum = 0 ; if ( N % 2 == 0 ) { alternateSum = ( - N ) / 2 ; } else { alternateSum = ( N + 1 ) / 2 ; } return alternateSum ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( alternatingSumOfFirst_N ( N ) ) ; } }
Minimum value to be added to the prefix sums at each array indices to make them positive | Java program for the above approach ; Function to find minimum startValue for positive prefix sum at each index ; Store the minimum prefix sum ; Stores prefix sum at each index ; Traverse over the array ; Update the prefix sum ; Update the minValue ; Return the positive start value ; Driver Code ; Function Call
import java . util . * ; class GFG { static int minStartValue ( int [ ] nums ) { int minValue = 0 ; int sum = 0 ; for ( int n : nums ) { sum += n ; minValue = Math . min ( minValue , sum ) ; } int startValue = 1 - minValue ; return startValue ; } public static void main ( String [ ] args ) { int [ ] nums = { - 3 , 2 , - 3 , 4 , 2 } ; System . out . println ( minStartValue ( nums ) ) ; } }
Count ways to split array into two equal sum subarrays by replacing each array element to 0 once | Java program for the above approach ; Function to find number of ways to split array into 2 subarrays having equal sum by changing element to 0 once ; Stores the count of elements in prefix and suffix of array elements ; Stores the sum of array ; Traverse the array ; Increase the frequency of current element in suffix ; Stores prefix sum upto index i ; Stores sum of suffix of index i ; Stores the desired result ; Traverse the array ; Modify prefix sum ; Add arr [ i ] to prefix map ; Calculate suffix sum by subtracting prefix sum from total sum of elements ; Remove arr [ i ] from suffix map ; Store the difference between the subarrays ; Count number of ways to split the array at index i such that subarray sums are equal ; Update the final result ; Return the result ; Driver Code ; Function Call
import java . util . * ; class GFG { static int countSubArrayRemove ( int [ ] arr , int N ) { HashMap < Integer , Integer > prefix_element_count = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > suffix_element_count = new HashMap < Integer , Integer > ( ) ; int total_sum_of_elements = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { total_sum_of_elements += arr [ i ] ; if ( ! suffix_element_count . containsKey ( arr [ i ] ) ) suffix_element_count . put ( arr [ i ] , 1 ) ; else suffix_element_count . put ( arr [ i ] , suffix_element_count . get ( arr [ i ] ) + 1 ) ; } int prefix_sum = 0 ; int suffix_sum = 0 ; int count_subarray_equal_sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { prefix_sum += arr [ i ] ; if ( ! prefix_element_count . containsKey ( arr [ i ] ) ) prefix_element_count . put ( arr [ i ] , 1 ) ; else prefix_element_count . put ( arr [ i ] , prefix_element_count . get ( arr [ i ] ) + 1 ) ; suffix_sum = total_sum_of_elements - prefix_sum ; if ( ! suffix_element_count . containsKey ( arr [ i ] ) ) suffix_element_count . put ( arr [ i ] , 0 ) ; else suffix_element_count . put ( arr [ i ] , suffix_element_count . get ( arr [ i ] ) - 1 ) ; int difference = prefix_sum - suffix_sum ; int number_of_subarray_at_i_split = 0 ; if ( prefix_element_count . containsKey ( difference ) ) number_of_subarray_at_i_split = prefix_element_count . get ( difference ) ; if ( suffix_element_count . containsKey ( - difference ) ) number_of_subarray_at_i_split += suffix_element_count . get ( - difference ) ; count_subarray_equal_sum += number_of_subarray_at_i_split ; } return count_subarray_equal_sum ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 1 , 1 , 3 , 1 } ; int N = arr . length ; System . out . println ( countSubArrayRemove ( arr , N ) ) ; } }
Count set bits in Bitwise XOR of all adjacent elements upto N | Java program to implement the above approach ; Function to count of set bits in Bitwise XOR of adjacent elements up to N ; Stores count of set bits by Bitwise XOR on adjacent elements of [ 0 , N ] ; Stores all possible values on right most set bit over [ 0 , N ] ; Iterate over the range [ 0 , N ] ; Update N ; Update bit_Position ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int countXORSetBitsAdjElemRange1_N ( int N ) { int total_set_bits = 0 ; int bit_Position = 1 ; while ( N != 0 ) { total_set_bits += ( ( N + 1 ) / 2 * bit_Position ) ; N -= ( N + 1 ) / 2 ; bit_Position ++ ; } return total_set_bits ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( countXORSetBitsAdjElemRange1_N ( N ) ) ; } }
Check if a right | Java program to implement the above approach ; Function to check if N is a perfect square number or not ; If N is a non positive integer ; Stores square root of N ; Check for perfect square ; If N is not a perfect square number ; Function to check if given two sides of a triangle forms a right - angled triangle ; If the value of ( A * A + B * B ) is a perfect square number ; Update checkTriangle ; If the value of ( A * A - B * B ) is a perfect square number ; Update checkTriangle ; If the value of ( B * B - A * A ) is a perfect square number ; Update checkTriangle ; Driver Code ; If the given two sides of a triangle forms a right - angled triangle ; Otherwise
import java . io . * ; import java . util . * ; class GFG { static int checkPerfectSquare ( int N ) { if ( N <= 0 ) { return 0 ; } double sq = Math . sqrt ( N ) ; if ( Math . floor ( sq ) == Math . ceil ( sq ) ) { return 1 ; } return 0 ; } static boolean checktwoSidesareRighTriangle ( int A , int B ) { boolean checkTriangle = false ; if ( checkPerfectSquare ( A * A + B * B ) != 0 ) { checkTriangle = true ; } if ( checkPerfectSquare ( A * A - B * B ) != 0 ) { checkTriangle = true ; } if ( checkPerfectSquare ( B * B - A * A ) != 0 ) { checkTriangle = true ; } return checkTriangle ; } public static void main ( String [ ] args ) { int A = 3 , B = 4 ; if ( checktwoSidesareRighTriangle ( A , B ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Minimize increments or decrements required to make sum and product of array elements non | Java program for the above approach ; Function to find the sum of array ; Return the sum ; Function that counts the minimum operations required to make the sum and product of array non - zero ; Stores count of zero elements ; Iterate over the array to count zero elements ; Sum of elements of the array ; Print the result ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
import java . util . * ; class GFG { static int array_sum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int countOperations ( int arr [ ] , int N ) { int count_zeros = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) count_zeros ++ ; } int sum = array_sum ( arr , N ) ; if ( count_zeros != 0 ) return count_zeros ; if ( sum == 0 ) return 1 ; return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , - 1 , 0 , 0 } ; int N = arr . length ; System . out . print ( countOperations ( arr , N ) ) ; } }
Bitwise OR of all unordered pairs from a given array | Java program to implement the above approach ; Function to find the bitwise OR of all possible pairs of the array ; Stores bitwise OR of all possible pairs of arr [ ] ; Traverse the array arr [ ] ; Update totalOR ; Return bitwise OR of all possible pairs of arr [ ] ; Driver Code
import java . util . * ; class GFG { static int TotalBitwiseORPair ( int arr [ ] , int N ) { int totalOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalOR |= arr [ i ] ; } return totalOR ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 12 , 15 } ; int N = arr . length ; System . out . print ( TotalBitwiseORPair ( arr , N ) ) ; } }
Difference between lexicographical ranks of two given permutations | Java program for the above approach ; Function the print the difference between the lexicographical ranks ; Store the permutations in lexicographic order ; Intital permutation ; Initial variables ; Check permutation ; Initialize second permutation ; Check permutation ; Print difference ; Driver Code ; Given array P [ ] ; Given array Q [ ] ; Given size ; Function call
import java . util . * ; class GFG { static void findDifference ( int [ ] p , int [ ] q , int N ) { int [ ] A = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = i + 1 ; boolean IsCorrect ; int a = 1 , b = 1 ; do { IsCorrect = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != p [ i ] ) { IsCorrect = false ; break ; } } if ( IsCorrect ) break ; a ++ ; } while ( next_permutation ( A ) ) ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = i + 1 ; do { IsCorrect = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != q [ i ] ) { IsCorrect = false ; break ; } } if ( IsCorrect ) break ; b ++ ; } while ( next_permutation ( A ) ) ; System . out . print ( Math . abs ( a - b ) + "NEW_LINE"); } static boolean next_permutation ( int [ ] p ) { for ( int a = p . length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void main ( String [ ] args ) { int [ ] p = { 1 , 3 , 2 } ; int [ ] q = { 3 , 1 , 2 } ; int n = p . length ; findDifference ( p , q , n ) ; } }
Count of packets placed in each box after performing given operations | Java program for the above approach ; Function to print final array after performing aint the operations ; Initialize variables ; Traverse through aint operations ; Operation Type ; Move left ; Move right ; Pick a packet ; Drop a packet ; Exit ; Print final array ; Driver Code ; Given capacity ; Given array with initial values ; Array size ; Operations ; Number of operations ; Function call
import java . util . * ; class GFG { static void printFinalArray ( int [ ] a , int n , int [ ] operations , int p , int capacity ) { int i , curr = 0 ; boolean picked = false ; for ( i = 0 ; i < p ; i ++ ) { int s = operations [ i ] ; boolean flag = false ; switch ( s ) { case 1 : if ( curr != 0 ) curr -- ; break ; case 2 : if ( curr != n - 1 ) curr ++ ; break ; case 3 : if ( picked == false && a [ curr ] != 0 ) { picked = true ; a [ curr ] -- ; } break ; case 4 : if ( picked == true && a [ curr ] != capacity ) { picked = false ; a [ curr ] ++ ; } break ; default : flag = true ; } if ( flag == true ) break ; } for ( i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int capacity = 5 ; int a [ ] = { 2 , 5 , 2 } ; int N = a . length ; int operations [ ] = { 3 , 2 , 4 , 1 , 4 , 5 } ; int M = operations . length ; printFinalArray ( a , N , operations , M , capacity ) ; } }
Check if sum of count of digits of array elements is Prime or not | Java program for the above approach ; Function to check whether a number is prime or not ; Corner cases ; If given number is a multiple of 2 or 3 ; Function to check if sum of count of digits of all array elements is prime or not ; Initialize sum with 0 ; Traverse over the array ; Convert array element to string ; Add the count of digits to sum ; Print the result ; Drive Code ; Function call
import java . io . * ; import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static void CheckSumPrime ( int [ ] A , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { String s = Integer . toString ( A [ i ] ) ; sum += s . length ( ) ; } if ( isPrime ( sum ) == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int [ ] A = { 1 , 11 , 12 } ; int N = A . length ; CheckSumPrime ( A , N ) ; } }
Probability of obtaining Prime Numbers as product of values obtained by throwing N dices | Java program to implement the above approach ; Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the probability of obtaining a prime number as the product of N thrown dices ; Stores count of favorable outcomes ; Stores count of sample space ; Print the required probability ; Driver code
import java . util . * ; class GFG { static int power ( int x , int N ) { int res = 1 ; while ( N > 0 ) { if ( N % 2 == 1 ) { res = ( res * x ) ; } x = ( x * x ) ; N = N >> 1 ; } return res ; } static void probablityPrimeprod ( int N ) { int N_E = 3 * N ; int N_S = power ( 6 , N ) ; System . out . print ( N_E + " ▁ / ▁ " + N_S ) ; } public static void main ( String [ ] args ) { int N = 2 ; probablityPrimeprod ( N ) ; } }
Smaller palindromic number closest to N | Java program to implement the above approach ; Function to check whether a number is palindrome or not ; Stores reverse of N ; Stores the value of N ; Calculate reverse of N ; Update rev ; Update N ; Update N ; if N is equal to rev of N ; Function to find the closest smaller palindromic number to N ; Calculate closest smaller palindromic number to N ; Update N ; Driver Code
import java . util . * ; import java . lang . Math ; class GFG { static boolean checkPalindrome ( int N ) { int rev = 0 ; int temp = N ; while ( N != 0 ) { rev = rev * 10 + N % 10 ; N = N / 10 ; } N = temp ; if ( N == rev ) { return true ; } return false ; } static int closestSmallerPalindrome ( int N ) { do { N -- ; } while ( N >= 0 && ! checkPalindrome ( N ) ) ; return N ; } public static void main ( String [ ] args ) { int N = 4000 ; System . out . println ( closestSmallerPalindrome ( N ) ) ; } }