codeparrot/xlcost-text-to-code huggingface dataset:
Prefix Factorials of a Prefix Sum Array | Java program for the above approach ; Function to find the factorial of a number N ; Base Case ; Find the factorial recursively ; Function to find the prefix factorial array ; Find the prefix sum array ; Find the factorials of each array element ; Print the resultant array ; Driver Code
class GFG { static int fact ( int N ) { if ( N == 1 N == 0 ) return 1 ; return N * fact ( N - 1 ) ; } static void prefixFactorialArray ( int [ ] arr , int N ) { for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = fact ( arr [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . length ; prefixFactorialArray ( arr , N ) ; } }
Mean of fourth powers of first N natural numbers | Java program for the above approach ; Function to find the average of the fourth power of first N natural numbers ; Store the resultant average calculated using formula ; Return the average ; Driver Code
import java . util . * ; class GFG { static double findAverage ( int N ) { double avg = ( ( 6 * N * N * N * N ) + ( 15 * N * N * N ) + ( 10 * N * N ) - 1 ) / 30.0 ; return avg ; } public static void main ( String args [ ] ) { int N = 3 ; System . out . print ( findAverage ( N ) ) ; } }
Modify array by removing ( arr [ i ] + arr [ i + 1 ] ) th element exactly K times | java program for the above approach ; Function to modify array by removing every K - th element from the array ; Check if current element is the k - th element ; Stores the elements after removing every kth element ; Append the current element if it is not k - th element ; Return the new array after removing every k - th element ; Function to print the array ; Traverse the array l [ ] ; Function to print the array after performing the given operations exactly k times ; Store first N natural numbers ; Iterate over the range [ 0 , k - 1 ] ; Store sums of the two consecutive terms ; Remove every p - th element from the array ; Increment x by 1 for the next iteration ; Print the resultant array ; Driver Code ; Given arrays ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int [ ] removeEveryKth ( int l [ ] , int k ) { for ( int i = 0 ; i < l . length ; i ++ ) { if ( i % k == 0 ) l [ i ] = 0 ; } ArrayList < Integer > list = new ArrayList < > ( ) ; list . add ( 0 ) ; for ( int i = 1 ; i < l . length ; i ++ ) { if ( l [ i ] != 0 ) list . add ( l [ i ] ) ; } return list . stream ( ) . mapToInt ( i -> i ) . toArray ( ) ; } static void printArray ( int l [ ] ) { for ( int i = 1 ; i < l . length ; i ++ ) System . out . print ( l [ i ] + " ▁ " ) ; System . out . println ( ) ; } static void printSequence ( int n , int k ) { int l [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) l [ i ] = i ; int x = 1 ; for ( int i = 0 ; i < k ; i ++ ) { int p = l [ x ] + l [ x + 1 ] ; l = removeEveryKth ( l , p ) ; x += 1 ; } printArray ( l ) ; } public static void main ( String [ ] args ) { int N = 8 ; int K = 2 ; printSequence ( N , K ) ; } }
Minimum number of bits of array elements required to be flipped to make all array elements equal | Java program for the above approach ; Function to count minimum number of bits required to be flipped to make all array elements equal ; Stores the count of unset bits ; Stores the count of set bits ; Traverse the array ; Traverse the bit of arr [ i ] ; If current bit is set ; Increment fre1 [ j ] ; Otherwise ; Increment fre0 [ j ] ; Right shift x by 1 ; Stores the count of total moves ; Traverse the range [ 0 , 32 ] ; Update the value of ans ; Return the minimum number of flips required ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int makeEqual ( int arr [ ] , int n ) { int fre0 [ ] = new int [ 33 ] ; int fre1 [ ] = new int [ 33 ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; for ( int j = 0 ; j < 33 ; j ++ ) { if ( ( x & 1 ) != 0 ) { fre1 [ j ] += 1 ; } else { fre0 [ j ] += 1 ; } x = x >> 1 ; } } int ans = 0 ; for ( int i = 0 ; i < 33 ; i ++ ) { ans += Math . min ( fre0 [ i ] , fre1 [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 } ; int N = arr . length ; System . out . print ( makeEqual ( arr , N ) ) ; } }
Sum of array elements possible by appending arr [ i ] / K to the end of the array K times for array elements divisible by K | Java program for the above approach ; Function to calculate sum of array elements after adding arr [ i ] / K to the end of the array if arr [ i ] is divisible by K ; Stores the sum of the array ; Traverse the array arr [ ] ; Traverse the vector ; If v [ i ] is divisible by K ; Iterate over the range [ 0 , K ] ; Update v ; Otherwise ; Traverse the vector v ; Return the sum of the updated array ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int sum ( int arr [ ] , int N , int K ) { int sum = 0 ; ArrayList < Integer > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { v . add ( arr [ i ] ) ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v . get ( i ) % K == 0 ) { int x = v . get ( i ) / K ; for ( int j = 0 ; j < K ; j ++ ) { v . add ( x ) ; } } else break ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) sum = sum + v . get ( i ) ; return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 8 , 2 } ; int K = 2 ; int N = arr . length ; System . out . println ( sum ( arr , N , K ) ) ; } }
Check if sum of arr [ i ] / j for all possible pairs ( i , j ) in an array is 0 or not | Java program for the above approach ; Function to check if sum of all values of ( arr [ i ] / j ) for all 0 < i <= j < ( N - 1 ) is 0 or not ; Stores the required sum ; Traverse the array ; If the sum is equal to 0 ; Otherwise ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void check ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum == 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , 3 , - 2 , - 1 } ; int N = arr . length ; check ( arr , N ) ; } }
Program to calculate expected increase in price P after N consecutive days | Java program for the above approach ; Function to find the increased value of P after N days ; Expected value of the number P after N days ; Print the expected value ; Driver code
class GFG { static void expectedValue ( int P , int a , int b , int N ) { double expValue = P + ( N * 0.5 * ( a + b ) ) ; System . out . print ( expValue ) ; } public static void main ( String [ ] args ) { int P = 3000 , a = 20 , b = 10 , N = 30 ; expectedValue ( P , a , b , N ) ; } }
Find the index in a circular array from which prefix sum is always non | Java program for the above approach ; Function to find the starting index of the given circular array s . t . prefix sum array is non negative ; Stores the sum of the array ; Stores the starting index ; Stores the minimum prefix sum of A [ 0. . i ] ; Traverse the array arr [ ] ; Update the value of sum ; If sum is less than min ; Update the min as the value of prefix sum ; Update in ; Otherwise , no such index is possible ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int startingPoint ( int A [ ] , int N ) { int sum = 0 ; int in = 0 ; int min = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( sum < min ) { min = sum ; in = i + 1 ; } } if ( sum < 0 ) { return - 1 ; } return in % N ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , - 6 , 7 , - 4 , - 4 , 6 , - 1 } ; int N = arr . length ; System . out . print ( startingPoint ( arr , N ) ) ; } }
Modify Linked List by replacing each node by nearest multiple of K | Java program for the above approach ; Structure of node ; Function to replace the node N by the nearest multiple of K ; Traverse the Linked List ; If data is less than K ; If the data of current node is same as K ; Otherwise change the value ; Move to the next node ; Return the updated LL ; Function to print the nodes of the Linked List ; Traverse the LL ; Print the node 's data ; Driver Code ; Given Linked List
import java . io . * ; class GFG { static class node { int data ; node next ; node ( int d ) { data = d ; } } static node EvalNearestMult ( node N , int K ) { node temp = N ; int t ; while ( N != null ) { if ( N . data < K ) N . data = 0 ; else { if ( N . data == K ) N . data = K ; else { N . data = ( N . data / K ) * K ; } } N = N . next ; } return temp ; } static void printList ( node N ) { while ( N != null ) { System . out . print ( N . data + " ▁ " ) ; N = N . next ; } } public static void main ( String [ ] args ) { node head = new node ( 3 ) ; head . next = new node ( 5 ) ; head . next . next = new node ( 8 ) ; node t = EvalNearestMult ( head , 3 ) ; printList ( t ) ; } }
Modify array by replacing elements with the nearest power of its previous or next element | Java program for the above approach ; Function to calculate the power of y which is nearest to x ; Base Case ; Stores the logarithmic value of x with base y ; Function to replace each array element by the nearest power of its previous or next element ; Stores the previous and next element ; Traverse the array ; Calculate nearest power for previous and next elements ; Replacing the array values ; Print the updated array ; Driver Code ; Given array
class GFG { static int nearestPow ( int x , int y ) { if ( y == 1 ) return 1 ; int k = ( int ) ( Math . log10 ( x ) / Math . log10 ( y ) ) ; if ( Math . abs ( Math . pow ( y , k ) - x ) < Math . abs ( Math . pow ( y , ( k + 1 ) ) - x ) ) return ( int ) ( Math . pow ( y , k ) ) ; return ( int ) ( Math . pow ( y , ( k + 1 ) ) ) ; } static void replacebyNearestPower ( int [ ] arr ) { int prev = arr [ arr . length - 1 ] ; int lastNext = arr [ 0 ] ; int next = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { int temp = arr [ i ] ; if ( i == arr . length - 1 ) next = lastNext ; else next = arr [ ( i + 1 ) % arr . length ] ; int prevPow = nearestPow ( arr [ i ] , prev ) ; int nextPow = nearestPow ( arr [ i ] , next ) ; if ( Math . abs ( arr [ i ] - prevPow ) < Math . abs ( arr [ i ] - nextPow ) ) arr [ i ] = prevPow ; else arr [ i ] = nextPow ; prev = temp ; } for ( int i = 0 ; i < arr . length ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 3 , 4 , 1 , 2 } ; replacebyNearestPower ( arr ) ; } }
Check if sum of array can be made equal to X by removing either the first or last digits of every array element | Java program for the above approach ; Utility Function to check if the sum of the array elements can be made equal to X by removing either the first or last digits of every array element ; Base Case ; Convert arr [ i ] to string ; Remove last digit ; Remove first digit ; Recursive function call ; Function to check if sum of given array can be made equal to X or not ; Driver Code ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static boolean makeSumX ( int arr [ ] , int X , int S , int i ) { if ( i == arr . length ) { return S == X ; } String a = Integer . toString ( arr [ i ] ) ; int l = Integer . parseInt ( a . substring ( 0 , a . length ( ) - 1 ) ) ; int r = Integer . parseInt ( a . substring ( 1 ) ) ; boolean x = makeSumX ( arr , X , S + l , i + 1 ) ; boolean y = makeSumX ( arr , X , S + r , i + 1 ) ; return ( x y ) ; } static void Check ( int arr [ ] , int X ) { if ( makeSumX ( arr , X , 0 , 0 ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 545 , 433 , 654 , 23 } ; int X = 134 ; Check ( arr , X ) ; } }
Count subarrays having product equal to the power of a given Prime Number | Java code of above approach ; Function to check if y is a power of m or not ; Calculate log y base m and store it in a variable with integer datatype ; Calculate log y base m and store it in a variable with double datatype ; If res1 and res2 are equal , return True . Otherwise , return false ; Function to count the number of subarrays having product of elements equal to a power of m , where m is a prime number ; Stores the count of subarrays required ; Stores current sequence of consecutive array elements which are a multiple of m ; Traverse the array ; If arr [ i ] is a power of M ; Increment cnt ; Update ans ; Update cnt ; Return the count of subarrays ; Driver code ; Input
import java . util . * ; class GFG { static boolean isPower ( int m , int y ) { int res1 = ( int ) Math . log ( y ) / ( int ) Math . log ( m ) ; double res2 = ( int ) Math . log ( y ) / ( int ) Math . log ( m ) ; return ( res1 == res2 ) ; } static int numSub ( int arr [ ] , int n , int m ) { int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPower ( m , arr [ i ] ) ) { cnt ++ ; ans += ( cnt * ( cnt - 1 ) ) / 2 ; } else { cnt = 0 ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 3 } ; int m = 3 ; int n = arr . length ; System . out . println ( numSub ( arr , n , m ) ) ; } }
Count pairs whose Bitwise AND exceeds Bitwise XOR from a given array | Java program to implement the above approach ; Function to count pairs that satisfy the above condition ; Stores the count of pairs ; Stores the count of array elements having same positions of MSB ; Traverse the array ; Stores the index of MSB of array elements ; Calculate number of pairs ; Driver Code ; Given Input ; Function call to count pairs satisfying the given condition
import java . io . * ; class GFG { static int cntPairs ( int arr [ ] , int N ) { int res = 0 ; int bit [ ] = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int pos = ( int ) ( Math . log ( arr [ i ] ) / Math . log ( 2 ) ) ; bit [ pos ] ++ ; } for ( int i = 0 ; i < 32 ; i ++ ) { res += ( bit [ i ] * ( bit [ i ] - 1 ) ) / 2 ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; System . out . println ( cntPairs ( arr , N ) ) ; } }
Minimum MEX from all subarrays of length K | Java program for the above approach ; Function to return minimum MEX from all K - length subarrays ; Stores element from [ 1 , N + 1 ] which are not present in subarray ; Store number 1 to N + 1 in set s ; Find the MEX of K - length subarray starting from index 0 ; Find the MEX of all subarrays of length K by erasing arr [ i ] and inserting arr [ i - K ] ; Store first element of set ; Updating the mex ; Print minimum MEX of all K length subarray ; Driver code
import java . util . HashSet ; class GFG { static void minimumMEX ( int arr [ ] , int N , int K ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 1 ; i <= N + 1 ; i ++ ) s . add ( i ) ; for ( int i = 0 ; i < K ; i ++ ) s . remove ( arr [ i ] ) ; int mex = s . iterator ( ) . next ( ) ; for ( int i = K ; i < N ; i ++ ) { s . remove ( arr [ i ] ) ; s . add ( arr [ i - K ] ) ; int firstElem = s . iterator ( ) . next ( ) ; mex = Math . min ( mex , firstElem ) ; } System . out . print ( mex + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 3 ; int N = arr . length ; minimumMEX ( arr , N , K ) ; } }
Count smaller elements present in the array for each array element | Java program for the above approach ; Function to count for each array element , the number of elements that are smaller than that element ; Stores the frequencies of array elements ; Traverse the array ; Update frequency of arr [ i ] ; Initialize sum with 0 ; Compute prefix sum of the array hash [ ] ; Traverse the array arr [ ] ; If current element is 0 ; Print the resultant count ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void smallerNumbers ( int arr [ ] , int N ) { int hash [ ] = new int [ 100000 ] ; for ( int i = 0 ; i < N ; i ++ ) hash [ arr [ i ] ] ++ ; int sum = 0 ; for ( int i = 1 ; i < 100000 ; i ++ ) { hash [ i ] += hash [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { System . out . println ( "0" ) ; continue ; } System . out . print ( hash [ arr [ i ] - 1 ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 1 , 1 , 2 } ; int N = arr . length ; smallerNumbers ( arr , N ) ; } }
Modulo Operations in Programming With Negative Results | Java program to illustrate modulo operations using the above equation ; Function to calculate and return the remainder of a % n ; ( a / n ) implicitly gives the truncated result ; Driver Code ; Modulo of two positive numbers ; Modulo of a negative number by a positive number ; Modulo of a positive number by a negative number ; Modulo of two negative numbers
class GFG { static int truncMod ( int a , int n ) { int q = a / n ; return a - n * q ; } public static void main ( String [ ] args ) { int a , b ; a = 9 ; b = 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; a = - 9 ; b = 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; a = 9 ; b = - 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; a = - 9 ; b = - 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + truncMod ( a , b ) ) ; } }
Program for average of an array without running into overflow | Java program for the above approach ; Function to calculate average of an array using standard method ; Stores the sum of array ; Find the sum of the array ; Return the average ; Function to calculate average of an array using efficient method ; Store the average of the array ; Traverse the array arr [ ] ; Update avg ; Return avg ; Driver Code ; Input ; Function call
import java . util . * ; class GFG { static Double average ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; return Double . valueOf ( sum / N ) ; } static Double mean ( int arr [ ] , int N ) { Double avg = 0.0 ; for ( int i = 0 ; i < N ; i ++ ) { avg += Double . valueOf ( ( arr [ i ] - avg ) / ( i + 1 ) ) ; } return avg ; } public static void main ( String args [ ] ) { int arr [ ] = { Integer . MAX_VALUE , 1 , 2 } ; int N = arr . length ; System . out . println ( " Average ▁ by ▁ Standard ▁ method : ▁ " + String . format ( " % .10f " , average ( arr , N ) ) ) ; System . out . println ( " Average ▁ by ▁ Efficient ▁ method : ▁ " + String . format ( " % .10f " , mean ( arr , N ) ) ) ; } }
Count number of pairs ( i , j ) from an array such that arr [ i ] * j = arr [ j ] * i | Java program for the above approach ; Function to count pairs from an array satisfying given conditions ; Stores the total count of pairs ; Stores count of a [ i ] / i ; Traverse the array ; Updating count ; Update frequency in the Map ; Print count of pairs ; Driver Code ; Given array ; Size of the array ; Function call to count pairs satisfying given conditions
import java . util . * ; class GFG { static void countPairs ( int [ ] arr , int N ) { int count = 0 ; Map < Double , Integer > mp = new HashMap < Double , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Double val = 1.0 * arr [ i ] ; Double idx = 1.0 * ( i + 1 ) ; if ( mp . containsKey ( val / idx ) ) count += mp . get ( val / idx ) ; if ( mp . containsKey ( val / idx ) ) mp . put ( val / idx , mp . getOrDefault ( val / idx , 0 ) + 1 ) ; else mp . put ( val / idx , 1 ) ; } System . out . print ( count ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 3 , 5 , 6 , 5 } ; int N = arr . length ; countPairs ( arr , N ) ; } }
Count ancestors with smaller value for each node of a Binary Tree | Java program for the above approach ; Function to add an edge between nodes u and v ; Function to perform the DFS Traversal and store parent of each node ; Store the immediate parent ; Traverse the children of the current node ; Recursively call for function dfs for the child node ; Function to count the number of ancestors with values smaller than that of the current node ; Stores the parent of each node ; Perform the DFS Traversal ; Traverse all the nodes ; Store the number of ancestors smaller than node ; Loop until parent [ node ] != - 1 ; If the condition satisfies , increment cnt by 1 ; Print the required result for the current node ; Driver code ; Tree Formation
import java . io . * ; import java . util . * ; class GFG { static void add_edge ( ArrayList < ArrayList < Integer > > adj , int u , int v ) { adj . get ( u ) . add ( v ) ; adj . get ( v ) . add ( u ) ; } static void dfs ( ArrayList < Integer > parent , ArrayList < ArrayList < Integer > > adj , int u , int par ) { parent . set ( u , par ) ; for ( int child : adj . get ( u ) ) { if ( child != par ) dfs ( parent , adj , child , u ) ; } } static void countSmallerAncestors ( ArrayList < ArrayList < Integer > > adj , int n ) { ArrayList < Integer > parent = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < ( int ) ( 1e5 ) ; i ++ ) { parent . add ( 0 ) ; } dfs ( parent , adj , 1 , - 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) { int node = i ; int cnt = 0 ; while ( parent . get ( node ) != - 1 ) { if ( parent . get ( node ) < i ) cnt += 1 ; node = parent . get ( node ) ; } System . out . print ( cnt + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 6 ; ArrayList < ArrayList < Integer > > adj = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < ( int ) ( 1e5 ) ; i ++ ) { adj . add ( new ArrayList < Integer > ( ) ) ; } add_edge ( adj , 1 , 5 ) ; add_edge ( adj , 1 , 4 ) ; add_edge ( adj , 4 , 6 ) ; add_edge ( adj , 5 , 3 ) ; add_edge ( adj , 5 , 2 ) ; countSmallerAncestors ( adj , N ) ; } }
Count subsequences having odd Bitwise XOR values from an array | Java program for the above approach ; Function to count the subsequences having odd bitwise XOR value ; Stores count of odd elements ; Stores count of even elements ; Traverse the array A [ ] ; If el is odd ; If count of odd elements is 0 ; Driver Code ; Given array A [ ] ; Function call to count subsequences having odd bitwise XOR value
import java . io . * ; class GFG { public static void countSubsequences ( int [ ] A ) { int odd = 0 ; int even = 0 ; for ( int el : A ) { if ( el % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 ) System . out . println ( 0 ) ; else System . out . println ( 1 << ( A . length - 1 ) ) ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 3 , 4 } ; countSubsequences ( A ) ; } }
Maximum subarray product modulo M | Java program for the above approach ; Function to find maximum subarray product modulo M and minimum length of the subarray ; Stores maximum subarray product modulo M and minimum length of the subarray ; Stores the minimum length of subarray having maximum product ; Traverse the array ; Stores the product of a subarray ; Calculate Subarray whose start index is i ; Multiply product by arr [ i ] ; If product greater than ans ; Update ans ; Update length ; Print maximum subarray product mod M ; Print minimum length of subarray having maximum product ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void maxModProdSubarr ( int arr [ ] , int n , int M ) { int ans = 0 ; int length = n ; for ( int i = 0 ; i < n ; i ++ ) { int product = 1 ; for ( int j = i ; j < n ; j ++ ) { product = ( product * arr [ i ] ) % M ; if ( product > ans ) { ans = product ; if ( length > j - i + 1 ) { length = j - i + 1 ; } } } } System . out . println ( " Maximum ▁ subarray ▁ product ▁ is ▁ " + ans ) ; System . out . println ( " Minimum ▁ length ▁ of ▁ the ▁ maximum ▁ " + " product ▁ subarray ▁ is ▁ " + length ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 2 } ; int N = arr . length ; int M = 5 ; maxModProdSubarr ( arr , N , M ) ; } }