codeparrot/xlcost-text-to-code huggingface dataset:
Divide array into two arrays which does not contain any pair with sum K | Java program for the above approach ; Function to split the given array into two separate arrays satisfying given condition ; Stores resultant arrays ; Traverse the array ; If a [ i ] is smaller than or equal to k / 2 ; Print first array ; Print second array ; Driver Code ; Given K ; Given array ; Given size
import java . util . * ; class GFG { static void splitArray ( int a [ ] , int n , int k ) { Vector < Integer > first = new Vector < > ( ) ; Vector < Integer > second = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] <= k / 2 ) first . add ( a [ i ] ) ; else second . add ( a [ i ] ) ; } for ( int i = 0 ; i < first . size ( ) ; i ++ ) { System . out . print ( first . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; for ( int i = 0 ; i < second . size ( ) ; i ++ ) { System . out . print ( second . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int k = 5 ; int a [ ] = { 0 , 1 , 3 , 2 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = a . length ; splitArray ( a , n , k ) ; } }
Count numbers in a given range whose count of prime factors is a Prime Number | Java program to implement the above approach ; Function to find the smallest prime factor of all the numbers in range [ 0 , MAX ] ; Stores smallest prime factor of all the numbers in the range [ 0 , MAX ] ; No smallest prime factor of 0 and 1 exists ; Traverse all the numbers in the range [ 1 , MAX ] ; Update spf [ i ] ; Update all the numbers whose smallest prime factor is 2 ; Traverse all the numbers in the range [ 1 , sqrt ( MAX ) ] ; Check if i is a prime number ; Update all the numbers whose smallest prime factor is i ; Check if j is a prime number ; Function to find count of prime factor of num ; Stores count of prime factor of num ; Calculate count of prime factor ; Update count ; Update num ; Function to precalculate the count of numbers in the range [ 0 , i ] whose count of prime factors is a prime number ; Stores the sum of all the numbers in the range [ 0 , i ] count of prime factor is a prime number ; Update sum [ 0 ] ; Traverse all the numbers in the range [ 1 , MAX ] ; Stores count of prime factor of i ; If count of prime factor is a prime number ; Update sum [ i ] ; Update sum [ i ] ; Driver code ; Stores smallest prime factor of all the numbers in the range [ 0 , MAX ] ; Stores the sum of all the numbers in the range [ 0 , i ] count of prime factor is a prime number ; int N = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ;
import java . util . * ; class GFG { public static int MAX = 1001 ; public static int [ ] sieve ( ) { int spf [ ] = new int [ MAX ] ; spf [ 0 ] = spf [ 1 ] = - 1 ; for ( int i = 2 ; i < MAX ; i ++ ) { spf [ i ] = i ; } for ( int i = 4 ; i < MAX ; i = i + 2 ) { spf [ i ] = 2 ; } for ( int i = 3 ; i * i < MAX ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAX ; j = j + i ) { if ( spf [ j ] == j ) { spf [ j ] = i ; } } } } return spf ; } public static int countFactors ( int spf [ ] , int num ) { int count = 0 ; while ( num > 1 ) { count ++ ; num = num / spf [ num ] ; } return count ; } public static int [ ] precalculateSum ( int spf [ ] ) { int sum [ ] = new int [ MAX ] ; sum [ 0 ] = 0 ; for ( int i = 1 ; i < MAX ; i ++ ) { int prime_factor = countFactors ( spf , i ) ; if ( spf [ prime_factor ] == prime_factor ) { sum [ i ] = sum [ i - 1 ] + 1 ; } else { sum [ i ] = sum [ i - 1 ] ; } } return sum ; } public static void main ( String [ ] args ) { int spf [ ] = sieve ( ) ; int sum [ ] = precalculateSum ( spf ) ; int Q [ ] [ ] = { { 4 , 8 } , { 30 , 32 } } ; for ( int i = 0 ; i < 2 ; i ++ ) { System . out . print ( ( sum [ Q [ i ] [ 1 ] ] - sum [ Q [ i ] [ 0 ] - 1 ] ) + " ▁ " ) ; } } }
Maximum number of Perfect Numbers present in a subarray of size K | Java program for the above approach ; Function to check a number is Perfect Number or not ; Stores sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to N ; Function to return maximum sum of a subarray of size K ; If k is greater than N ; Compute sum of first window of size K ; Compute sums of remaining windows by removing first element of previous window and adding last element of current window ; return the answer ; Function to find all the perfect numbers in the array ; The given array is converted into binary array ; Driver Code
import java . util . * ; class GFG { static int isPerfect ( int N ) { int sum = 1 ; for ( int i = 2 ; i < Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { sum += i ; } else { sum += i + N / i ; } } } if ( sum == N && N != 1 ) return 1 ; return 0 ; } static int maxSum ( int arr [ ] , int N , int K ) { if ( N < K ) { System . out . print ( " Invalid " ) ; return - 1 ; } int res = 0 ; for ( int i = 0 ; i < K ; i ++ ) { res += arr [ i ] ; } int curr_sum = res ; for ( int i = K ; i < N ; i ++ ) { curr_sum += arr [ i ] - arr [ i - K ] ; res = Math . max ( res , curr_sum ) ; } return res ; } static int max_PerfectNumbers ( int arr [ ] , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = isPerfect ( arr [ i ] ) == 1 ? 1 : 0 ; } return maxSum ( arr , N , K ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 28 , 2 , 3 , 6 , 496 , 99 , 8128 , 24 } ; int K = 4 ; int N = arr . length ; System . out . print ( max_PerfectNumbers ( arr , N , K ) ) ; } }
Count greater elements on the left side of every array element | Java program to implement the above approach ; Function to print the count of greater elements on left of each array element ; Function to get the count of greater elements on left of each array element ; Store distinct array elements in sorted order ; Stores the count of greater elements on the left side ; Traverse the array ; Insert array elements into the set ; Find previous greater element ; Find the distance between the previous greater element of arr [ i ] and last element of the set ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static void display ( int countLeftGreater [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( countLeftGreater [ i ] + " ▁ " ) ; } } static void countGreater ( int arr [ ] , int N ) { Set < Integer > St = new TreeSet < > ( ) ; int [ ] countLeftGreater = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { St . add ( arr [ i ] ) ; int it = 0 ; Iterator < Integer > iterator = St . iterator ( ) ; while ( iterator . hasNext ( ) ) { if ( arr [ i ] < iterator . next ( ) ) { break ; } it ++ ; } countLeftGreater [ i ] = Math . abs ( it - St . size ( ) ) ; } display ( countLeftGreater , N ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 1 , 2 , 3 , 0 , 11 , 4 } ; int N = arr . length ; countGreater ( arr , N ) ; } }
Count characters to be shifted from the start or end of a string to obtain another string | Java Program for the above approach ; Function to find the minimum cost to convert string A to string B ; Length of string ; Initialize maxlen as 0 ; Traverse the string A ; Stores the length of substrings of string A ; Traversing string B for each character of A ; Shift i pointer towards right and increment length , if A [ i ] equals B [ j ] ; If traverse till end ; Update maxlen ; Return minimum cost ; Driver Code ; Given two strings A and B ; Function call
import java . util . * ; import java . lang . * ; class GFG { static int minCost ( String A , String B ) { int n = A . length ( ) ; int i = 0 ; int maxlen = 0 ; while ( i < n ) { int length = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( A . charAt ( i ) == B . charAt ( j ) ) { ++ i ; ++ length ; if ( i == n ) break ; } } maxlen = Math . max ( maxlen , length ) ; } return n - maxlen ; } public static void main ( String [ ] args ) { String A = " edacb " ; String B = " abcde " ; System . out . println ( minCost ( A , B ) ) ; } }
Lexicographic rank of a string among all its substrings | Java program for the above approach ; Function to find lexicographic rank of String among all its subString ; Length of String ; Traverse the given String and store the indices of each character ; Extract the index ; Store it in the vector ; Traverse the alphaIndex array lesser than the index of first character of given String ; If alphaIndex [ i ] size exceeds 0 ; Traverse over the indices ; Add count of subString equal to n - alphaIndex [ i ] [ j ] ; Store all subStrings in a vector str starting with the first character of the given String ; Insert the current character to subString ; Store the subString formed ; Sort the subString in the lexicographical order ; Find the rank of given String ; increase the rank until the given String is same ; If subString is same as the given String ; Add 1 to rank of the given String ; Driver Code ; Given String ; Function Call
import java . util . * ; class GFG { static int lexicographicRank ( char [ ] s ) { int n = s . length ; Vector < Integer > [ ] alphaIndex = new Vector [ 26 ] ; for ( int i = 0 ; i < alphaIndex . length ; i ++ ) alphaIndex [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < s . length ; i ++ ) { int x = s [ i ] - ' a ' ; alphaIndex [ x ] . add ( i ) ; } int rank = 0 ; for ( int i = 0 ; i < 26 && ' a ' + i < s [ 0 ] ; i ++ ) { if ( alphaIndex [ i ] . size ( ) > 0 ) { for ( int j = 0 ; j < alphaIndex [ i ] . size ( ) ; j ++ ) { rank = rank + ( n - alphaIndex [ i ] . get ( j ) ) ; } } } Vector < String > str = new Vector < String > ( ) ; for ( int i = 0 ; i < alphaIndex [ s [ 0 ] - ' a ' ] . size ( ) ; i ++ ) { String subString = " " ; int j = alphaIndex [ s [ 0 ] - ' a ' ] . get ( i ) ; for ( ; j < n ; j ++ ) { subString += ( s [ j ] ) ; str . add ( subString ) ; } } Collections . sort ( str ) ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) { if ( ! str . get ( i ) . equals ( String . valueOf ( s ) ) ) { rank ++ ; } else { break ; } } return rank + 1 ; } public static void main ( String [ ] args ) { String str = " enren " ; System . out . print ( lexicographicRank ( str . toCharArray ( ) ) ) ; } }
Find H | Java implementation of the above approach ; Function to find the H - index ; Set the range for binary search ; Check if current citations is possible ; Check to the right of mid ; Update h - index ; Since current value is not possible , check to the left of mid ; Print the h - index ; Driver Code ; citations
import java . io . * ; class GFG { static int hIndex ( int [ ] citations , int n ) { int hindex = 0 ; int low = 0 , high = n - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( citations [ mid ] >= ( mid + 1 ) ) { low = mid + 1 ; hindex = mid + 1 ; } else { high = mid - 1 ; } } System . out . println ( hindex ) ; return hindex ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] citations = { 5 , 3 , 3 , 2 , 2 } ; hIndex ( citations , n ) ; } }
Check if all strings of an array can be made same by interchanging characters | Java Program to implement the above approach ; Function to check if all Strings are equal after swap operations ; Stores the frequency of characters ; Stores the length of String ; Traverse the array ; Traverse each String ; Check if frequency of character is divisible by N ; Driver Code
class GFG { static boolean checkEqual ( String arr [ ] , int N ) { int hash [ ] = new int [ 256 ] ; int M = arr [ 0 ] . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { hash [ arr [ i ] . charAt ( j ) ] ++ ; } } for ( int i = 0 ; i < 256 ; i ++ ) { if ( hash [ i ] % N != 0 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String arr [ ] = { " fdd " , " fhh " } ; int N = arr . length ; if ( checkEqual ( arr , N ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Maximize sum of remaining elements after every removal of the array half with greater sum | Java program to implement the above approach ; Function to find the maximum sum ; Base Case ; Check if ( mapped key is found in the dictionary ; Traverse the array ; Store left prefix sum ; Store right prefix sum ; Compare the left and right values ; Store the value in dp array ; Return the final answer ; Function to print maximum sum ; Stores prefix sum ; Store results of subproblems ; Traversing the array ; Add prefix sum of array ; Print the answer ; Driver Code ; Function call
import java . util . * ; class solution { static int [ ] [ ] dp = new int [ 100 ] [ 100 ] ; static int maxweight ( int s , int e , HashMap < Integer , Integer > pre ) { if ( s == e ) return 0 ; if ( dp [ s ] [ e ] != - 1 ) return dp [ s ] [ e ] ; int ans = 0 ; for ( int i = s ; i < e ; i ++ ) { int left = pre . get ( i ) - pre . get ( s - 1 ) ; int right = pre . get ( e ) - pre . get ( i ) ; if ( left < right ) ans = Math . max ( ans , ( int ) ( left + maxweight ( s , i , pre ) ) ) ; if ( left == right ) ans = Math . max ( ans , Math . max ( left + maxweight ( s , i , pre ) , right + maxweight ( i + 1 , e , pre ) ) ) ; if ( left > right ) ans = Math . max ( ans , right + maxweight ( i + 1 , e , pre ) ) ; dp [ s ] [ e ] = ans ; } return dp [ s ] [ e ] ; } static void maxSum ( int arr [ ] , int n ) { HashMap < Integer , Integer > pre = new HashMap < Integer , Integer > ( ) ; pre . put ( - 1 , 0 ) ; pre . put ( 0 , arr [ 0 ] ) ; for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 100 ; j ++ ) dp [ i ] [ j ] = - 1 ; } for ( int i = 0 ; i < n ; i ++ ) pre . put ( i , pre . get ( i - 1 ) + arr [ i ] ) ; System . out . print ( ( maxweight ( 0 , n - 1 , pre ) ) ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 6 , 2 , 3 , 4 , 5 , 5 } ; maxSum ( arr , 6 ) ; } }
Find K smallest leaf nodes from a given Binary Tree | Java program of the above approach ; Structure of binary tree node ; Function to create new node ; Utility function which calculates smallest three nodes of all leaf nodes ; Check if current root is a leaf node ; Traverse the left and right subtree ; Function to find the K smallest nodes of the Binary Tree ; Sorting the Leaf nodes array ; Loop to print the K smallest Leaf nodes of the array ; Driver Code ; Construct binary tree ; Function call
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Vector < Integer > storeLeaf ( Node root , Vector < Integer > arr ) { if ( root == null ) return arr ; if ( root . left == null && root . right == null ) { arr . add ( root . data ) ; return arr ; } arr = storeLeaf ( root . left , arr ) ; arr = storeLeaf ( root . right , arr ) ; return arr ; } static void KSmallest ( Node root , int k ) { Vector < Integer > arr = new Vector < Integer > ( ) ; arr = storeLeaf ( root , arr ) ; Collections . sort ( arr ) ; for ( int i = 0 ; i < k ; i ++ ) { if ( i < arr . size ( ) ) { System . out . print ( arr . get ( i ) + " ▁ " ) ; } else { break ; } } } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 4 ) ; root . left . left . left = newNode ( 21 ) ; root . left . right = newNode ( 5 ) ; root . left . right . right = newNode ( 8 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . right . right = newNode ( 19 ) ; KSmallest ( root , 3 ) ; } }
Check if all the pairs of an array are coprime with each other | Java implementation of the above approach ; Function to check if all the pairs of the array are coprime with each other or not ; Check if GCD of the pair is not equal to 1 ; All pairs are non - coprime Return false ; Function return gcd of two number ; Driver Code
import java . util . * ; import java . lang . * ; class GFG { static boolean allCoprime ( int A [ ] , int n ) { boolean all_coprime = true ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( gcd ( A [ i ] , A [ j ] ) != 1 ) { all_coprime = false ; break ; } } } return all_coprime ; } static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 5 , 11 , 7 , 19 } ; int arr_size = A . length ; if ( allCoprime ( A , arr_size ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Smallest power of 2 consisting of N digits | Java Program of the above approach ; Function to return smallest power of 2 consisting of N digits ; calculate log2 N indirectly using log ( ) method ; Driver Code
class GFG { static int smallestNum ( int n ) { double power = log2 ( 10 ) ; return ( int ) Math . ceil ( ( n - 1 ) * power ) ; } static double log2 ( int N ) { return ( Math . log ( N ) / Math . log ( 2 ) ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( smallestNum ( n ) ) ; } }
Find a valid parenthesis sequence of length K from a given valid parenthesis sequence | Java program for the above approach ; Function to find the subsequence of length K forming valid sequence ; Stores the resultant String ; Check whether character at index i is visited or not ; Traverse the String ; Push index of open bracket ; Pop and mark visited ; Increment count by 2 ; Append the characters and create the resultant String ; Return the resultant String ; Driver Code ; Function call
import java . util . * ; class GFG { static String findString ( String s , int k ) { int n = s . length ( ) ; String ans = " ▁ " ; Stack < Integer > st = new Stack < > ( ) ; boolean [ ] vis = new boolean [ n ] ; Vector < boolean > vis ( n , false ) ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s . charAt ( i ) == ' ( ' ) { st . add ( i ) ; } if ( count < k && s . charAt ( i ) == ' ) ' ) { vis [ st . peek ( ) ] = true ; st . pop ( ) ; vis [ i ] = true ; count += 2 ; } } for ( int i = 0 ; i < n ; ++ i ) { if ( vis [ i ] == true ) { ans += s . charAt ( i ) ; } } return ans ; } public static void main ( String [ ] args ) { String s = " ( ) ( ) ( ) " ; int K = 2 ; System . out . print ( findString ( s , K ) ) ; } }
Node having maximum number of nodes less than its value in its subtree | Java program for the above approach ; Stores the nodes to be deleted ; Structure of a Tree node ; Function to create a new node ; Function to compare the current node key with keys received from it left & right tree by Post Order traversal ; Base Case ; Find nodes lesser than the current root in the left subtree ; Find nodes lesser than the current root in the right subtree ; Stores all the nodes less than the current node 's ; Add the nodes which are less than current node in left [ ] ; Add the nodes which are less than current node in right [ ] ; Create a combined vector for pass to it 's parent ; Stores key that has maximum nodes ; Return the vector of nodes ; Driver Code ; Given Tree ; Function Call ; Print the node value
import java . util . * ; class GFG { static HashMap < Integer , Boolean > mp = new HashMap < Integer , Boolean > ( ) ; static int max_v , rootIndex ; static class Node { int key ; Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static Vector < Integer > findNodes ( Node root ) { if ( root == null ) { return new Vector < Integer > ( ) ; } Vector < Integer > left = findNodes ( root . left ) ; Vector < Integer > right = findNodes ( root . right ) ; Vector < Integer > combined = new Vector < Integer > ( ) ; int count = 0 ; for ( int i = 0 ; i < left . size ( ) ; i ++ ) { if ( left . get ( i ) < root . key ) { count += 1 ; } combined . add ( left . get ( i ) ) ; } for ( int i = 0 ; i < right . size ( ) ; i ++ ) { if ( right . get ( i ) < root . key ) { count += 1 ; } combined . add ( right . get ( i ) ) ; } combined . add ( root . key ) ; if ( count > max_v ) { rootIndex = root . key ; max_v = count ; } return combined ; } public static void main ( String [ ] args ) { Node root = newNode ( 3 ) ; root . left = newNode ( 4 ) ; root . right = newNode ( 6 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; root . left . left = newNode ( 10 ) ; root . left . right = newNode ( 2 ) ; max_v = 0 ; rootIndex = - 1 ; findNodes ( root ) ; System . out . print ( rootIndex ) ; } }
Maximum sum path in a matrix from top | Java program for the above approach ; Function to find the maximum sum path in the grid ; Dimensions of grid [ ] [ ] ; Stores maximum sum at each cell sum [ i ] [ j ] from cell sum [ 0 ] [ 0 ] ; Iterate to compute the maximum sum path in the grid ; Update the maximum path sum ; Return the maximum sum ; Driver Code
import java . util . * ; class GFG { static int MaximumPath ( int [ ] [ ] grid ) { int N = grid . length ; int M = grid [ 0 ] . length ; int [ ] [ ] sum = new int [ N + 1 ] [ M + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { sum [ i ] [ j ] = Math . max ( sum [ i - 1 ] [ j ] , sum [ i ] [ j - 1 ] ) + grid [ i - 1 ] [ j - 1 ] ; } } return sum [ N ] [ M ] ; } public static void main ( String [ ] args ) { int [ ] [ ] grid = { { 1 , 2 } , { 3 , 5 } } ; System . out . print ( MaximumPath ( grid ) ) ; } }
Maximize cost of deletions to obtain string having no pair of similar adjacent characters | Java program for the above approach ; Function to find maximum cost to remove consecutive characters ;  ; Maximum cost ; Traverse from 0 to len ( s ) - 2 ; If characters are identical ; Add cost [ i ] if its maximum ; Add cost [ i + 1 ] if its maximum ; Increment i ; Return the final max count ; Driver code ; Given string s ; Given cost of removal ; Function call
import java . io . * ; import java . util . * ; class GFG { static int Maxcost ( String s , int [ ] cost ) { ' ' ▁ Initialize ▁ the ▁ count ' '' int maxcost = 0 ; int i = 0 ; while ( i < s . length ( ) - 1 ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) { if ( cost [ i ] > cost [ i + 1 ] ) maxcost += cost [ i ] ; else { maxcost += cost [ i + 1 ] ; cost [ i + 1 ] = cost [ i ] ; } } i ++ ; } return maxcost ; } public static void main ( String [ ] args ) { String s = " abaac " ; int [ ] cost = { 1 , 2 , 3 , 4 , 5 } ; System . out . print ( Maxcost ( s , cost ) ) ; } }
Maximum count of values of S modulo M lying in a range [ L , R ] after performing given operations on the array | Java program for the above approach ; Lookup table ; Function to count the value of S after adding arr [ i ] or arr [ i - 1 ] to the sum S at each time ; Base Case ; Store the mod value ; If the mod value lies in the range then return 1 ; Else return 0 ; Store the current state ; If already computed , return the computed value ; Recursively adding the elements to the sum adding ai value ; Adding arr [ i ] - 1 value ; Return the maximum count to check for root value as well ; Avoid counting idx = 0 as possible solution we are using idx != 0 ; Return the value of current state ; Driver Code
import java . util . * ; import java . awt . Point ; public class Main { static HashMap < Point , Integer > dp = new HashMap < Point , Integer > ( ) ; static int countMagicNumbers ( int idx , int sum , int [ ] a , int n , int m , int l , int r ) { if ( idx == n ) { int Temp = sum % m ; if ( Temp == l || Temp == r || ( Temp > l && Temp < r ) ) { dp . put ( new Point ( idx , sum ) , 1 ) ; return dp . get ( new Point ( idx , sum ) ) ; } else { dp . put ( new Point ( idx , sum ) , 0 ) ; return dp . get ( new Point ( idx , sum ) ) ; } } Point curr = new Point ( idx , sum ) ; if ( dp . containsKey ( curr ) ) return dp . get ( curr ) ; int ls = countMagicNumbers ( idx + 1 , sum + a [ idx ] , a , n , m , l , r ) ; int rs = countMagicNumbers ( idx + 1 , sum + ( a [ idx ] - 1 ) , a , n , m , l , r ) ; int temp1 = Math . max ( ls , rs ) ; int temp = sum % m ; if ( ( temp == l || temp == r || ( temp > l && temp < r ) ) && idx != 0 ) { temp1 += 1 ; } dp . put ( new Point ( idx , sum ) , temp1 ) ; return dp . get ( new Point ( idx , sum ) ) ; } public static void main ( String [ ] args ) { int N = 5 , M = 22 , L = 14 , R = 16 ; int [ ] arr = { 17 , 11 , 10 , 8 , 15 } ; System . out . print ( countMagicNumbers ( 0 , 0 , arr , N , M , L , R ) ) ; } }