codeparrot/xlcost-text-to-code huggingface dataset:
Longest sub | Java implementation of the approach ; Function to return the length of the longest required sub - sequence ; Find the maximum element from the array ; Insert all lucas numbers below max to the set a and b are first two elements of the Lucas sequence ; If current element is a Lucas number , increment count ; Return the count ; Driver code
import java . util . * ; class GFG { static int LucasSequence ( int [ ] arr , int n ) { int max = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int counter = 0 ; HashSet < Integer > s = new HashSet < > ( ) ; int a = 2 , b = 1 ; s . add ( a ) ; s . add ( b ) ; while ( b < max ) { int c = a + b ; a = b ; b = c ; s . add ( b ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( arr [ i ] ) ) { counter ++ ; } } return counter ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 } ; int n = arr . length ; System . out . println ( LucasSequence ( arr , n ) ) ; } }
Find the number of solutions to the given equation | Java implementation of the approach ; Function to return the count of valid values of X ; Iterate through all possible sum of digits of X ; Get current value of X for sum of digits i ; Find sum of digits of cr ; If cr is a valid choice for X ; Return the count ; Driver code
class GfG { static int getCount ( int a , int b , int c ) { int count = 0 ; for ( int i = 1 ; i <= 81 ; i ++ ) { int cr = b * ( int ) Math . pow ( i , a ) + c ; int tmp = cr ; int sm = 0 ; while ( tmp != 0 ) { sm += tmp % 10 ; tmp /= 10 ; } if ( sm == i && cr < 1e9 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int a = 3 , b = 2 , c = 8 ; System . out . println ( getCount ( a , b , c ) ) ; } }
Check if an array of 1 s and 2 s can be divided into 2 parts with equal sum | Java implementation of the above approach : ; Function to check if it is possible to split the array in two parts with equal sum ; Calculate sum of elements and count of 1 's ; If total sum is odd , return False ; If sum of each part is even , return True ; If sum of each part is even but there is atleast one 1 ; Driver Code
class GFG { static boolean isSpiltPossible ( int n , int a [ ] ) { int sum = 0 , c1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; if ( a [ i ] == 1 ) { c1 ++ ; } } if ( sum % 2 != 0 ) return false ; if ( ( sum / 2 ) % 2 == 0 ) return true ; if ( c1 > 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 3 ; int a [ ] = { 1 , 1 , 2 } ; if ( isSpiltPossible ( n , a ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Sum of all Submatrices of a Given Matrix | Java program to find the sum of all possible submatrices of a given Matrix ; Function to find the sum of all possible submatrices of a given Matrix ; Varialbe to store the required sum ; Nested loop to find the number of submatrices , each number belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code
class GFG { static final int n = 3 ; static int matrixSum ( int arr [ ] [ ] ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int top_left = ( i + 1 ) * ( j + 1 ) ; int bottom_right = ( n - i ) * ( n - j ) ; sum += ( top_left * bottom_right * arr [ i ] [ j ] ) ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; System . out . println ( matrixSum ( arr ) ) ; } }
Maximum Bitwise AND pair from given range | Java implementation of the approach ; Function to return the maximum bitwise AND possible among all the possible pairs ; Maximum among all ( i , j ) pairs ; Driver code
class GFG { static int maxAND ( int L , int R ) { int maximum = L & R ; for ( int i = L ; i < R ; i ++ ) for ( int j = i + 1 ; j <= R ; j ++ ) maximum = Math . max ( maximum , ( i & j ) ) ; return maximum ; } public static void main ( String [ ] args ) { int L = 1 , R = 632 ; System . out . println ( maxAND ( L , R ) ) ; } }
Split the array into odd number of segments of odd lengths | Java to check whether given array is breakable or not ; Function to check ; Check the result by processing the first & last element and size ; Driver code
class GFG { static int checkArray ( int [ ] arr , int n ) { return ( ( arr [ 0 ] % 2 ) > 0 && ( arr [ n - 1 ] % 2 ) > 0 && ( n % 2 ) > 0 ) ? 1 : 0 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( checkArray ( arr , n ) ) ; } }
Minimum removals to make array sum odd | Java implementation of the above approach ; Function to find minimum removals ; Count odd numbers ; If the counter is odd return 0 otherwise return 1 ; Driver Code
class GfG { static int findCount ( int arr [ ] , int n ) { int countOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 1 ) countOdd ++ ; if ( countOdd % 2 == 0 ) return 1 ; else return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 5 , 1 } ; int n = arr . length ; System . out . println ( findCount ( arr , n ) ) ; } }
Check whether the number can be made perfect square after adding 1 | Java implementation of the approach ; Function that returns true if x is a perfect square ; Find floating point value of square root of x ; If square root is an integer ; Function that returns true if n is a sunny number ; If ( n + 1 ) is a perfect square ; Driver code
class GFG { static boolean isPerfectSquare ( double x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static boolean isSunnyNum ( int n ) { if ( isPerfectSquare ( n + 1 ) ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 3 ; if ( isSunnyNum ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Minimum operations to make counts of remainders same in an array | Java implementation of the approach ; Function to return the minimum number of operations required ; To store modulos values ; If it 's size greater than k  it needed to be decreased ; If it 's size is less than k  it needed to be increased ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int minOperations ( int n , int a [ ] , int m ) { int k = n / m ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] val = new Vector [ m ] ; for ( int i = 0 ; i < val . length ; i ++ ) val [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { val [ a [ i ] % m ] . add ( i ) ; } long ans = 0 ; Vector < pair > extra = new Vector < > ( ) ; for ( int i = 0 ; i < 2 * m ; ++ i ) { int cur = i % m ; while ( ( val [ cur ] . size ( ) ) > k ) { int elem = val [ cur ] . lastElement ( ) ; val [ cur ] . removeElementAt ( val [ cur ] . size ( ) - 1 ) ; extra . add ( new pair ( elem , i ) ) ; } while ( val [ cur ] . size ( ) < k && ! extra . isEmpty ( ) ) { int elem = extra . get ( extra . size ( ) - 1 ) . first ; int mmod = extra . get ( extra . size ( ) - 1 ) . second ; extra . remove ( extra . size ( ) - 1 ) ; val [ cur ] . add ( elem ) ; ans += i - mmod ; } } return ( int ) ans ; } public static void main ( String [ ] args ) { int m = 3 ; int a [ ] = { 3 , 2 , 0 , 6 , 10 , 12 } ; int n = a . length ; System . out . print ( minOperations ( n , a , m ) ) ; } }
Count primes that can be expressed as sum of two consecutive primes and 1 | Java implementation of the approach ; To check if a number is prime or not ; To store possible numbers ; Function to return all prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count all possible prime numbers that can be expressed as the sum of two consecutive primes and one ; All possible prime numbers below N ; Driver code
import java . util . * ; class GfG { static int N = 100005 ; static boolean isprime [ ] = new boolean [ N ] ; static boolean can [ ] = new boolean [ N ] ; static ArrayList < Integer > SieveOfEratosthenes ( ) { for ( int a = 0 ; a < isprime . length ; a ++ ) { isprime [ a ] = true ; } for ( int p = 2 ; p * p < N ; p ++ ) { if ( isprime [ p ] == true ) { for ( int i = p * p ; i < N ; i += p ) isprime [ i ] = false ; } } ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; for ( int i = 2 ; i < N ; i ++ ) if ( isprime [ i ] ) primes . add ( i ) ; return primes ; } static int Prime_Numbers ( int n ) { ArrayList < Integer > primes = SieveOfEratosthenes ( ) ; for ( int i = 0 ; i < ( int ) ( primes . size ( ) ) - 1 ; i ++ ) if ( primes . get ( i ) + primes . get ( i + 1 ) + 1 < N ) can [ primes . get ( i ) + primes . get ( i + 1 ) + 1 ] = true ; int ans = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( can [ i ] && isprime [ i ] == true ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int n = 50 ; System . out . println ( Prime_Numbers ( n ) ) ; } }
Sum of bitwise AND of all subarrays | Java program to find sum of bitwise AND of all subarrays ; Function to find the sum of bitwise AND of all subarrays ; variable to store the final sum ; multiplier ; variable to check if counting is on ; variable to store the length of the subarrays ; loop to find the contiguous segments ; updating the multiplier ; returning the sum ; Driver Code
class GFG { static int findAndSum ( int [ ] arr , int n ) { int sum = 0 ; int mul = 1 ; for ( int i = 0 ; i < 30 ; i ++ ) { boolean count_on = false ; int l = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) if ( count_on ) l ++ ; else { count_on = true ; l ++ ; } else if ( count_on ) { sum += ( ( mul * l * ( l + 1 ) ) / 2 ) ; count_on = false ; l = 0 ; } } if ( count_on ) { sum += ( ( mul * l * ( l + 1 ) ) / 2 ) ; count_on = false ; l = 0 ; } mul *= 2 ; } return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 1 , 1 , 5 } ; int n = arr . length ; System . out . println ( findAndSum ( arr , n ) ) ; } }
Source to destination in 2 | Java implementation of the approach ; Function that returns true if it is possible to move from source to the destination with the given moves ; Driver code
import java . io . * ; class GFG { static boolean isPossible ( int Sx , int Sy , int Dx , int Dy , int x , int y ) { if ( Math . abs ( Sx - Dx ) % x == 0 && Math . abs ( Sy - Dy ) % y == 0 && ( Math . abs ( Sx - Dx ) / x ) % 2 == ( Math . abs ( Sy - Dy ) / y ) % 2 ) return true ; return false ; } public static void main ( String [ ] args ) { int Sx = 0 , Sy = 0 , Dx = 0 , Dy = 0 ; int x = 3 , y = 4 ; if ( isPossible ( Sx , Sy , Dx , Dy , x , y ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count of pairs ( x , y ) in an array such that x < y | Java implementation of the approach ; Function to return the number of pairs ( x , y ) such that x < y ; To store the number of valid pairs ; If a valid pair is found ; Return the count of valid pairs ; Driver code
class GFG { static int getPairs ( int a [ ] ) { int count = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { for ( int j = 0 ; j < a . length ; j ++ ) { if ( a [ i ] < a [ j ] ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 4 , 3 , 1 } ; System . out . println ( getPairs ( a ) ) ; } }
Composite numbers with digit sum 1 | Java implementation of the above approach ; Function that returns true if number n is a composite number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if the eventual digit sum of number nm is 1 ; Loop till the sum is not single digit number ; Intitialize the sum as zero ; Find the sum of digits ; If sum is eventually 1 ; Function to print the required numbers from the given range ; If i is one of the required numbers ; Driver code
public class GFG { static boolean isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } static boolean isDigitSumOne ( int nm ) { while ( nm > 9 ) { int sum_digit = 0 ; while ( nm > 0 ) { int digit = nm % 10 ; sum_digit = sum_digit + digit ; nm = nm / 10 ; } nm = sum_digit ; } if ( nm == 1 ) return true ; else return false ; } static void printValidNums ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { if ( isComposite ( i ) && isDigitSumOne ( i ) ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String arg [ ] ) { int l = 10 , r = 100 ; printValidNums ( l , r ) ; } }
Determine the count of Leaf nodes in an N | Java program to find number of leaf nodes ; Function to calculate leaf nodes in n - ary tree ; Driver code
class GfG { static int calcNodes ( int N , int I ) { int result = 0 ; result = I * ( N - 1 ) + 1 ; return result ; } public static void main ( String [ ] args ) { int N = 5 , I = 2 ; System . out . println ( " Leaf ▁ nodes ▁ = ▁ " + calcNodes ( N , I ) ) ; } }
An application on Bertrand 's ballot theorem | Java implementation of the approach ; Function to calculate factorial of a number mod 1000000007 ; Factorial of i = factorial of ( i - 1 ) * i ; ; Taking mod along with calculation . ; Function for modular exponentiation ; If p is odd ; If p is even ; Function to return the count of required permutations ; Calculating multiplicative modular inverse for x ! and multiplying with ans ; Calculating multiplicative modular inverse for y ! and multiplying with ans ; Driver code ; Pre - compute factorials
class GFG { static long mod = 1000000007 ; static long [ ] arr = new long [ 1000001 ] ; static void cal_factorial ( ) { arr [ 0 ] = 1 ; for ( int i = 1 ; i <= 1000000 ; i ++ ) { arr [ i ] = ( ( arr [ i - 1 ] * i ) % mod ) ; } } static long mod_exponent ( long num , long p ) { if ( p == 0 ) return 1 ; if ( ( p & 1 ) != 0 ) { return ( ( num % mod ) * ( mod_exponent ( ( num * num ) % mod , p / 2 ) ) % mod ) % mod ; } else return ( mod_exponent ( ( num * num ) % mod , p / 2 ) ) % mod ; } static long getCount ( long x , long y ) { long ans = arr [ ( int ) x + ( int ) y - 1 ] ; ans *= mod_exponent ( arr [ ( int ) x ] , mod - 2 ) ; ans %= mod ; ans *= mod_exponent ( arr [ ( int ) y ] , mod - 2 ) ; ans %= mod ; ans *= ( x - y ) ; ans %= mod ; return ans ; } public static void main ( String [ ] args ) { cal_factorial ( ) ; long x = 3 , y = 1 ; System . out . println ( getCount ( x , y ) ) ; } }
Find the values of X and Y in the Given Equations | Java program to find the values of X and Y using the given equations ; Function to find the values of X and Y ; base condition ; required answer ; Driver Code
import java . io . * ; class GFG { static void findValues ( int a , int b ) { if ( ( a - b ) % 2 == 1 ) { System . out . println ( " - 1" ) ; return ; } System . out . println ( ( ( a - b ) / 2 ) + " ▁ " + ( ( a + b ) / 2 ) ) ; } public static void main ( String [ ] args ) { int a = 12 , b = 8 ; findValues ( a , b ) ; } }
Program to implement Inverse Interpolation using Lagrange Formula | Java code for solving inverse interpolation ; Consider a structure to keep each pair of x and y together ; Function to calculate the inverse interpolation ; Initialize final x ; Calculate each term of the given formula ; Add term to final result ; Driver Code ; Sample dataset of 4 points Here we find the value of x when y = 4.5 ; Size of dataset ; Sample y value ; Using the Inverse Interpolation function to find the value of x when y = 4.5
class GFG { static class Data { double x , y ; public Data ( double x , double y ) { super ( ) ; this . x = x ; this . y = y ; } } ; static double inv_interpolate ( Data [ ] d , int n , double y ) { double x = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { double xi = d [ i ] . x ; for ( j = 0 ; j < n ; j ++ ) { if ( j != i ) { xi = xi * ( y - d [ j ] . y ) / ( d [ i ] . y - d [ j ] . y ) ; } } x += xi ; } return x ; } public static void main ( String [ ] args ) { Data [ ] d = { new Data ( 1.27 , 2.3 ) , new Data ( 2.25 , 2.95 ) , new Data ( 2.5 , 3.5 ) , new Data ( 3.6 , 5.1 ) } ; int n = 4 ; double y = 4.5 ; System . out . printf ( " Value ▁ of ▁ x ▁ at ▁ y ▁ = ▁ 4.5 ▁ : ▁ % .5f " , inv_interpolate ( d , n , y ) ) ; } }
Count triplet pairs ( A , B , C ) of points in 2 | Java implementation of the approach ; Function to return the count of possible triplets ; Insert all the points in a set ; If the mid point exists in the set ; Return the count of valid triplets ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int countTriplets ( int n , Vector < pair > points ) { Set < pair > pts = new HashSet < pair > ( ) ; int ct = 0 ; for ( int i = 0 ; i < n ; i ++ ) pts . add ( points . get ( i ) ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int x = points . get ( i ) . first + points . get ( j ) . first ; int y = points . get ( i ) . second + points . get ( j ) . second ; if ( x % 2 == 0 && y % 2 == 0 ) if ( ! pts . contains ( new pair ( x / 2 , y / 2 ) ) ) ct ++ ; } return ct ; } public static void main ( String args [ ] ) { Vector < pair > points = new Vector < > ( ) ; points . add ( new pair ( 1 , 1 ) ) ; points . add ( new pair ( 2 , 2 ) ) ; points . add ( new pair ( 3 , 3 ) ) ; int n = points . size ( ) ; System . out . println ( countTriplets ( n , points ) ) ; } }
Concentration of juice after mixing n glasses in equal proportion | Java implementation of the approach ; Function to return the concentration of the resultant mixture ; Driver code
class GFG { static double mixtureConcentration ( int n , int [ ] p ) { double res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res += p [ i ] ; res /= n ; return res ; } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 20 , 20 } ; int n = arr . length ; System . out . println ( String . format ( " % .4f " , mixtureConcentration ( n , arr ) ) ) ; } }
Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B | Java implementation of the approach ; Function that returns true if the num contains a and b digits only ; Modular Exponentiation ; Function to return the modular inverse of x modulo MOD ; Function to return the required count of numbers ; Generating factorials of all numbers ; Generating inverse of factorials modulo MOD of all numbers ; Keeping a as largest number ; Iterate over all possible values of s and if it is a valid S then proceed further ; Check for invalid cases in the equation ; Find answer using combinatorics ; Add this result to final answer ; Driver Code
class GFG { static int MAX = ( int ) ( 1E5 + 5 ) ; static long MOD = ( long ) ( 1E9 + 7 ) ; static boolean check ( long num , long a , long b ) { while ( num > 0 ) { long rem = num % 10 ; num /= 10 ; if ( rem != a && rem != b ) return false ; } return true ; } static long power ( long x , long y ) { long ans = 1 ; while ( y > 0 ) { if ( ( y & 1 ) > 0 ) ans = ( ans * x ) % MOD ; y >>= 1 ; x = ( x * x ) % MOD ; } return ans % MOD ; } static long modInverse ( long x ) { return power ( x , MOD - 2 ) ; } static long countNumbers ( long n , long a , long b ) { long [ ] fact = new long [ MAX ] ; long [ ] inv = new long [ MAX ] ; long ans = 0 ; fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAX ; i ++ ) { fact [ i ] = ( 1 * fact [ i - 1 ] * i ) ; fact [ i ] %= MOD ; } inv [ MAX - 1 ] = modInverse ( fact [ MAX - 1 ] ) ; for ( int i = MAX - 2 ; i >= 0 ; i -- ) { inv [ i ] = ( inv [ i + 1 ] * ( i + 1 ) ) ; inv [ i ] %= MOD ; } if ( a < b ) { long x = a ; a = b ; b = x ; } for ( long s = n ; s <= 9 * n ; s ++ ) { if ( ! check ( s , a , b ) ) continue ; if ( s < n * b || ( s - n * b ) % ( a - b ) != 0 ) continue ; int numDig = ( int ) ( ( s - n * b ) / ( a - b ) ) ; if ( numDig > n ) continue ; long curr = fact [ ( int ) n ] ; curr = ( curr * inv [ numDig ] ) % MOD ; curr = ( curr * inv [ ( int ) n - numDig ] ) % MOD ; ans = ( ans + curr ) % MOD ; } return ans ; } public static void main ( String [ ] args ) { long n = 3 , a = 1 , b = 3 ; System . out . println ( countNumbers ( n , a , b ) ) ; } }
Number of elements with even factors in the given range | Java implementation of the above approach ; Function to count the perfect squares ; Driver code
import java . io . * ; class GFG { static int countOddSquares ( int n , int m ) { return ( int ) Math . pow ( m , 0.5 ) - ( int ) Math . pow ( n - 1 , 0.5 ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 100 ; System . out . println ( " Count ▁ is ▁ " + ( ( m - n + 1 ) - countOddSquares ( n , m ) ) ) ; } }
Total position where king can reach on a chessboard in exactly M moves | Java implementation of above approach ; Function to return the number of squares that the king can reach in the given number of moves ; Calculate initial and final coordinates ; Since chessboard is of size 8 X8 so if any coordinate is less than 1 or greater than 8 make it 1 or 8. ; Calculate total positions ; Driver code
class GFG { static int Square ( int row , int column , int moves ) { int a = 0 , b = 0 , c = 0 , d = 0 , total = 0 ; a = row - moves ; b = row + moves ; c = column - moves ; d = column + moves ; if ( a < 1 ) a = 1 ; if ( c < 1 ) c = 1 ; if ( b > 8 ) b = 8 ; if ( d > 8 ) d = 8 ; total = ( b - a + 1 ) * ( d - c + 1 ) - 1 ; return total ; } public static void main ( String [ ] args ) { int R = 4 , C = 5 , M = 2 ; System . out . println ( Square ( R , C , M ) ) ; } }
Find M | Java program to Find m - th number whose sum of digits of a number until sum becomes single digit is N ; Function to find the M - th number whosesum till one digit is N ; Driver Code
class GFG { static int findNumber ( int n , int m ) { int num = ( m - 1 ) * 9 + n ; return num ; } public static void main ( String args [ ] ) { int n = 2 , m = 5 ; System . out . print ( findNumber ( n , m ) ) ; } }
Check if a number can be represented as a sum of 2 triangular numbers | Java implementation of the approach ; Function to check if it is possible or not ; Store all triangular numbers up to N in a Set ; Check if a pair exists ; Driver code
import java . util . * ; class GFG { static boolean checkTriangularSumRepresentation ( int n ) { HashSet < Integer > tri = new HashSet < > ( ) ; int i = 1 ; while ( true ) { int x = i * ( i + 1 ) / 2 ; if ( x >= n ) { break ; } tri . add ( x ) ; i ++ ; } for ( Integer tm : tri ) { if ( tri . contains ( n - tm ) && ( n - tm ) != ( int ) tri . toArray ( ) [ tri . size ( ) - 1 ] ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int n = 24 ; if ( checkTriangularSumRepresentation ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Absolute difference between the first X and last X Digits of N | Java implementation of the above approach ; Function to find the number of digits in the integer ; Function to find the absolute difference ; Store the last x digits in last ; Count the no . of digits in N ; Remove the digits except the first x ; Store the first x digits in first ; Return the absolute difference between the first and last ; Driver code
import java . util . * ; class GFG { static int digitsCount ( int n ) { int len = 0 ; while ( n > 0 ) { len ++ ; n /= 10 ; } return len ; } static int absoluteFirstLast ( int n , int x ) { int i = 0 , mod = 1 ; while ( i < x ) { mod *= 10 ; i ++ ; } int last = n % mod ; int len = digitsCount ( n ) ; while ( len != x ) { n /= 10 ; len -- ; } int first = n ; return Math . abs ( first - last ) ; } public static void main ( String args [ ] ) { int n = 21546 , x = 2 ; System . out . println ( absoluteFirstLast ( n , x ) ) ; } }