codeparrot/xlcost-text-to-code huggingface dataset:
Count of non co | Java program for the above approach ; Recursive function to return gcd of two numbers ; Function to count the number of non co - prime pairs for each query ; Traverse the array arr [ ] ; Stores the count of non co - prime pairs ; Iterate over the range [ 1 , x ] ; Iterate over the range [ x , y ] ; If gcd of current pair is greater than 1 ; Driver Code ; Given Input ; Function Call
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void countPairs ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int x = 1 ; x <= arr [ i ] ; x ++ ) { for ( int y = x ; y <= arr [ i ] ; y ++ ) { if ( gcd ( x , y ) > 1 ) count ++ ; } } System . out . print ( count + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 20 } ; int N = 3 ; countPairs ( arr , N ) ; } }
Count of non co | Java program for the above approach ; Auxiliary function to pre - compute the answer for each array ; Iterate over the range [ 1 , MAX ] ; Iterate over the range [ 1 , MAX ] ; If the number is prime ; Subtract the number of pairs which has i as one of their factors ; Iterate over the range [ 1 , MAX ] ; Function to count the number of non co - prime pairs for each query ; The i - th element stores the count of element that are co - prime with i ; Stores the resulting array ; Function Call ; Traverse the array arr [ ] ; Driver Code ; Given Input ; Function Call
import java . util . * ; class GFG { static int MAX = 1005 ; static void preCalculate ( int [ ] phi , int [ ] ans ) { phi [ 0 ] = 0 ; phi [ 1 ] = 1 ; for ( int i = 2 ; i <= MAX ; i ++ ) phi [ i ] = i ; for ( int i = 2 ; i <= MAX ; i ++ ) { if ( phi [ i ] == i ) { for ( int j = i ; j <= MAX ; j += i ) phi [ j ] -= ( phi [ j ] / i ) ; } } for ( int i = 1 ; i <= MAX ; i ++ ) ans [ i ] = ans [ i - 1 ] + ( i - phi [ i ] ) ; } static void countPairs ( int [ ] arr , int N ) { int [ ] phi = new int [ 100000 ] ; Arrays . fill ( phi , 0 ) ; int [ ] ans = new int [ 100000 ] ; Arrays . fill ( ans , 0 ) ; preCalculate ( phi , ans ) ; for ( int i = 0 ; i < N ; ++ i ) { System . out . print ( ans [ arr [ i ] ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 20 } ; int N = 3 ; countPairs ( arr , N ) ; } }
Maximum number of multiplication by 3 or division by 2 operations possible on an array | Java program for the above approach ; Function to count maximum number of multiplication by 3 or division by 2 operations that can be performed ; Stores the maximum number of operations possible ; Traverse the array arr [ ] ; Iterate until arr [ i ] is even ; Increment count by 1 ; Update arr [ i ] ; Return the value of Count as the answer ; Driver code ; Given Input Given Input ; Function Call
public class GFG { static int maximumTurns ( int arr [ ] , int N ) { int Count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { while ( arr [ i ] % 2 == 0 ) { Count ++ ; arr [ i ] = arr [ i ] / 2 ; } } return Count ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 4 } ; int M = 3 , K = 2 ; int N = arr . length ; System . out . println ( maximumTurns ( arr , N ) ) ; } }
Distribute the white and black objects into maximum groups under certain constraints | Java program for the above approach ; Function to check if it is possible to distribute W and B into maximum groups possible ; If W is greater than B , swap them ; Distribution is not possible ; Distribution is possible ; Driver code ; Input ; Function call
import java . io . * ; class GFG { public static void isPossible ( int W , int B , int D ) { if ( W > B ) { int temp = W ; W = B ; B = temp ; } if ( B > W * ( D + 1 ) ) System . out . println ( " NO " ) ; else System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { int W = 2 ; int B = 5 ; int D = 2 ; isPossible ( W , B , D ) ; } }
Find the maximum GCD possible for some pair in a given range [ L , R ] | Java program for the above approach ; Function to calculate GCD ; Function to calculate maximum GCD in a range ; Variable to store the answer ; If Z has two multiples in [ L , R ] ; Update ans ; Return the value ; Driver code ; Input ; Function Call
import java . io . * ; class GFG { public static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } public static int maxGCDInRange ( int L , int R ) { int ans = 1 ; for ( int Z = R ; Z >= 1 ; Z -- ) { if ( ( R / Z ) - ( L - 1 ) / Z > 1 ) { ans = Z ; break ; } } return ans ; } public static void main ( String [ ] args ) { int L = 102 ; int R = 139 ; System . out . println ( maxGCDInRange ( L , R ) ) ; }
Number of ways to form a number with maximum Ks in it | Java program for tha above approach ; Function to calculate number of ways a number can be formed that has the maximum number of Ks ; Convert to string ; Calculate length of subarrays that can contribute to the answer ; Count length of subarray where adjacent digits add up to K ; Current subarray can contribute to the answer only if it is odd ; Return the answer ; Driver Code ; Input ; Function call
import java . util . * ; class GFG { static int noOfWays ( int N , int K ) { String S = String . valueOf ( N ) ; int ans = 1 ; for ( int i = 1 ; i < S . length ( ) ; i ++ ) { int count = 1 ; while ( i < S . length ( ) && ( int ) S . charAt ( i ) - 48 + ( int ) S . charAt ( i - 1 ) - 48 == K ) { count ++ ; i ++ ; } if ( count % 2 == 1 ) ans *= ( count + 1 ) / 2 ; } return ans ; } public static void main ( String [ ] args ) { int N = 1454781 ; int K = 9 ; System . out . print ( noOfWays ( N , K ) ) ; } }
Minimum bit swaps between given numbers to make their Bitwise OR equal to Bitwise AND | Java program for the above approach ; Function for counting number of set bit ; Function to return the count of minimum operations required ; cnt to count the number of bits set in A and in B ; if odd numbers of total set bits ; one_zero = 1 in A and 0 in B at ith bit similarly for zero_one ; When bitpos is set in B , unset in B ; When bitpos is set in A , unset in B ; number of moves is half of number pairs of each group ; odd number pairs ; Driver Code ; Input ; Function call to compute the result
import java . io . * ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static int minOperations ( int A , int B ) { int cnt1 = 0 , cnt2 = 0 ; cnt1 += countSetBits ( A ) ; cnt2 += countSetBits ( B ) ; if ( ( cnt1 + cnt2 ) % 2 != 0 ) return - 1 ; int oneZero = 0 , zeroOne = 0 ; int ans = 0 ; for ( int i = 0 ; i < Math . max ( cnt1 , cnt2 ) ; i ++ ) { int bitpos = 1 << i ; if ( ( ( bitpos & A ) == 0 ) && ( ( bitpos & B ) != 0 ) ) zeroOne ++ ; if ( ( ( bitpos & A ) != 0 ) && ( ( bitpos & B ) == 0 ) ) oneZero ++ ; } ans = ( zeroOne / 2 ) + ( oneZero / 2 ) ; if ( zeroOne % 2 != 0 ) ans += 2 ; return ans ; } public static void main ( String args [ ] ) { int A = 27 , B = 5 ; System . out . println ( minOperations ( A , B ) ) ; } }
Largest sum subarray of size K containing consecutive elements | Java program for the above approach ; Function to find the largest sum subarray such that it contains K consecutive elements ; Stores sum of subarray having K consecutive elements ; Stores the maximum sum among all subarrays of size K having consecutive elements ; Traverse the array ; Store K elements of one subarray at a time ; Sort the duplicate array in ascending order ; Checks if elements in subarray are consecutive or not ; Traverse the k elements ; If not consecutive , break ; If flag is true update the maximum sum ; Stores the sum of elements of the current subarray ; Update the max_sum ; Reset curr_sum ; Return the result ; Driver Code
import java . util . Arrays ; class GFG { public static Integer maximumSum ( int [ ] A , int N , int K ) { int curr_sum = 0 ; int max_sum = Integer . MIN_VALUE ; for ( int i = 0 ; i < N - K + 1 ; i ++ ) { int [ ] dupl_arr = Arrays . copyOfRange ( A , i , i + K ) ; Arrays . sort ( dupl_arr ) ; Boolean flag = true ; for ( int j = 1 ; j < K ; j ++ ) { if ( dupl_arr [ j ] - dupl_arr [ j - 1 ] != 1 ) { flag = false ; break ; } } if ( flag ) { int temp = 0 ; curr_sum = 0 ; for ( int x = 0 ; x < dupl_arr . length ; x ++ ) { curr_sum += dupl_arr [ x ] ; } max_sum = Math . max ( max_sum , curr_sum ) ; curr_sum = 0 ; } } return max_sum ; } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 12 , 9 , 8 , 10 , 15 , 1 , 3 , 2 } ; int K = 3 ; int N = arr . length ; System . out . println ( maximumSum ( arr , N , K ) ) ; } }
Find the count of Smith Brothers Pairs in a given Array | Java program for the above approach ; Array to store all prime less than and equal to MAX ; Utility function for sieve of sundaram ; Main logic of Sundaram . ; Since 2 is a prime number ; Only primes are selected ; Function to check whether a number is a smith number . ; Find sum the digits of prime factors of n ; Add its digits of prime factors to pDigitSum . ; One prime factor is still to be summed up ; Now sum the digits of the original number ; Return the answer ; Function to check if X and Y are a Smith Brother Pair ; Function to find pairs from array ; Iterate through all pairs ; Increment count if there is a smith brother pair ; Driver code ; Preprocessing ; Input ; Function call
import java . util . ArrayList ; class GFG { static int MAX = 10000 ; static ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; public static void sieveSundaram ( ) { ArrayList < Boolean > marked = new ArrayList < Boolean > ( MAX / 2 + 100 ) ; for ( int i = 0 ; i < MAX / 2 + 100 ; i ++ ) { marked . add ( false ) ; } for ( int i = 1 ; i <= ( Math . sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked . set ( j , true ) ; primes . add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked . get ( i ) == false ) primes . add ( 2 * i + 1 ) ; } public static Boolean isSmith ( int n ) { int original_no = n ; int pDigitSum = 0 ; for ( int i = 0 ; primes . get ( i ) <= n / 2 ; i ++ ) { while ( n % primes . get ( i ) == 0 ) { int p = primes . get ( i ) ; n = n / p ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = p / 10 ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = n / 10 ; } } int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = original_no / 10 ; } return ( pDigitSum == sumDigits ) ; } public static Boolean isSmithBrotherPair ( int X , int Y ) { return isSmith ( X ) && isSmith ( Y ) && Math . abs ( X - Y ) == 1 ; } public static Integer countSmithBrotherPairs ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) { if ( isSmithBrotherPair ( A [ i ] , A [ j ] ) ) count ++ ; } return count ; } public static void main ( String args [ ] ) { sieveSundaram ( ) ; int A [ ] = { 728 , 729 , 28 , 2964 , 2965 } ; int N = A . length ; System . out . println ( countSmithBrotherPairs ( A , N ) ) ; } }
Find the count of Smith Brothers Pairs in a given Array | Java program for the above approach ; array to store all prime less than and equal to MAX ; utility function for sieve of sundaram ; Main logic of Sundaram . ; Since 2 is a prime number ; only primes are selected ; Function to check whether a number is a smith number . ; Find sum the digits of prime factors of n ; add its digits of prime factors to pDigitSum . ; one prime factor is still to be summed up ; Now sum the digits of the original number ; return the answer ; Function to check if X and Y are a Smith Brother Pair ; Function to find pairs from array ; Variable to store number of Smith Brothers Pairs ; sort A ; check for consecutive numbers only ; Driver Code ; Preprocessing sieve of sundaram ; Input ; Function call
import java . util . * ; class GFG { static int MAX = 10000 ; static ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; static void sieveSundaram ( ) { ArrayList < Boolean > marked = new ArrayList < Boolean > ( MAX / 2 + 100 ) ; for ( int i = 0 ; i < MAX / 2 + 100 ; i ++ ) { marked . add ( false ) ; } for ( int i = 1 ; i <= ( Math . sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked . set ( j , true ) ; primes . add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked . get ( i ) == false ) primes . add ( 2 * i + 1 ) ; } static Boolean isSmith ( int n ) { int original_no = n ; int pDigitSum = 0 ; for ( int i = 0 ; primes . get ( i ) <= n / 2 ; i ++ ) { while ( n % primes . get ( i ) == 0 ) { int p = primes . get ( i ) ; n = n / p ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = p / 10 ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = n / 10 ; } } int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = original_no / 10 ; } return ( pDigitSum == sumDigits ) ; } static Boolean isSmithBrotherPair ( int X , int Y ) { return isSmith ( X ) && isSmith ( Y ) ; } static Integer countSmithBrotherPairs ( int A [ ] , int N ) { int count = 0 ; Arrays . sort ( A ) ; for ( int i = 0 ; i < N - 2 ; i ++ ) if ( isSmithBrotherPair ( A [ i ] , A [ i + 1 ] ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { sieveSundaram ( ) ; int A [ ] = { 728 , 729 , 28 , 2964 , 2965 } ; int N = A . length ; System . out . print ( countSmithBrotherPairs ( A , N ) ) ; } }
Count pair sums that are factors of the sum of the array | Java program for the above approach ; Function to find the number of pairs whose sums divides the sum of array ; Initialize the totalSum and count as 0 ; Calculate the total sum of array ; Generate all possible pairs ; If the sum is a factor of totalSum or not ; Increment count by 1 ; Print the total count obtained ; Driver code
import java . io . * ; class GFG { public static void countPairs ( int arr [ ] , int N ) { int count = 0 , totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( totalSum % ( arr [ i ] + arr [ j ] ) == 0 ) { count += 1 ; } } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; countPairs ( arr , N ) ; } }
Print all numbers that are divisors of N and are co | Java program for tha above approach ; Function to print all numbers that are divisors of N and are co - prime with the quotient of their division ; Iterate upto square root of N ; If divisors are equal and gcd is 1 , then print only one of them ; Otherwise print both ; Driver Code
import java . util . * ; class GFG { static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static void printUnitaryDivisors ( int n ) { for ( int i = 1 ; i <= ( int ) Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i && gcd ( i , n / i ) == 1 ) { System . out . print ( i + " ▁ " ) ; } else { if ( gcd ( i , n / i ) == 1 ) { System . out . print ( i + " ▁ " + n / i + " ▁ " ) ; } } } } } public static void main ( String [ ] args ) { int N = 12 ; printUnitaryDivisors ( N ) ; } }
Maximize K to make given array Palindrome when each element is replaced by its remainder with K | Java program for the above approach ; Utility function to calculate the GCD of two numbers ; Function to calculate the largest K , replacing all elements of an array A by their modulus with K , makes A a palindromic array ; Check if A is palindrome ; A is not palindromic ; K can be infitely large in this case ; Variable to store the largest K that makes A palindromic ; Return the required answer ; Driver code ; Input ; Function call
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static int largestK ( int A [ ] , int N ) { int l = 0 , r = N - 1 , flag = 0 ; while ( l < r ) { if ( A [ l ] != A [ r ] ) { flag = 1 ; break ; } l ++ ; r -- ; } if ( flag == 0 ) return - 1 ; int K = Math . abs ( A [ 0 ] - A [ N - 1 ] ) ; for ( int i = 1 ; i < N / 2 ; i ++ ) K = gcd ( K , Math . abs ( A [ i ] - A [ N - i - 1 ] ) ) ; return K ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 2 , 1 } ; int N = A . length ; System . out . println ( largestK ( A , N ) ) ; } }
Check if N can be represented as sum of distinct powers of 3 | Java program for the above approach ; Function to try all permutations of distinct powers ; Base Case ; If the distinct powers sum is obtained ; Otherwise ; If current element not selected in power [ ] ; If current element selected in power [ ] ; Return 1 if any permutation found ; Function to check the N can be represented as the sum of the distinct powers of 3 ; Stores the all distincts powers of three to [ 0 , 15 ] ; Function Call ; print ; Driven Code
class GFG { public static boolean PermuteAndFind ( int [ ] power , int idx , int SumSoFar , int target ) { if ( idx == power . length ) { if ( SumSoFar == target ) return true ; return false ; } boolean select = PermuteAndFind ( power , idx + 1 , SumSoFar , target ) ; boolean notselect = PermuteAndFind ( power , idx + 1 , SumSoFar + power [ idx ] , target ) ; return ( select notselect ) ; } public static void DistinctPowersOf3 ( int N ) { int [ ] power = new int [ 16 ] ; power [ 0 ] = 1 ; for ( int i = 1 ; i < 16 ; i ++ ) power [ i ] = 3 * power [ i - 1 ] ; boolean found = PermuteAndFind ( power , 0 , 0 , N ) ; if ( found == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { int N = 91 ; DistinctPowersOf3 ( N ) ; } }
Check if N can be represented as sum of distinct powers of 3 | Java program for the above approach ; Function to check whether the given N can be represented as the sum of the distinct powers of 3 ; Iterate until N is non - zero ; Termination Condition ; Right shift ternary bits by 1 for the next digit ; If N can be expressed as the sum of perfect powers of 3 ; Driver Code
class GFG { public static void DistinctPowersOf3 ( int N ) { while ( N > 0 ) { if ( N % 3 == 2 ) { System . out . println ( " No " ) ; return ; } N /= 3 ; } System . out . println ( " Yes " ) ; } public static void main ( String args [ ] ) { int N = 91 ; DistinctPowersOf3 ( N ) ; } }
Generate an N | Java program for tha above approach ; Function to print the permutation of size N with absolute difference of adjacent elements in range [ 2 , 4 ] ; If N is less than 4 ; Check if N is even ; Traverse through odd integers ; Update the value of i ; Traverse through even integers ; Driver Code
import java . util . * ; class GFG { static void getPermutation ( int N ) { if ( N <= 3 ) { System . out . print ( - 1 ) ; return ; } int i = N ; if ( N % 2 == 0 ) i -- ; while ( i >= 1 ) { System . out . print ( i + " ▁ " ) ; i -= 2 ; } System . out . print ( 4 + " ▁ " + 2 + " ▁ " ) ; i = 6 ; while ( i <= N ) { System . out . print ( i + " ▁ " ) ; i += 2 ; } } public static void main ( String [ ] args ) { int N = 9 ; getPermutation ( N ) ; } }
Count of distinct values till C formed by adding or subtracting A , B , or 0 any number of times | Java program for the above approach ; Function to calculate gcd ; Function to find number of possible final values ; Find the gcd of two numbers ; Calculate number of distinct values ; Return values ; Driver Code
import java . util . * ; class GFG { static int gcd ( int A , int B ) { if ( B == 0 ) return A ; else return gcd ( B , A % B ) ; } static int getDistinctValues ( int A , int B , int C ) { int g = gcd ( A , B ) ; int num_values = C / g ; return num_values ; } public static void main ( String [ ] args ) { int A = 2 ; int B = 3 ; int C = 10 ; System . out . println ( getDistinctValues ( A , B , C ) ) ; } }
Find array whose elements are XOR of adjacent elements in given array | Java implementation of the above approach ; Function to reconstruct the array arr [ ] with xor of adjacent elements ; Iterate through each element ; Store the xor of current and next element in arr [ i ] ; Function to print the array ; Driver Code ; Inputs ; Length of the array given ; Function call to reconstruct the arr [ ] ; Function call to print arr [ ]
import java . io . * ; class GFG { static int [ ] game_with_number ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i + 1 ] ; } return arr ; } static void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 11 , 1 , 2 , 3 } ; int n = arr . length ; int [ ] new_arr = game_with_number ( arr , n ) ; print ( new_arr , n ) ; } }
Maximize score of same | Java program for the above approach ; Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Traverse the current subarray ; Finding the score without reversing the subarray ; Calculating the score of the reversed subarray ; Return the score of subarray ; Function to find the subarray with the maximum score ; Stores the maximum score and the starting and the ending point of subarray with maximum score ; Traverse all the subarrays ; Store the score of the current subarray ; Update the maximum score ; Print the maximum score ; Driver Code
import java . io . * ; class GFG { static int currSubArrayScore ( int [ ] a , int [ ] b , int l , int r ) { int straightScore = 0 ; int reverseScore = 0 ; for ( int i = l ; i <= r ; i ++ ) { straightScore += a [ i ] * b [ i ] ; reverseScore += a [ r - ( i - l ) ] * b [ i ] ; } return Math . max ( straightScore , reverseScore ) ; } static void maxScoreSubArray ( int [ ] a , int [ ] b , int n ) { int res = 0 , start = 0 , end = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int currScore = currSubArrayScore ( a , b , i , j ) ; if ( currScore > res ) { res = currScore ; start = i ; end = j ; } } } System . out . print ( res ) ; } public static void main ( String [ ] args ) { int A [ ] = { 13 , 4 , 5 } ; int B [ ] = { 10 , 22 , 2 } ; int N = A . length ; maxScoreSubArray ( A , B , N ) ; } }
Maximize score of same | Java Program for the above approach ; Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Store the required result ; Iterate in the range [ 0 , N - 1 ] ; Consider the case of odd length subarray ; Update the maximum score ; Expanding the subarray in both directions with equal length so that mid point remains same ; Update both the scores ; Consider the case of even length subarray ; Update both the scores ; Print the result ; Driver Code
import java . io . * ; class GFG { static void maxScoreSubArray ( int [ ] a , int [ ] b , int n ) { int res = 0 ; for ( int mid = 0 ; mid < n ; mid ++ ) { int straightScore = a [ mid ] * b [ mid ] , reverseScore = a [ mid ] * a [ mid ] ; int prev = mid - 1 , next = mid + 1 ; res = Math . max ( res , Math . max ( straightScore , reverseScore ) ) ; while ( prev >= 0 && next < n ) { straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) ; reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) ; res = Math . max ( res , Math . max ( straightScore , reverseScore ) ) ; prev -- ; next ++ ; } straightScore = 0 ; reverseScore = 0 ; prev = mid - 1 ; next = mid ; while ( prev >= 0 && next < n ) { straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) ; reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) ; res = Math . max ( res , Math . max ( straightScore , reverseScore ) ) ; prev -- ; next ++ ; } } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int A [ ] = { 13 , 4 , 5 } ; int B [ ] = { 10 , 22 , 2 } ; int N = A . length ; maxScoreSubArray ( A , B , N ) ; } }