codeparrot/xlcost-text-to-code huggingface dataset:
Queries to count characters having odd frequency in a range [ L , R ] | Java Program to implement the above problem ; Function to print the number of characters having odd frequencies for each query ; A function to construct the arr [ ] and prefix [ ] ; Stores array length ; Stores the unique powers of 2 associated to each character ; Prefix array to store the XOR values from array elements ; Driver Code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void queryResult ( int prefix [ ] , pair Q ) { int l = Q . first ; int r = Q . second ; if ( l == 0 ) { int xorval = prefix [ r ] ; System . out . print ( Integer . bitCount ( xorval ) + "NEW_LINE"); } else { int xorval = prefix [ r ] ^ prefix [ l - 1 ] ; System . out . print ( Integer . bitCount ( xorval ) + "NEW_LINE"); } } static void calculateCount ( String S , pair Q [ ] , int m ) { int n = S . length ( ) ; int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = ( 1 << ( S . charAt ( i ) - ' a ' ) ) ; } int [ ] prefix = new int [ n ] ; int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { x ^= arr [ i ] ; prefix [ i ] = x ; } for ( int i = 0 ; i < m ; i ++ ) { queryResult ( prefix , Q [ i ] ) ; } } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; pair Q [ ] = { new pair ( 2 , 4 ) , new pair ( 0 , 3 ) , new pair ( 0 , 12 ) } ; calculateCount ( S , Q , 3 ) ; } }
Count of Binary Strings possible as per given conditions | Java program to implement the above approach ; Function to generate maximum possible strings that can be generated ; Maximum possible strings ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static long countStrings ( long A , long B , long K ) { long X = ( A + B ) / ( K + 1 ) ; return ( Math . min ( A , Math . min ( B , X ) ) * ( K + 1 ) ) ; } public static void main ( String [ ] args ) { long N = 101 , M = 231 , K = 15 ; System . out . print ( countStrings ( N , M , K ) ) ; } }
Construct a Matrix N x N with first N ^ 2 natural numbers for an input N | Java program for the above approach ; Function to print the desired matrix ; Iterate ove all [ 0 , N ] ; If is even ; If row number is even print the row in forward order ; If row number is odd print the row in reversed order ; Driver Code ; Given matrix size ; Function call
public class Gfg { public static void UniqueMatrix ( int N ) { int element_value = 1 ; int i = 0 ; while ( i < N ) { if ( i % 2 == 0 ) { for ( int f = element_value ; f < element_value + N ; f ++ ) { System . out . print ( f + " ▁ " ) ; } element_value += N ; } else { for ( int k = element_value + N - 1 ; k > element_value - 1 ; k -- ) { System . out . print ( k + " ▁ " ) ; } element_value += N ; } System . out . println ( ) ; i = i + 1 ; } } public static void main ( String [ ] args ) { int N = 4 ; UniqueMatrix ( N ) ; } }
Maximum and minimum of an array using minimum number of comparisons | Java program of above implementation ; Class Pair is used to return two values from getMinMax ( ) ; If there is only one element then return it as min and max both ; If there are more than one elements , then initialize min and max ; Driver program to test above function
public class GFG { static class Pair { int min ; int max ; } static Pair getMinMax ( int arr [ ] , int n ) { Pair minmax = new Pair ( ) ; int i ; if ( n == 1 ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 0 ] ; return minmax ; } if ( arr [ 0 ] > arr [ 1 ] ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 1 ] ; } else { minmax . max = arr [ 1 ] ; minmax . min = arr [ 0 ] ; } for ( i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > minmax . max ) { minmax . max = arr [ i ] ; } else if ( arr [ i ] < minmax . min ) { minmax . min = arr [ i ] ; } } return minmax ; } public static void main ( String args [ ] ) { int arr [ ] = { 1000 , 11 , 445 , 1 , 330 , 3000 } ; int arr_size = 6 ; Pair minmax = getMinMax ( arr , arr_size ) ; System . out . printf ( " Minimum element is % d ", minmax.min); System . out . printf ( " Maximum element is % d ", minmax.max); } }
Find sum of all nodes of the given perfect binary tree | Java program to implement the above approach ; function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; list of vector to store nodes of all of the levels ; initialize ; store the nodes of last level i . e . , the leaf nodes ; store nodes of rest of the level by moving in bottom - up manner ; loop to calculate values of parent nodes from the children nodes of lower level ; store the value of parent node as sum of children nodes ; traverse the list of vector and calculate the sum ; Driver Code
import java . util . * ; class GFG { static int sumNodes ( int l ) { int leafNodeCount = ( int ) Math . pow ( 2 , l - 1 ) ; Vector < Vector < Integer > > vec = new Vector < Vector < Integer > > ( ) ; for ( int i = 1 ; i <= l ; i ++ ) vec . add ( new Vector < Integer > ( ) ) ; for ( int i = 1 ; i <= leafNodeCount ; i ++ ) vec . get ( l - 1 ) . add ( i ) ; for ( int i = l - 2 ; i >= 0 ; i -- ) { int k = 0 ; while ( k < vec . get ( i + 1 ) . size ( ) - 1 ) { vec . get ( i ) . add ( vec . get ( i + 1 ) . get ( k ) + vec . get ( i + 1 ) . get ( k + 1 ) ) ; k += 2 ; } } int sum = 0 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 0 ; j < vec . get ( i ) . size ( ) ; j ++ ) sum += vec . get ( i ) . get ( j ) ; } return sum ; } public static void main ( String args [ ] ) { int l = 3 ; System . out . println ( sumNodes ( l ) ) ; } }
Maximum and minimum of an array using minimum number of comparisons | Java program of above implementation ; Class Pair is used to return two values from getMinMax ( ) ; If array has even number of elements then initialize the first two elements as minimum and maximum ; set the starting index for loop ; If array has odd number of elements then initialize the first element as minimum and maximum ; set the starting index for loop ; In the while loop , pick elements in pair and compare the pair with max and min so far ; Increment the index by 2 as two elements are processed in loop ; Driver program to test above function
public class GFG { static class Pair { int min ; int max ; } static Pair getMinMax ( int arr [ ] , int n ) { Pair minmax = new Pair ( ) ; int i ; if ( n % 2 == 0 ) { if ( arr [ 0 ] > arr [ 1 ] ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 1 ] ; } else { minmax . min = arr [ 0 ] ; minmax . max = arr [ 1 ] ; } i = 2 ; } else { minmax . min = arr [ 0 ] ; minmax . max = arr [ 0 ] ; i = 1 ; } while ( i < n - 1 ) { if ( arr [ i ] > arr [ i + 1 ] ) { if ( arr [ i ] > minmax . max ) { minmax . max = arr [ i ] ; } if ( arr [ i + 1 ] < minmax . min ) { minmax . min = arr [ i + 1 ] ; } } else { if ( arr [ i + 1 ] > minmax . max ) { minmax . max = arr [ i + 1 ] ; } if ( arr [ i ] < minmax . min ) { minmax . min = arr [ i ] ; } } i += 2 ; } return minmax ; } public static void main ( String args [ ] ) { int arr [ ] = { 1000 , 11 , 445 , 1 , 330 , 3000 } ; int arr_size = 6 ; Pair minmax = getMinMax ( arr , arr_size ) ; System . out . printf ( " Minimum element is % d ", minmax.min); System . out . printf ( " Maximum element is % d ", minmax.max); } }
MO 's Algorithm (Query Square Root Decomposition) | Set 1 (Introduction) | Java Program to compute sum of ranges for different range queries . ; Class to represent a query range ; Prints sum of all query ranges . m is number of queries n is the size of the array . ; One by one compute sum of all queries ; Left and right boundaries of current range ; Compute sum of current query range ; Print sum of current query range ; Driver program
import java . util . * ; class Query { int L ; int R ; Query ( int L , int R ) { this . L = L ; this . R = R ; } } class GFG { static void printQuerySums ( int a [ ] , int n , ArrayList < Query > q , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int L = q . get ( i ) . L , R = q . get ( i ) . R ; int sum = 0 ; for ( int j = L ; j <= R ; j ++ ) sum += a [ j ] ; System . out . println ( " Sum ▁ of ▁ [ " + L + " , ▁ " + R + " ] ▁ is ▁ " + sum ) ; } } public static void main ( String argv [ ] ) { int a [ ] = { 1 , 1 , 2 , 1 , 3 , 4 , 5 , 2 , 8 } ; int n = a . length ; ArrayList < Query > q = new ArrayList < Query > ( ) ; q . add ( new Query ( 0 , 4 ) ) ; q . add ( new Query ( 1 , 3 ) ) ; q . add ( new Query ( 2 , 4 ) ) ; int m = q . size ( ) ; printQuerySums ( a , n , q , m ) ; } }
Count of unique palindromic strings of length X from given string | Java implementation to count different palindromic string of length X from the given string S ; Function to count different palindromic string of length X from the given string S ; Base case ; Create the frequency array ; Intitalise frequency array with 0 ; Count the frequency in the string ; Store frequency of the char ; Check the frequency which is greater than zero ; No . of different char we can put at the position of the i and x - i ; Iterator pointing to the last element of the set ; Decrease the value of the char we put on the position i and n - i ; Different no of char we can put at the position x / 2 ; Return total no of different string ; Driver code
import java . util . Arrays ; import java . util . HashSet ; import java . util . Set ; class GFG { static int findways ( String s , int x ) { if ( x > s . length ( ) ) return 0 ; int n = s . length ( ) ; int [ ] freq = new int [ 26 ] ; Arrays . fill ( freq , 0 ) ; for ( int i = 0 ; i < n ; ++ i ) freq [ s . charAt ( i ) - ' a ' ] ++ ; multiset < int > se ; Set < Integer > se = new HashSet < > ( ) ; for ( int i = 0 ; i < 26 ; ++ i ) if ( freq [ i ] > 0 ) se . add ( freq [ i ] ) ; int ans = 1 ; for ( int i = 0 ; i < x / 2 ; ++ i ) { int count = 0 ; for ( int u : se ) { if ( u >= 2 ) count ++ ; } if ( count == 0 ) return 0 ; else ans = ans * count ; int p = ( int ) se . toArray ( ) [ se . size ( ) - 1 ] ; int val = p ; se . remove ( p ) ; if ( val > 2 ) se . add ( val - 2 ) ; } if ( x % 2 != 0 ) { int count = 0 ; for ( int u : se ) if ( u > 0 ) count ++ ; ans = ans * count ; } return ans ; } public static void main ( String [ ] args ) { String s = " aaa " ; int x = 2 ; System . out . println ( findways ( s , x ) ) ; } }
Min operations to reduce N to 1 by multiplying by A or dividing by B | Java program for the above approach ; Function to check if it is possible to convert a number N to 1 by a minimum use of the two operations ; For the Case b % a != 0 ; Check if n equal to 1 ; Check if n is not divisible by b ; Initialize a variable ' c ' ; Loop until n is divisible by b ; Count number of divisions ; Loop until n is divisible by c ; Count number of operations ; Check if n is reduced to 1 ; Count steps ; Return the total number of steps ; Driver Code ; Given n , a and b ; Function Call
import java . util . * ; class GFG { static int findIfPossible ( int n , int a , int b ) { if ( b % a != 0 ) { if ( n == 1 ) return 0 ; else if ( n % b != 0 ) return - 1 ; else return ( int ) n / b ; } int c = b / a ; int x = 0 , y = 0 ; while ( n % b == 0 ) { n = n / b ; x ++ ; } while ( n % c == 0 ) { n = n / c ; y ++ ; } if ( n == 1 ) { int total_steps = x + ( 2 * y ) ; return total_steps ; } else return - 1 ; } public static void main ( String s [ ] ) { int n = 48 ; int a = 3 , b = 12 ; System . out . println ( findIfPossible ( n , a , b ) ) ; } }
Sqrt ( or Square Root ) Decomposition Technique | Set 1 ( Introduction ) | Java program to demonstrate working of Square Root Decomposition . ; original array ; decomposed array ; block size ; Time Complexity : O ( 1 ) ; Time Complexity : O ( sqrt ( n ) ) ; traversing first block in range ; traversing completely overlapped blocks in range ; traversing last block in range ; Fills values in input [ ] ; initiating block pointer ; calculating size of block ; building the decomposed array ; entering next block incementing block pointer ; Driver code ; We have used separate array for input because the purpose of this code is to explain SQRT decomposition in competitive programming where we have multiple inputs .
import java . util . * ; class GFG { static int MAXN = 10000 ; static int SQRSIZE = 100 ; static int [ ] arr = new int [ MAXN ] ; static int [ ] block = new int [ SQRSIZE ] ; static int blk_sz ; static void update ( int idx , int val ) { int blockNumber = idx / blk_sz ; block [ blockNumber ] += val - arr [ idx ] ; arr [ idx ] = val ; } static int query ( int l , int r ) { int sum = 0 ; while ( l < r && l % blk_sz != 0 && l != 0 ) { sum += arr [ l ] ; l ++ ; } while ( l + blk_sz <= r ) { sum += block [ l / blk_sz ] ; l += blk_sz ; } while ( l <= r ) { sum += arr [ l ] ; l ++ ; } return sum ; } static void preprocess ( int input [ ] , int n ) { int blk_idx = - 1 ; blk_sz = ( int ) Math . sqrt ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = input [ i ] ; if ( i % blk_sz == 0 ) { blk_idx ++ ; } block [ blk_idx ] += arr [ i ] ; } } public static void main ( String [ ] args ) { int input [ ] = { 1 , 5 , 2 , 4 , 6 , 1 , 3 , 5 , 7 , 10 } ; int n = input . length ; preprocess ( input , n ) ; System . out . println ( " query ( 3 , ▁ 8 ) ▁ : ▁ " + query ( 3 , 8 ) ) ; System . out . println ( " query ( 1 , ▁ 6 ) ▁ : ▁ " + query ( 1 , 6 ) ) ; update ( 8 , 0 ) ; System . out . println ( " query ( 8 , ▁ 8 ) ▁ : ▁ " + query ( 8 , 8 ) ) ; } }
Find two numbers with sum N such that neither of them contains digit K | Java implementation of the above approach ; Count leading zeros ; It removes i characters starting from index 0 ; Check each digit of the N ; If digit is K break it ; For odd numbers ; Add D1 to A and D2 to B ; If the digit is not K , no need to break String D in A and 0 in B ; Remove leading zeros ; Print the answer ; Driver code
class GFG { static String removeLeadingZeros ( String str ) { int i = 0 ; int n = str . length ( ) ; while ( str . charAt ( i ) == '0' && i < n ) i ++ ; str = str . substring ( i ) ; return str ; } static void findPairs ( int sum , int K ) { String A , B ; A = " " ; B = " " ; String N = String . valueOf ( sum ) ; int n = N . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int D = N . charAt ( i ) - '0' ; if ( D == K ) { int D1 , D2 ; D1 = D / 2 ; D2 = D / 2 + D % 2 ; A = A + ( char ) ( D1 + '0' ) ; B = B + ( char ) ( D2 + '0' ) ; } else { A = A + ( char ) ( D + '0' ) ; B = B + '0' ; } } A = removeLeadingZeros ( A ) ; B = removeLeadingZeros ( B ) ; System . out . print ( A + " , ▁ " + B + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 33673 ; int K = 3 ; findPairs ( N , K ) ; } }
Range Minimum Query ( Square Root Decomposition and Sparse Table ) | Java program to do range minimum query in O ( 1 ) time with O ( n * n ) extra space and O ( n * n ) preprocessing time . ; lookup [ i ] [ j ] is going to store index of minimum value in arr [ i . . j ] ; Structure to represent a query range ; Fills lookup array lookup [ n ] [ n ] for all possible values of query ranges ; Initialize lookup [ ] [ ] for the intervals with length 1 ; Fill rest of the entries in bottom up manner ; To find minimum of [ 0 , 4 ] , we compare minimum of arr [ lookup [ 0 ] [ 3 ] ] with arr [ 4 ] . ; Prints minimum of given m query ranges in arr [ 0. . n - 1 ] ; Fill lookup table for all possible input queries ; One by one compute sum of all queries ; Left and right boundaries of current range ; Print sum of current query range ; Driver Code
import java . util . * ; class GFG { static int MAX = 500 ; static int [ ] [ ] lookup = new int [ MAX ] [ MAX ] ; static class Query { int L , R ; public Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static void preprocess ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i ] [ i ] = i ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ lookup [ i ] [ j - 1 ] ] < arr [ j ] ) lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] ; else lookup [ i ] [ j ] = j ; } } static void RMQ ( int arr [ ] , int n , Query q [ ] , int m ) { preprocess ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; System . out . println ( " Minimum ▁ of ▁ [ " + L + " , ▁ " + R + " ] ▁ is ▁ " + arr [ lookup [ L ] [ R ] ] ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = a . length ; Query q [ ] = { new Query ( 0 , 4 ) , new Query ( 4 , 7 ) , new Query ( 7 , 8 ) } ; int m = q . length ; RMQ ( a , n , q , m ) ; } }
Minimum salary hike for each employee such that no employee feels unfair | Java Program to find the minimum hike of each employee such that no adjacent employee feels unfair ; As hikes are positive integers , keeping minimum value ; Pass - 1 : compare with previous employee ; Pass - 2 : compare with Next employee ; Driver Code ; Function Call ; result -> [ 2 , 1 , 3 , 2 , 1 , 2 ] ; Function Call ; result -> [ 1 , 2 , 3 , 1 ] ; Function Call ; result -> [ 1 , 2 ]
import java . io . * ; import java . util . * ; class GFG { public static int [ ] findMinimumHike ( int [ ] ratings , int employees ) { int [ ] hikes = new int [ employees ] ; for ( int i = 0 ; i < employees ; i ++ ) hikes [ i ] = 1 ; for ( int i = 1 ; i < employees ; i ++ ) { if ( ratings [ i - 1 ] < ratings [ i ] && hikes [ i - 1 ] >= hikes [ i ] ) hikes [ i ] = hikes [ i - 1 ] + 1 ; } for ( int i = employees - 2 ; i >= 0 ; i -- ) { if ( ratings [ i ] > ratings [ i + 1 ] && hikes [ i + 1 ] >= hikes [ i ] ) hikes [ i ] = hikes [ i + 1 ] + 1 ; } return hikes ; } public static void main ( String [ ] args ) { int [ ] data = new int [ ] { 5 , 3 , 4 , 2 , 1 , 6 } ; int [ ] result = findMinimumHike ( data , data . length ) ; System . out . println ( Arrays . toString ( result ) ) ; data = new int [ ] { 1 , 3 , 5 , 4 } ; result = findMinimumHike ( data , data . length ) ; System . out . println ( Arrays . toString ( result ) ) ; data = new int [ ] { 1 , 4 } ; result = findMinimumHike ( data , data . length ) ; System . out . println ( Arrays . toString ( result ) ) ; } }
Range Minimum Query ( Square Root Decomposition and Sparse Table ) | Java program to do range minimum query in O ( 1 ) time with O ( n Log n ) extra space and O ( n Log n ) preprocessing time ; lookup [ i ] [ j ] is going to store index of minimum value in arr [ i . . j ] . Ideally lookup table size should not be fixed and should be determined using n Log n . It is kept constant to keep code simple . ; Structure to represent a query range ; Fills lookup array lookup [ ] [ ] in bottom up manner . ; Initialize M for the intervals with length 1 ; Compute values from smaller to bigger intervals ; Compute minimum value for all intervals with size 2 ^ j ; For arr [ 2 ] [ 10 ] , we compare arr [ lookup [ 0 ] [ 3 ] ] and arr [ lookup [ 3 ] [ 3 ] ] ; Returns minimum of arr [ L . . R ] ; For [ 2 , 10 ] , j = 3 ; For [ 2 , 10 ] , we compare arr [ lookup [ 0 ] [ 3 ] ] and arr [ lookup [ 3 ] [ 3 ] ] , ; Prints minimum of given m query ranges in arr [ 0. . n - 1 ] ; Fills table lookup [ n ] [ Log n ] ; One by one compute sum of all queries ; Left and right boundaries of current range ; Print sum of current query range ; Driver Code
import java . util . * ; class GFG { static int MAX = 500 ; static int [ ] [ ] lookup = new int [ MAX ] [ MAX ] ; static class Query { int L , R ; public Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static void preprocess ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i ] [ 0 ] = i ; for ( int j = 1 ; ( 1 << j ) <= n ; j ++ ) { for ( int i = 0 ; ( i + ( 1 << j ) - 1 ) < n ; i ++ ) { if ( arr [ lookup [ i ] [ j - 1 ] ] < arr [ lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ] ) lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] ; else lookup [ i ] [ j ] = lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ; } } } static int query ( int arr [ ] , int L , int R ) { int j = ( int ) Math . log ( R - L + 1 ) ; if ( arr [ lookup [ L ] [ j ] ] <= arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] ) return arr [ lookup [ L ] [ j ] ] ; else return arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] ; } static void RMQ ( int arr [ ] , int n , Query q [ ] , int m ) { preprocess ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; System . out . println ( " Minimum ▁ of ▁ [ " + L + " , ▁ " + R + " ] ▁ is ▁ " + query ( arr , L , R ) ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = a . length ; Query q [ ] = { new Query ( 0 , 4 ) , new Query ( 4 , 7 ) , new Query ( 7 , 8 ) } ; int m = q . length ; RMQ ( a , n , q , m ) ; } }
Find sum of all nodes of the given perfect binary tree | Java code to find sum of all nodes of the given perfect binary tree ; function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; sum of nodes at last level ; sum of all nodes ; Driver Code
import java . io . * ; import java . lang . Math ; class GFG { static double sumNodes ( int l ) { double leafNodeCount = Math . pow ( 2 , l - 1 ) ; double sumLastLevel = 0 ; sumLastLevel = ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ; double sum = sumLastLevel * l ; return sum ; } public static void main ( String [ ] args ) { int l = 3 ; System . out . println ( sumNodes ( l ) ) ; } }
Longest subsequence with different adjacent characters | Java program for the above approach ; dp table ; A recursive function to find the update the dp [ ] [ ] table ; If we reach end of the String ; If subproblem has been computed ; Initialise variable to find the maximum length ; Choose the current character ; Omit the current character ; Return the store answer to the current subproblem ; Function to find the longest Subsequence with different adjacent character ; Length of the String s ; Initialise the memoisation table ; Return the final ans after every recursive call ; Driver Code ; Function call
class GFG { static int dp [ ] [ ] = new int [ 100005 ] [ 27 ] ; static int calculate ( int pos , int prev , String s ) { if ( pos == s . length ( ) ) { return 0 ; } if ( dp [ pos ] [ prev ] != - 1 ) return dp [ pos ] [ prev ] ; int val = 0 ; if ( s . charAt ( pos ) - ' a ' + 1 != prev ) { val = Math . max ( val , 1 + calculate ( pos + 1 , s . charAt ( pos ) - ' a ' + 1 , s ) ) ; } val = Math . max ( val , calculate ( pos + 1 , prev , s ) ) ; return dp [ pos ] [ prev ] = val ; } static int longestSubsequence ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < 100005 ; i ++ ) { for ( int j = 0 ; j < 27 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return calculate ( 0 , 0 , s ) ; } public static void main ( String [ ] args ) { String str = " ababa " ; System . out . print ( longestSubsequence ( str ) ) ; } }
Constant time range add operation on an array | Java program to get updated array after many array range add operation ; Utility method to add value val , to range [ lo , hi ] ; Utility method to get actual array from operation array ; convert array into prefix sum array ; method to print final updated array ; Driver code ; Range add Queries
import java . io . * ; class GFG { static void add ( int arr [ ] , int N , int lo , int hi , int val ) { arr [ lo ] += val ; if ( hi != N - 1 ) arr [ hi + 1 ] -= val ; } static void updateArray ( int arr [ ] , int N ) { for ( int i = 1 ; i < N ; i ++ ) arr [ i ] += arr [ i - 1 ] ; } static void printArr ( int arr [ ] , int N ) { updateArray ( arr , N ) ; for ( int i = 0 ; i < N ; i ++ ) System . out . print ( " " + arr [ i ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = new int [ N ] ; add ( arr , N , 0 , 2 , 100 ) ; add ( arr , N , 1 , 5 , 100 ) ; add ( arr , N , 2 , 3 , 100 ) ; printArr ( arr , N ) ; } }
Count of elements in an Array whose set bits are in a multiple of K | Java implementation of above approach ; Function to find the count of numbers ; Get the set - bits count of each element ; Check if the setbits count is divisible by K ; Increment the count of required numbers by 1 ; Driver code
class GFG { static int find_count ( int [ ] arr , int k ) { int ans = 0 ; for ( int i : arr ) { int x = Integer . bitCount ( i ) ; if ( x % k == 0 ) ans += 1 ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 12 , 345 , 2 , 68 , 7896 } ; int K = 2 ; System . out . print ( find_count ( arr , K ) ) ; } }