codeparrot/xlcost-text-to-code huggingface dataset:
Program to find minimum number of lectures to attend to maintain 75 % | Java Program to find minimum number of lectures to attend to maintain 75 % attendance ; Method to compute minimum lecture ; Formula to compute ; Driver Code
public class GFG { static int minimumLectures ( int m , int n ) { int ans = 0 ; if ( n < ( int ) Math . ceil ( 0.75 * m ) ) ans = ( int ) Math . ceil ( ( ( 0.75 * m ) - n ) / 0.25 ) ; else ans = 0 ; return ans ; } public static void main ( String [ ] args ) { int M = 9 , N = 1 ; System . out . println ( minimumLectures ( M , N ) ) ; } }
Count Numbers with N digits which consists of odd number of 0 's | Java program to count numbers with N digits which consists of odd number of 0 's ; Function to count Numbers with N digits which consists of odd number of 0 's ; Driver code
import java . io . * ; class GFG { static int countNumbers ( int N ) { return ( int ) ( Math . pow ( 10 , N ) - Math . pow ( 8 , N ) ) / 2 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( countNumbers ( n ) ) ; } }
Sum of all Primes in a given range using Sieve of Eratosthenes | Java program to find sum of primes in range L to R ; prefix [ i ] is going to store sum of primes till i ( including i ) . ; Function to build the prefix sum array ; Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Build prefix array ; Function to return sum of prime in range ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static final int MAX = 10000 ; static int prefix [ ] = new int [ MAX + 1 ] ; static void buildPrefix ( ) { boolean prime [ ] = new boolean [ MAX + 1 ] ; for ( int i = 0 ; i < MAX + 1 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } prefix [ 0 ] = prefix [ 1 ] = 0 ; for ( int p = 2 ; p <= MAX ; p ++ ) { prefix [ p ] = prefix [ p - 1 ] ; if ( prime [ p ] == true ) prefix [ p ] += p ; } } static int sumPrimeRange ( int L , int R ) { buildPrefix ( ) ; return prefix [ R ] - prefix [ L - 1 ] ; } public static void main ( String args [ ] ) { int L = 10 , R = 20 ; System . out . println ( sumPrimeRange ( L , R ) ) ; } }
Sum of the first N terms of the series 5 , 12 , 23 , 38. ... | Java program to find sum of first n terms ; Function to calculate the sum ; Driver code ; number of terms to be included in sum ; find the Sn
import java . io . * ; class GFG { static int calculateSum ( int n ) { return 2 * ( n * ( n + 1 ) * ( 2 * n + 1 ) / 6 ) + n * ( n + 1 ) / 2 + 2 * ( n ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( " Sum ▁ = ▁ " + calculateSum ( n ) ) ; } }
Program to find number of solutions in Quadratic Equation | Java Program to find the solutions of specified equations ; Method to check for solutions of equations ; If the expression is greater than 0 , then 2 solutions ; If the expression is equal 0 , then 2 solutions ; Else no solutions ; Driver Code
public class GFG { static void checkSolution ( int a , int b , int c ) { if ( ( ( b * b ) - ( 4 * a * c ) ) > 0 ) System . out . println ( "2 ▁ solutions " ) ; else if ( ( ( b * b ) - ( 4 * a * c ) ) == 0 ) System . out . println ( "1 ▁ solution " ) ; else System . out . println ( " No ▁ solutions " ) ; } public static void main ( String [ ] args ) { int a = 2 , b = 5 , c = 2 ; checkSolution ( a , b , c ) ; } }
Program to convert KiloBytes to Bytes and Bits | Java implementation of above program ; Function to calculates the bits ; calculates Bits 1 kilobytes ( s ) = 8192 bits ; Function to calculates the bytes ; calculates Bytes 1 KB = 1024 bytes ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; import java . math . BigInteger ; class GFG { static BigInteger Bits ( int kilobytes ) { BigInteger Bits = new BigInteger ( "0" ) ; BigInteger kilo = BigInteger . valueOf ( kilobytes ) ; Bits = kilo . multiply ( BigInteger . valueOf ( 8192 ) ) ; return Bits ; } static BigInteger Bytes ( int kilobytes ) { BigInteger Bytes = new BigInteger ( "0" ) ; BigInteger kilo = BigInteger . valueOf ( kilobytes ) ; Bytes = kilo . multiply ( BigInteger . valueOf ( 1024 ) ) ; return Bytes ; } public static void main ( String args [ ] ) { int kilobytes = 1 ; System . out . print ( kilobytes + " ▁ Kilobytes ▁ = ▁ " + Bytes ( kilobytes ) + " ▁ Bytes ▁ and ▁ " + Bits ( kilobytes ) + " ▁ Bits . " ) ; } }
Program to find the Hidden Number | Java Program to find the hidden number ; Driver Code ; Getting the size of array ; Getting the array of size n ; Solution ; Finding sum of the array elements ; Dividing sum by size n ; Print x , if found
public class GFG { public static void main ( String args [ ] ) { int n = 3 ; int a [ ] = { 1 , 2 , 3 } ; int i = 0 ; long sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } long x = sum / n ; if ( x * n == sum ) System . out . println ( x ) ; else System . out . println ( " - 1" ) ; } }
Find sum of the series ? 3 + ? 12 + ... ... ... upto N terms | Java implementation of above approach ; Function to find the sum ; Apply AP formula ; Driver code ; number of terms
import java . io . * ; class GFG { static double findSum ( long n ) { return Math . sqrt ( 3 ) * ( n * ( n + 1 ) / 2 ) ; } public static void main ( String [ ] args ) { long n = 10 ; System . out . println ( findSum ( n ) ) ; } }
Find the sum of the series x ( x + y ) + x ^ 2 ( x ^ 2 + y ^ 2 ) + x ^ 3 ( x ^ 3 + y ^ 3 ) + ... + x ^ n ( x ^ n + y ^ n ) | Java program to find the sum of series ; Function to return required sum ; sum of first series ; sum of second series ; Driver code ; function call to print sum
public class GFG { static int sum ( int x , int y , int n ) { int sum1 = ( int ) ( ( Math . pow ( x , 2 ) * ( Math . pow ( x , 2 * n ) - 1 ) ) / ( Math . pow ( x , 2 ) - 1 ) ) ; int sum2 = ( int ) ( ( x * y * ( Math . pow ( x , n ) * Math . pow ( y , n ) - 1 ) ) / ( x * y - 1 ) ) ; return sum1 + sum2 ; } public static void main ( String args [ ] ) { int x = 2 , y = 2 , n = 2 ; System . out . println ( sum ( x , y , n ) ) ; } }
Find any pair with given GCD and LCM | Java program to print any pair with a given gcd G and lcm L ; Function to print the pairs ; Driver Code
import java . io . * ; class GFG { static void printPair ( int g , int l ) { System . out . print ( g + " ▁ " + l ) ; } public static void main ( String [ ] args ) { int g = 3 , l = 12 ; printPair ( g , l ) ; } }
Sum of first n terms of a given series 3 , 6 , 11 , ... . . | Java program to find sum of first n terms ; Function to calculate the sum ; starting number ; Common Ratio ; Common difference ; Driver code ; N th term to be find ; find the Sn
import java . io . * ; class GFG { static int calculateSum ( int n ) { int a1 = 1 , a2 = 2 ; int r = 2 ; int d = 1 ; return ( n ) * ( 2 * a1 + ( n - 1 ) * d ) / 2 + a2 * ( int ) ( Math . pow ( r , n ) - 1 ) / ( r - 1 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( " Sum ▁ = ▁ " + calculateSum ( n ) ) ; } }
Maximum of sum and product of digits until number is reduced to a single digit | Java implementation of above approach ; Function to sum the digits until it becomes a single digit ; Function to product the digits until it becomes a single digit ; Loop to do sum while sum is not less than or equal to 9 ; Function to find the maximum among repeated sum and repeated product ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static long repeatedSum ( long n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } public static long repeatedProduct ( long n ) { long prod = 1 ; while ( n > 0 prod > 9 ) { if ( n == 0 ) { n = prod ; prod = 1 ; } prod *= n % 10 ; n /= 10 ; } return prod ; } public static long maxSumProduct ( long N ) { if ( N < 10 ) return N ; return Math . max ( repeatedSum ( N ) , repeatedProduct ( N ) ) ; } public static void main ( String [ ] args ) { long n = 631 ; System . out . println ( maxSumProduct ( n ) ) ; } }
Find maximum possible value of advertising | Java program for the above approach ; Function to find maximum possible advertising value ; To store advertising value at i - th minute ; Base Case ; If no advertisement is taken on ith minute ; If advertisement is taken on i - th minute ; Driver code ; array [ ] [ 0 ] start time array [ ] [ 1 ] advertising value
import java . util . * ; import java . lang . * ; class GFG { static int max_value ( int array [ ] [ ] , int M , int K , int N ) { int [ ] time = new int [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { time [ array [ i ] [ 0 ] ] = array [ i ] [ 1 ] ; } int [ ] [ ] dp = new int [ M ] [ 2 ] ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = time [ 0 ] ; for ( int i = 1 ; i < M ; i ++ ) { dp [ i ] [ 0 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; dp [ i ] [ 1 ] = time [ i ] ; if ( i - K >= 0 ) { dp [ i ] [ 1 ] += Math . max ( dp [ i - K ] [ 0 ] , dp [ i - K ] [ 1 ] ) ; } } return Math . max ( dp [ M - 1 ] [ 0 ] , dp [ M - 1 ] [ 1 ] ) ; } public static void main ( String [ ] args ) { int array [ ] [ ] = { { 0 , 10 } , { 4 , 110 } , { 5 , 30 } } ; int N = 3 ; int K = 4 ; int M = 6 ; System . out . println ( max_value ( array , M , K , N ) ) ; } }
Count numbers with exactly K non | Java program to count the numbers having exactly K non - zero digits and sum of digits are odd and distinct . ; To store digits of N ; visited map ; DP Table ; Push all the digits of N into digits vector ; Function returns the count ; If desired number is formed whose sum is odd ; If it is not present in map , mark it as true and return 1 ; Sum is present in map already ; Desired result not found ; If that state is already calculated just return that state value ; Upper limit ; To store the count of desired numbers ; If k is non - zero , i ranges from 0 to j else [ 1 , j ] ; If current digit is 0 , decrement k and recurse sum is not changed as we are just adding 0 that makes no difference ; If i is non zero , then k remains unchanged and value is added to sum ; Memoize and return ; Driver code ; K is the number of exact non - zero elements to have in number ; break N into its digits ; We keep record of 0 s we need to place in the number
import java . util . * ; class GFG { static Vector < Integer > digits = new Vector < Integer > ( ) ; static boolean [ ] vis = new boolean [ 170 ] ; static int [ ] [ ] [ ] [ ] dp = new int [ 19 ] [ 19 ] [ 2 ] [ 170 ] ; static void ConvertIntoDigit ( int n ) { while ( n > 0 ) { int dig = n % 10 ; digits . add ( dig ) ; n /= 10 ; } Collections . reverse ( digits ) ; } static int solve ( int idx , int k , int tight , int sum ) { if ( idx == digits . size ( ) && k == 0 && sum % 2 == 1 ) { if ( ! vis [ sum ] ) { vis [ sum ] = true ; return 1 ; } return 0 ; } if ( idx > digits . size ( ) ) { return 0 ; } if ( dp [ idx ] [ k ] [ tight ] [ sum ] > 0 ) { return dp [ idx ] [ k ] [ tight ] [ sum ] ; } int j ; if ( idx < digits . size ( ) && tight == 0 ) { j = digits . get ( idx ) ; } else { j = 9 ; } int cnt = 0 ; for ( int i = ( k > 0 ? 0 : 1 ) ; i <= j ; i ++ ) { int newtight = tight ; if ( i < j ) { newtight = 1 ; } if ( i == 0 ) cnt += solve ( idx + 1 , k - 1 , newtight , sum ) ; else cnt += solve ( idx + 1 , k , newtight , sum + i ) ; } return dp [ idx ] [ k ] [ tight ] [ sum ] = cnt ; } public static void main ( String [ ] args ) { int N , k ; N = 169 ; k = 2 ; ConvertIntoDigit ( N ) ; k = digits . size ( ) - k ; System . out . print ( solve ( 0 , k , 0 , 0 ) ) ; } }
Count of subsets of integers from 1 to N having no adjacent elements | Java code to count subsets not containing adjacent elements from 1 to N ; Function to count subsets ; Driver code
class GFG { static int countSubsets ( int N ) { if ( N <= 2 ) return N ; if ( N == 3 ) return 2 ; int [ ] DP = new int [ N + 1 ] ; DP [ 0 ] = 0 ; DP [ 1 ] = 1 ; DP [ 2 ] = 2 ; DP [ 3 ] = 2 ; for ( int i = 4 ; i <= N ; i ++ ) { DP [ i ] = DP [ i - 2 ] + DP [ i - 3 ] ; } return DP [ N ] ; } public static void main ( String [ ] args ) { int N = 20 ; System . out . print ( countSubsets ( N ) ) ; } }
Count the number of ordered sets not containing consecutive numbers | Java program to count the number of ordered sets not containing consecutive numbers ; DP table ; Function to calculate the count of ordered set for a given size ; Base cases ; If subproblem has been soved before ; Store and return answer to this subproblem ; Function returns the count of all ordered sets ; Prestore the factorial value ; Initialise the dp table ; Iterate all ordered set sizes and find the count for each one maximum ordered set size will be smaller than N as all elements are distinct and non consecutive . ; Multiply ny size ! for all the arrangements because sets are ordered . ; Add to total answer ; Driver code
import java . util . * ; class GFG { static int [ ] [ ] dp = new int [ 500 ] [ 500 ] ; static int CountSets ( int x , int pos ) { if ( x <= 0 ) { if ( pos == 0 ) return 1 ; else return 0 ; } if ( pos == 0 ) return 1 ; if ( dp [ x ] [ pos ] != - 1 ) return dp [ x ] [ pos ] ; int answer = CountSets ( x - 1 , pos ) + CountSets ( x - 2 , pos - 1 ) ; return dp [ x ] [ pos ] = answer ; } static int CountOrderedSets ( int n ) { int [ ] factorial = new int [ 10000 ] ; factorial [ 0 ] = 1 ; for ( int i = 1 ; i < 10000 ; i ++ ) factorial [ i ] = factorial [ i - 1 ] * i ; int answer = 0 ; for ( int i = 0 ; i < 500 ; i ++ ) { for ( int j = 0 ; j < 500 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { int sets = CountSets ( n , i ) * factorial [ i ] ; answer = answer + sets ; } return answer ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( CountOrderedSets ( N ) ) ; } }
Count the Arithmetic sequences in the Array of size at least 3 | Java program to find all arithmetic sequences of size atleast 3 ; Function to find all arithmetic sequences of size atleast 3 ; If array size is less than 3 ; Finding arithmetic subarray length ; To store all arithmetic subarray of length at least 3 ; Check if current element makes arithmetic sequence with previous two elements ; Begin with a new element for new arithmetic sequences ; Accumulate result in till i . ; Return final count ; Driver code ; Function to find arithmetic sequences
class GFG { static int numberOfArithmeticSequences ( int L [ ] , int N ) { if ( N <= 2 ) return 0 ; int count = 0 ; int res = 0 ; for ( int i = 2 ; i < N ; ++ i ) { if ( L [ i ] - L [ i - 1 ] == L [ i - 1 ] - L [ i - 2 ] ) { ++ count ; } else { count = 0 ; } res += count ; } return res ; } public static void main ( String [ ] args ) { int L [ ] = { 1 , 3 , 5 , 6 , 7 , 8 } ; int N = L . length ; System . out . print ( numberOfArithmeticSequences ( L , N ) ) ; } }
Count triplet of indices ( i , j , k ) such that XOR of elements between [ i , j ) equals [ j , k ] | Java program to count the Number of triplets in array having subarray XOR equal ; Function return the count of triplets having subarray XOR equal ; XOR value till i ; Count and ways array as defined above ; Using the formula stated ; Increase the frequency of x ; Add i + 1 to ways [ x ] for upcoming indices ; Driver code
import java . io . * ; import java . util . Arrays ; import java . util . ArrayList ; import java . lang . * ; import java . util . Collections ; class GFG { static int CountOfTriplets ( int a [ ] , int n ) { int answer = 0 ; int x = 0 ; int count [ ] = new int [ 100005 ] ; int ways [ ] = new int [ 100005 ] ; for ( int i = 0 ; i < n ; i ++ ) { x ^= a [ i ] ; answer += count [ x ] * i - ways [ x ] ; count [ x ] ++ ; ways [ x ] += ( i + 1 ) ; } return answer ; } public static void main ( String [ ] args ) { int Arr [ ] = { 3 , 6 , 12 , 8 , 6 , 2 , 1 , 5 } ; int N = Arr . length ; System . out . print ( CountOfTriplets ( Arr , N ) ) ; } }
Count of subarrays of an Array having all unique digits | Java program to find the count of subarrays of an Array having all unique digits ; Dynamic programming table ; Function to obtain the mask for any integer ; Function to count the number of ways ; Subarray must not be empty ; If subproblem has been solved ; Excluding this element in the subarray ; If there are no common digits then only this element can be included ; Calculate the new mask if this element is included ; Store and return the answer ; Function to find the count of subarray with all digits unique ; initializing dp ; Driver code
import java . util . * ; class GFG { static int [ ] [ ] dp = new int [ 5000 ] [ ( 1 << 10 ) + 5 ] ; static int getmask ( int val ) { int mask = 0 ; if ( val == 0 ) return 1 ; while ( val > 0 ) { int d = val % 10 ; mask |= ( 1 << d ) ; val /= 10 ; } return mask ; } static int countWays ( int pos , int mask , int a [ ] , int n ) { if ( pos == n ) return ( mask > 0 ? 1 : 0 ) ; if ( dp [ pos ] [ mask ] != - 1 ) return dp [ pos ] [ mask ] ; int count = 0 ; count = count + countWays ( pos + 1 , mask , a , n ) ; if ( ( getmask ( a [ pos ] ) & mask ) == 0 ) { int new_mask = ( mask | ( getmask ( a [ pos ] ) ) ) ; count = count + countWays ( pos + 1 , new_mask , a , n ) ; } return dp [ pos ] [ mask ] = count ; } static int numberOfSubarrays ( int a [ ] , int n ) { for ( int i = 0 ; i < 5000 ; i ++ ) { for ( int j = 0 ; j < ( 1 << 10 ) + 5 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return countWays ( 0 , 0 , a , n ) ; } public static void main ( String [ ] args ) { int N = 4 ; int A [ ] = { 1 , 12 , 23 , 34 } ; System . out . print ( numberOfSubarrays ( A , N ) ) ; } }
Count of Fibonacci paths in a Binary tree | Java program to count all of Fibonacci paths in a Binary tree ; Vector to store the fibonacci series ; Binary Tree Node ; Function to create a new tree node ; Function to find the height of the given tree ; Function to make fibonacci series upto n terms ; Preorder Utility function to count exponent path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is not a number of Math . pow ( x , y ) ; Increment count when encounter leaf node ; Left recursive call save the value of count ; Right recursive call and return value of count ; Function to find whether fibonacci path exists or not ; To find the height ; Making fibonacci series upto ht terms ; Driver code ; Create binary tree ; Function Call
import java . util . * ; class GFG { static Vector < Integer > fib = new Vector < Integer > ( ) ; static class node { node left ; int data ; node right ; } ; static node newNode ( int data ) { node temp = new node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static int height ( node root ) { if ( root == null ) return 0 ; return ( Math . max ( height ( root . left ) , height ( root . right ) ) + 1 ) ; } static void FibonacciSeries ( int n ) { fib . add ( 0 ) ; fib . add ( 1 ) ; for ( int i = 2 ; i < n ; i ++ ) fib . add ( fib . get ( i - 1 ) + fib . get ( i - 2 ) ) ; } static int CountPathUtil ( node root , int i , int count ) { if ( root == null || ! ( fib . get ( i ) == root . data ) ) { return count ; } if ( root . left != null && root . right != null ) { count ++ ; } count = CountPathUtil ( root . left , i + 1 , count ) ; return CountPathUtil ( root . right , i + 1 , count ) ; } static void CountPath ( node root ) { int ht = height ( root ) ; FibonacciSeries ( ht ) ; System . out . print ( CountPathUtil ( root , 0 , 0 ) ) ; } public static void main ( String [ ] args ) { node root = newNode ( 0 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 1 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 4 ) ; root . right . right = newNode ( 1 ) ; root . right . right . left = newNode ( 2 ) ; CountPath ( root ) ; } }
Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range | Java program to count the numbers in the range having the difference between the sum of digits at even and odd positions as a Fibonacci Number ; To store all the Fibonacci numbers ; Function to generate Fibonacci numbers upto 100 ; Adding the first two Fibonacci numbers in the set ; Computing the remaining Fibonacci numbers using the first two Fibonacci numbers ; Function to return the count of required numbers from 0 to num ; Base Case ; Check if the difference is equal to any fibonacci number ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 1 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; If the current position is odd add it to currOdd , otherwise to currEven ; Function to convert x into its digit vector and uses count ( ) function to return the required count ; Initialize dp ; Driver Code ; Generate fibonacci numbers
import java . util . * ; class GFG { static int M = 18 ; static int a , b ; static int [ ] [ ] [ ] [ ] dp = new int [ M ] [ 90 ] [ 90 ] [ 2 ] ; static HashSet < Integer > fib = new HashSet < Integer > ( ) ; static void fibonacci ( ) { int prev = 0 , curr = 1 ; fib . add ( prev ) ; fib . add ( curr ) ; while ( curr <= 100 ) { int temp = curr + prev ; fib . add ( temp ) ; prev = curr ; curr = temp ; } } static int count ( int pos , int even , int odd , int tight , Vector < Integer > num ) { if ( pos == num . size ( ) ) { if ( num . size ( ) % 2 == 1 ) { odd = odd + even ; even = odd - even ; odd = odd - even ; } int d = even - odd ; if ( fib . contains ( d ) ) return 1 ; return 0 ; } if ( dp [ pos ] [ even ] [ odd ] [ tight ] != - 1 ) return dp [ pos ] [ even ] [ odd ] [ tight ] ; int ans = 0 ; int limit = ( tight == 1 ? 9 : num . get ( pos ) ) ; for ( int d = 0 ; d <= limit ; d ++ ) { int currF = tight , currEven = even ; int currOdd = odd ; if ( d < num . get ( pos ) ) currF = 1 ; if ( pos % 2 == 1 ) currOdd += d ; else currEven += d ; ans += count ( pos + 1 , currEven , currOdd , currF , num ) ; } return dp [ pos ] [ even ] [ odd ] [ tight ] = ans ; } static int solve ( int x ) { Vector < Integer > num = new Vector < Integer > ( ) ; while ( x > 0 ) { num . add ( x % 10 ) ; x /= 10 ; } Collections . reverse ( num ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < 90 ; j ++ ) { for ( int l = 0 ; l < 90 ; l ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { dp [ i ] [ j ] [ l ] [ k ] = - 1 ; } } } } return count ( 0 , 0 , 0 , 0 , num ) ; } public static void main ( String [ ] args ) { fibonacci ( ) ; int L = 1 , R = 50 ; System . out . print ( solve ( R ) - solve ( L - 1 ) + "NEW_LINE"); L = 50 ; R = 100 ; System . out . print ( solve ( R ) - solve ( L - 1 ) + "NEW_LINE"); } }
Count maximum occurrence of subsequence in string such that indices in subsequence is in A . P . | Java implementation to find the maximum occurrence of the subsequence such that the indices of characters are in arithmetic progression ; Function to find the maximum occurrence of the subsequence such that the indices of characters are in arithmetic progression ; Frequency for characters ; Loop to count the occurrence of ith character before jth character in the given String ; Increase the frequency of s [ i ] or c of String ; Maximum occurrence of subsequence of length 1 in given String ; Maximum occurrence of subsequence of length 2 in given String ; Driver Code
class GFG { static int maximumOccurrence ( String s ) { int n = s . length ( ) ; int freq [ ] = new int [ 26 ] ; int dp [ ] [ ] = new int [ 26 ] [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { int c = ( s . charAt ( i ) - ' a ' ) ; for ( int j = 0 ; j < 26 ; j ++ ) dp [ j ] += freq [ j ] ; freq ++ ; } int answer = Integer . MIN_VALUE ; for ( int i = 0 ; i < 26 ; i ++ ) answer = Math . max ( answer , freq [ i ] ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { answer = Math . max ( answer , dp [ i ] [ j ] ) ; } } return answer ; } public static void main ( String [ ] args ) { String s = " xxxyy " ; System . out . print ( maximumOccurrence ( s ) ) ; } }