codeparrot/xlcost-text-to-code huggingface dataset:
Check whether a number can be represented by the product of two squares | Java implementation to check whether a number can be represented by the product of two squares ; Function to check if there exist two numbers product of whose squares is n ; Initialize map ; Store square value in hashmap ; Driver code
import java . util . * ; class GFG { static boolean prodSquare ( int n ) { HashMap < Float , Float > s = new HashMap < Float , Float > ( ) ; for ( int i = 2 ; i * i <= n ; ++ i ) { s . put ( ( float ) ( i * i ) , ( float ) 1 ) ; if ( s . containsKey ( ( float ) n / ( i * i ) ) ) return true ; } return false ; } public static void main ( String [ ] args ) { int n = 25 ; if ( prodSquare ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Print N distinct numbers following the given operations | Java implementation to print N distinct numbers ; Function to print the required array ; Check if number is a multiple of 4 ; Printing left half of the array ; Printing Right Half of the array ; Driver code
import java . util . * ; class GFG { static void printArr ( int n ) { if ( n % 4 == 0 ) { for ( int i = 1 ; i <= n / 2 ; i ++ ) System . out . print ( i * 2 + " ▁ " ) ; for ( int i = 1 ; i < n / 2 ; i ++ ) System . out . print ( i * 2 - 1 + " ▁ " ) ; System . out . println ( n + n / 2 - 1 ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 22 ; printArr ( n ) ; } }
Sum of product of all subsets formed by only divisors of N | Java program to find the sum of product of all the subsets formed by only divisors of N ; Function to find the sum of product of all the subsets formed by only divisors of N ; Vector to store all the divisors of n ; Loop to find out the divisors of N ; Both ' i ' and ' n / i ' are the divisors of n ; Check if ' i ' and ' n / i ' are equal or not ; Calculating the answer ; Excluding the value of the empty set ; Driver code
import java . util . * ; class GFG { static int GetSum ( int n ) { Vector < Integer > divisors = new Vector < Integer > ( ) ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { divisors . add ( i ) ; if ( i != n / i ) { divisors . add ( n / i ) ; } } } int ans = 1 ; for ( int i = 0 ; i < divisors . size ( ) ; i ++ ) { ans *= ( divisors . get ( i ) + 1 ) ; } ans = ans - 1 ; return ans ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( GetSum ( N ) ) ; } }
Length of longest Powerful number subsequence in an Array | Java program to find the length of Longest Powerful Subsequence in an Array ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; Check if only 2 ^ 1 divides n , then return false ; Check if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to find the longest subsequence which contain all powerful numbers ; Driver code
class GFG { static boolean isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } static int longestPowerfulSubsequence ( int arr [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 4 , 10 , 13 , 9 , 25 } ; int n = arr . length ; System . out . print ( longestPowerfulSubsequence ( arr , n ) + "NEW_LINE"); } }
Maximum OR value of a pair in an Array without using OR operator | Java implementation of the approach ; Function to return the maximum bitwise OR for any pair of the given array without using bitwise OR operation ; find maximum element in the array ; finding complement will set all unset bits in a number ; iterate through all other array elements to find maximum AND value ; c will give the maximum value that could be added to max_value to produce maximum OR value ; Driver code
import java . util . * ; class GFG { static int maxOR ( int arr [ ] , int n ) { int max_value = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int number_of_bits = ( int ) ( ( Math . log ( max_value ) ) ) + 2 ; int complement = ( ( 1 << number_of_bits ) - 1 ) ^ max_value ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != max_value ) { c = Math . max ( c , ( complement & arr [ i ] ) ) ; } } return ( max_value + c ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 8 , 16 } ; int n = arr . length ; System . out . print ( maxOR ( arr , n ) ) ; } }
Find the XOR of the elements in the given range [ L , R ] with the value K for a given set of queries | Java implementation to perform the XOR range updates on an array ; Function to perform the update operation on the given array ; Converting the indices to 0 indexing . ; Saving the XOR of K from the starting index in the range [ L , R ] . ; Saving the XOR of K at the ending index in the given [ L , R ] . ; Function to display the resulting array ; Finding the resultant value in the result array ; Combining the effects of the updates with the original array without changing the initial array . ; Driver code ; Query 1 ; Query 2 ; Query 3
class GFG { static void update ( int res [ ] , int L , int R , int K ) { L -= 1 ; R -= 1 ; res [ L ] ^= K ; res [ R + 1 ] ^= K ; } static void display ( int arr [ ] , int res [ ] , int n ) { int i ; for ( i = 1 ; i < n ; i ++ ) { res [ i ] = res [ i ] ^ res [ i - 1 ] ; } for ( i = 0 ; i < n ; i ++ ) { System . out . print ( ( arr [ i ] ^ res [ i ] ) + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 , 10 } ; int N = arr . length ; int res [ ] = new int [ N ] ; int L = 1 , R = 3 , K = 2 ; update ( res , L , R , K ) ; L = 2 ; R = 4 ; K = 3 ; update ( res , L , R , K ) ; display ( arr , res , N ) ; } }
Shortest subarray to be removed to make all Array elements unique | Java program to make array elements pairwise distinct by removing at most one subarray of minimum length ; Function to check if elements of Prefix and suffix of each sub array of size K are pairwise distinct or not ; Hash map to store frequencies of elements of prefix and suffix ; Variable to store number of occurrences of an element other than one ; Adding frequency of elements of suffix to hash for subarray starting from first index There is no prefix for this sub array ; Counting extra elements in current Hash map ; If there are no extra elements return true ; Check for remaining sub arrays ; First element of suffix is now part of subarray which is being removed so , check for extra elements ; Decrement frequency of first element of the suffix ; Increment frequency of last element of the prefix ; Check for extra elements ; If there are no extra elements return true ; Function for calculating minimum length of the subarray , which on removing make all elements pairwise distinct ; Possible range of length of subarray ; Binary search to find minimum ans ; Driver Code
import java . util . * ; import java . lang . * ; class GFG { static boolean check ( int a [ ] , int n , int k ) { Map < Integer , Integer > m = new HashMap < > ( ) ; int extra = 0 ; for ( int i = k ; i < n ; i ++ ) m . put ( a [ i ] , m . getOrDefault ( a [ i ] , 0 ) + 1 ) ; for ( Integer x : m . values ( ) ) extra += x - 1 ; if ( extra == 0 ) return true ; for ( int i = 1 ; i + k - 1 < n ; i ++ ) { if ( m . get ( a [ i + k - 1 ] ) > 1 ) extra -- ; m . put ( a [ i + k - 1 ] , m . get ( a [ i + k - 1 ] ) - 1 ) ; m . put ( a [ i - 1 ] , m . get ( a [ i - 1 ] ) + 1 ) ; if ( m . get ( a [ i - 1 ] ) > 1 ) extra ++ ; if ( extra == 0 ) return true ; } return false ; } static int minlength ( int a [ ] , int n ) { int lo = 0 , hi = n + 1 ; int ans = 0 ; while ( lo < hi ) { int mid = ( lo + hi ) / 2 ; if ( check ( a , n , mid ) ) { ans = mid ; hi = mid ; } else lo = mid + 1 ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 1 , 2 , 3 } ; int n = a . length ; System . out . println ( minlength ( a , n ) ) ; } }
Find N distinct integers with zero sum | Java implementation to Print N distinct numbers such that their sum is 0 ; Function to print distinct n numbers such that their sum is 0 ; Print 2 symmetric numbers ; Print a extra 0 if N is odd ; Driver code
class GFG { static void findNumbers ( int N ) { for ( int i = 1 ; i <= N / 2 ; i ++ ) { System . out . print ( i + " , ▁ " + - i + " , ▁ " ) ; } if ( N % 2 == 1 ) System . out . print ( 0 ) ; } public static void main ( String [ ] args ) { int N = 10 ; findNumbers ( N ) ; } }
Program to calculate Electricity Bill | Java implementation to calculate the electricity bill ; Function to calculate the electricity bill ; Condition to find the charges bar in which the units consumed is fall ; Driver Code
import java . util . * ; class ComputeElectricityBill { public static int calculateBill ( int units ) { if ( units <= 100 ) { return units * 10 ; } else if ( units <= 200 ) { return ( 100 * 10 ) + ( units - 100 ) * 15 ; } else if ( units <= 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( units - 200 ) * 20 ; } else if ( units > 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( 100 * 20 ) + ( units - 300 ) * 25 ; } return 0 ; } public static void main ( String args [ ] ) { int units = 250 ; System . out . println ( calculateBill ( units ) ) ; } }
Find all divisors of first N natural numbers | Java implementation to find all the divisors of the first N natural numbers ; Function to find the factors of the numbers from 1 to N ; Loop to find the factors of the first N natural numbers of the integer ; Driver Code
import java . util . * ; class GFG { static void factors ( int n ) { int i , j ; System . out . print ( "1 -->1NEW_LINE"); for ( i = 2 ; i <= n ; i ++ ) { System . out . print ( i + " ▁ - - > " ) ; for ( j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { System . out . print ( j + " , ▁ " ) ; if ( i / j != j ) System . out . print ( i / j + " , ▁ " ) ; } } System . out . print ( "NEW_LINE"); } } public static void main ( String args [ ] ) { int n = 5 ; factors ( n ) ; } }
Find all divisors of first N natural numbers | Java implementation to find the factors of first N natural numbers ; Initialize global divisor vector array of sequence container ; Calculate all divisors of number ; Function to find the factors of first n natural numbers ; Driver Code ; Function Call
import java . util . * ; class GFG { static int MAX = ( int ) 1e5 ; static Vector < Integer > [ ] divisor = new Vector [ MAX + 1 ] ; static void sieve ( ) { for ( int i = 1 ; i <= MAX ; ++ i ) { for ( int j = i ; j <= MAX ; j += i ) divisor [ j ] . add ( i ) ; } } static void findNFactors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( i + " - - > " ) ; for ( int divi : divisor [ i ] ) { System . out . print ( divi + " , ▁ " ) ; } System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int n = 5 ; for ( int i = 0 ; i < divisor . length ; i ++ ) divisor [ i ] = new Vector < Integer > ( ) ; sieve ( ) ; findNFactors ( n ) ; } }
Maximum number of prime factors a number can have with exactly x factors | Java implementation to find the maximum count of the prime factors by the count of factors of number ; Function to count number of prime factors of x ; Count variable is incremented form every prime factor of x ; Loop to count the number of the prime factors of the given number ; Driver Code
class GFG { static int countPrimeFactors ( int n ) { if ( n == 1 ) return 0 ; int cnt = 0 ; while ( n % 2 == 0 ) { cnt ++ ; n = n / 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { cnt ++ ; n = n / i ; } } if ( n > 2 ) cnt ++ ; return cnt ; } public static void main ( String [ ] args ) { int x = 8 ; int prime_factor_cnt = countPrimeFactors ( x ) ; System . out . print ( prime_factor_cnt + "NEW_LINE"); } }
Count of N digit palindromic numbers divisible by 9 | Java implementation to count the number of N digit palindromic numbers divisible by 9 ; Function to find the count of N digits palindromic numbers which are divisible by 9 ; If N is odd ; If N is even ; Driver Code
import java . util . * ; class GFG { static int countPalindromic ( int n ) { int count ; if ( n % 2 == 1 ) { count = ( int ) Math . pow ( 9 , ( n - 1 ) / 2 ) ; } else { count = ( int ) Math . pow ( 9 , ( n - 2 ) / 2 ) ; } return count ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countPalindromic ( n ) ) ; } }
Count of sub | Java program to find the count of sub - arrays with odd product ; Function that returns the count of sub - arrays with odd product ; Initialize the count variable ; Initialize variable to store the last index with even number ; Initialize variable to store count of continuous odd numbers ; Loop through the array ; Check if the number is even or not ; Calculate count of continuous odd numbers ; Increase the count of sub - arrays with odd product ; Store the index of last even number ; N considered as index of even number ; Driver Code ; Function call
class GFG { static int countSubArrayWithOddProduct ( int A [ ] , int N ) { int count = 0 ; int last = - 1 ; int K = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { K = ( i - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; last = i ; } } K = ( N - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 15 , 7 , 3 , 25 , 6 , 2 , 1 , 1 , 7 } ; int n = arr . length ; System . out . println ( countSubArrayWithOddProduct ( arr , n ) ) ; } }
Calculate the CGPA and CGPA % of marks obtained by a Student in N subjects | Java program to calculate the CGPA and CGPA percentage of a student ; Variable to store the grades in every subject ; Variables to store CGPA and the sum of all the grades ; Computing the grades ; Computing the sum of grades ; Computing the CGPA ; Driver code
import java . util . Scanner ; class CGPA { public static double CgpaCalc ( double [ ] marks , int n ) { double grade [ ] = new double [ n ] ; double cgpa , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { grade [ i ] = ( marks [ i ] / 10 ) ; } for ( int i = 0 ; i < n ; i ++ ) { sum += grade [ i ] ; } cgpa = sum / n ; return cgpa ; } public static void main ( String args [ ] ) { int n = 5 ; double [ ] marks = { 90 , 80 , 70 , 80 , 90 } ; double cgpa = CgpaCalc ( marks , n ) ; System . out . println ( " CGPA ▁ = ▁ " + cgpa ) ; System . out . println ( " CGPA ▁ Percentage ▁ = ▁ " + String . format ( " % .2f " , cgpa * 9.5 ) ) ; } }
Find the subarray of size K with minimum XOR | Java implementation to find the subarray with minimum XOR ; Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Driver Code ; Subarray size ; Function Call
class GFG { static void findMinXORSubarray ( int arr [ ] , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_xor = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; int min_xor = curr_xor ; for ( int i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } System . out . print ( min_xor + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; int k = 3 ; int n = arr . length ; findMinXORSubarray ( arr , n , k ) ; } }
Greatest number that can be formed from a pair in a given Array | Java implementation to find the greatest number from the given pairs of the array ; Function to find the greatest number formed from the pairs ; First append Y at the end of X ; Then append X at the end of Y ; Now see which of the two formed numbers is greater than other ; Function to find pairs from array ; Iterate through all pairs ; Driver code
import java . util . * ; class GFG { static String getNumber ( int a , int b ) { String X = Integer . toString ( a ) ; String Y = Integer . toString ( b ) ; String XY = X + Y ; String YX = Y + X ; return XY . compareTo ( YX ) > 0 ? XY : YX ; } static void printMaxPair ( int arr [ ] , int n ) { int largest = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int number = Integer . parseInt ( getNumber ( arr [ i ] , arr [ j ] ) ) ; largest = Math . max ( largest , number ) ; } System . out . println ( largest ) ; } public static void main ( String [ ] args ) { int a [ ] = { 23 , 14 , 16 , 25 , 3 , 9 } ; int n = a . length ; printMaxPair ( a , n ) ; } }
Check whether two numbers are in golden ratio | Java implementation to check whether two numbers are in golden ratio with each other ; Function to check that two numbers are in golden ratio ; Swapping the numbers such that A contains the maximum number between these numbers ; First Ratio ; Second Ratio ; Condition to check that two numbers are in golden ratio ; Driver code ; Function Call
class GFG { public static Boolean checkGoldenRatio ( float a , float b ) { if ( a <= b ) { float temp = a ; a = b ; b = temp ; } String ratio1 = String . format ( " % .3f " , a / b ) ; String ratio2 = String . format ( " % .3f " , ( a + b ) / a ) ; if ( ratio1 . equals ( ratio2 ) && ratio1 . equals ( "1.618" ) ) { System . out . println ( " Yes " ) ; return true ; } else { System . out . println ( " No " ) ; return false ; } } public static void main ( String [ ] args ) { float a = ( float ) 0.618 ; float b = 1 ; checkGoldenRatio ( a , b ) ; } }
Find the minimum number to be added to N to make it a power of K | Java program to find the minimum number to be added to N to make it a power of K ; Function to return the minimum number to be added to N to make it a power of K . ; Computing the difference between then next greater power of K and N ; Driver code
class GFG { static int minNum ( int n , int k ) { int x = ( int ) ( Math . log ( n ) / Math . log ( k ) ) + 1 ; int mn = ( int ) ( Math . pow ( k , x ) - n ) ; return mn ; } public static void main ( String [ ] args ) { int n = 20 , k = 5 ; System . out . print ( minNum ( n , k ) ) ; } }
Previous perfect square and cube number smaller than number N | Java implementation to find the previous perfect square and cube smaller than the given number ; Function to find the previous perfect square of the number N ; If N is already a perfect square decrease prevN by 1. ; Function to find the previous perfect cube ; If N is already a perfect cube decrease prevN by 1. ; Driver Code
import java . util . * ; class GFG { static int previousPerfectSquare ( int N ) { int prevN = ( int ) Math . floor ( Math . sqrt ( N ) ) ; if ( prevN * prevN == N ) prevN -= 1 ; return prevN * prevN ; } static int previousPerfectCube ( int N ) { int prevN = ( int ) Math . floor ( Math . cbrt ( N ) ) ; if ( prevN * prevN * prevN == N ) prevN -= 1 ; return prevN * prevN * prevN ; } public static void main ( String [ ] args ) { int n = 30 ; System . out . println ( previousPerfectSquare ( n ) ) ; System . out . println ( previousPerfectCube ( n ) ) ; } }
Find the count of even odd pairs in a given Array | Java program to count the pairs in array of the form ( even , odd ) ; Function to count the pairs in array of the form ( even , odd ) ; Variable to store count of such pairs ; Iterate through all pairs ; Increment count if condition is satisfied ; Driver code
import java . util . * ; class GFG { static int findCount ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( ( arr [ i ] % 2 == 0 ) && ( arr [ j ] % 2 == 1 ) ) { res ++ ; } return res ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 4 , 1 , 2 , 3 } ; int n = a . length ; System . out . print ( findCount ( a , n ) ) ; } }
Find the count of even odd pairs in a given Array | Java program to count the pairs in array of the form ( even , odd ) ; Function to count the pairs in array of the form ( even , odd ) ; Check if number is even or not ; Driver code
class GFG { static int findCount ( int arr [ ] , int n ) { int count = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { count ++ ; } else { ans = ans + count ; } } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 4 , 1 , 2 , 3 } ; int n = a . length ; System . out . print ( findCount ( a , n ) ) ; } }
Product of all non repeating Subarrays of an Array | Java program to find the product of all non - repeating Subarrays of an Array ; Function to find the product of all non - repeating Subarrays of an Array ; Finding the occurrence of every element ; Iterating through the array and finding the product ; We are taking the power of each element in array with the occurrence and then taking product of those . ; Driver code
public class GFG { private static long product ( int [ ] arr ) { double occurrence = Math . pow ( 2 , arr . length - 1 ) ; double product = 1 ; for ( int i = 0 ; i < arr . length ; i ++ ) { product *= Math . pow ( arr [ i ] , occurrence ) ; } return ( long ) product ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 3 , 7 } ; System . out . println ( product ( arr ) ) ; } }
Sum of all N | Java program for the above approach ; Function to find a ^ b efficiently ; Base Case ; To store the value of a ^ b ; Multiply temp by a until b is not 0 ; Return the final ans a ^ b ; Function to find sum of all N - digit palindromic number divisible by 9 ; Base Case ; If N is even , decrease ways by 1 ; Find the total number of ways ; Iterate over [ 1 , N ] and find the sum at each index ; Print the final Sum ; Driver Code
class GFG { static int MOD = 1000000007 ; static int power ( int a , int b ) { if ( b == 0 ) return 1 ; int temp = power ( a , b / 2 ) ; temp = ( temp * temp ) % MOD ; if ( b % 2 != 0 ) { temp = ( temp * a ) % MOD ; } return temp ; } static void palindromicSum ( int N ) { int sum = 0 , res , ways ; if ( N == 1 ) { System . out . print ( "9" + "NEW_LINE"); return ; } if ( N == 2 ) { System . out . print ( "99" + "NEW_LINE"); return ; } ways = N / 2 ; if ( N % 2 == 0 ) ways -- ; res = power ( 9 , ways - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { sum = sum * 10 + 45 * res ; sum %= MOD ; } System . out . print ( sum + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 3 ; palindromicSum ( N ) ; } }
Maximize array sum by concatenating corresponding elements of given two arrays | Java implementation to find the maximum array sum by concatenating corresponding elements of given two arrays ; Function to join the two numbers ; Loop to reverse the digits of the one number ; Loop to join two numbers ; Function to find the maximum array sum ; Loop to iterate over the two elements of the array ; Find the array sum ; Return the array sum ; Driver Code
class GFG { static int joinNumbers ( int numA , int numB ) { int revB = 0 ; while ( numB > 0 ) { revB = revB * 10 + ( numB % 10 ) ; numB = numB / 10 ; } while ( revB > 0 ) { numA = numA * 10 + ( revB % 10 ) ; revB = revB / 10 ; } return numA ; } static int findMaxSum ( int A [ ] , int B [ ] , int n ) { int maxArr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { int X = joinNumbers ( A [ i ] , B [ i ] ) ; int Y = joinNumbers ( B [ i ] , A [ i ] ) ; int mx = Math . max ( X , Y ) ; maxArr [ i ] = mx ; } int maxAns = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxAns += maxArr [ i ] ; } return maxAns ; } public static void main ( String args [ ] ) { int N = 5 ; int A [ ] = { 11 , 23 , 38 , 43 , 59 } ; int B [ ] = { 36 , 24 , 17 , 40 , 56 } ; System . out . println ( findMaxSum ( A , B , N ) ) ; } }