codeparrot/xlcost-text-to-code huggingface dataset:
Search , insert and delete in a sorted array | Java program to delete an element from a sorted array ; binary search ; Function to delete an element ; Find position of element to be deleted ; Deleting element ; Driver Code
class Main { static int binarySearch ( int arr [ ] , int low , int high , int key ) { if ( high < low ) return - 1 ; int mid = ( low + high ) / 2 ; if ( key == arr [ mid ] ) return mid ; if ( key > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , key ) ; return binarySearch ( arr , low , ( mid - 1 ) , key ) ; } static int deleteElement ( int arr [ ] , int n , int key ) { int pos = binarySearch ( arr , 0 , n - 1 , key ) ; if ( pos == - 1 ) { System . out . println ( " Element ▁ not ▁ found " ) ; return n ; } int i ; for ( i = pos ; i < n - 1 ; i ++ ) arr [ i ] = arr [ i + 1 ] ; return n - 1 ; } public static void main ( String [ ] args ) { int i ; int arr [ ] = { 10 , 20 , 30 , 40 , 50 } ; int n = arr . length ; int key = 30 ; System . out . print ( "Array before deletion:NEW_LINE"); for ( i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; n = deleteElement ( arr , n , key ) ; System . out . print ( " Array after deletion : "); for ( i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Queries on number of Binary sub | Java Program to answer queries on number of submatrix of given size ; Return the minimum of three numbers ; Solve each query on matrix ; For each of the cell . ; finding submatrix size of oth row and column . ; intermediate cells . ; Find frequency of each distinct size for 0 s and 1 s . ; Find the Cumulative Sum . ; Output the answer for each query ; Driver Program
import java . io . * ; class GFG { static int MAX = 100 ; static int N = 5 ; static int M = 4 ; static int min ( int a , int b , int c ) { return Math . min ( a , Math . min ( b , c ) ) ; } static void solveQuery ( int n , int m , int mat [ ] [ ] , int q , int a [ ] , int binary [ ] ) { int dp [ ] [ ] = new int [ n ] [ m ] ; int max = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( i == 0 j == 0 ) dp [ i ] [ j ] = 1 ; else if ( ( mat [ i ] [ j ] == mat [ i - 1 ] [ j ] ) && ( mat [ i ] [ j ] == mat [ i ] [ j - 1 ] ) && ( mat [ i ] [ j ] == mat [ i - 1 ] [ j - 1 ] ) ) { dp [ i ] [ j ] = min ( dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ j - 1 ] , dp [ i ] [ j - 1 ] ) + 1 ; if ( max < dp [ i ] [ j ] ) max = dp [ i ] [ j ] ; } else dp [ i ] [ j ] = 1 ; } } int freq0 [ ] = new int [ MAX ] ; int freq1 [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 0 ) freq0 [ dp [ i ] [ j ] ] ++ ; else freq1 [ dp [ i ] [ j ] ] ++ ; } } for ( int i = max - 1 ; i >= 0 ; i -- ) { freq0 [ i ] += freq0 [ i + 1 ] ; freq1 [ i ] += freq1 [ i + 1 ] ; } for ( int i = 0 ; i < q ; i ++ ) { if ( binary [ i ] == 0 ) System . out . println ( freq0 [ a [ i ] ] ) ; else System . out . println ( freq1 [ a [ i ] ] ) ; } } public static void main ( String [ ] args ) { int n = 5 , m = 4 ; int mat [ ] [ ] = { { 0 , 0 , 1 , 1 } , { 0 , 0 , 1 , 0 } , { 0 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 } , { 0 , 1 , 1 , 1 } } ; int q = 2 ; int a [ ] = { 2 , 2 } ; int binary [ ] = { 1 , 0 } ; solveQuery ( n , m , mat , q , a , binary ) ; } }
Dynamic Programming | Wildcard Pattern Matching | Linear Time and Constant Space | Java program to implement wildcard pattern matching algorithm ; Function that matches input text with given wildcard pattern ; empty pattern can only match with empty string . Base Case : ; step - 1 : initialize markers : ; For step - ( 2 , 5 ) ; For step - ( 3 ) ; For step - ( 4 ) ; For step - ( 5 ) ; For step - ( 6 ) ; For step - ( 7 ) ; Final Check ; Driver code
class GFG { static boolean strmatch ( char txt [ ] , char pat [ ] , int n , int m ) { if ( m == 0 ) return ( n == 0 ) ; int i = 0 , j = 0 , index_txt = - 1 , index_pat = - 1 ; while ( i < n ) { if ( j < m && txt [ i ] == pat [ j ] ) { i ++ ; j ++ ; } else if ( j < m && pat [ j ] == ' ? ' ) { i ++ ; j ++ ; } else if ( j < m && pat [ j ] == ' * ' ) { index_txt = i ; index_pat = j ; j ++ ; } else if ( index_pat != - 1 ) { j = index_pat + 1 ; i = index_txt + 1 ; index_txt ++ ; } else { return false ; } } while ( j < m && pat [ j ] == ' * ' ) { j ++ ; } if ( j == m ) { return true ; } return false ; } public static void main ( String [ ] args ) { char str [ ] = " baaabab " . toCharArray ( ) ; char pattern [ ] = " * * * * * ba * * * * * ab " . toCharArray ( ) ; char pattern [ ] = " ba * * * * * ab " ; char pattern [ ] = " ba * ab " ; char pattern [ ] = " a * ab " ; if ( strmatch ( str , pattern , str . length , pattern . length ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; char pattern2 [ ] = " a * * * * * ab " . toCharArray ( ) ; if ( strmatch ( str , pattern2 , str . length , pattern2 . length ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find n | Java program to find the nth element of Stern 's Diatomic Series ; function to find nth stern '  diatomic series ; Initializing the DP array ; SET the Base case ; Traversing the array from 2 nd Element to nth Element ; Case 1 : for even n ; Case 2 : for odd n ; Driver program
class GFG { static int findSDSFunc ( int n ) { int DP [ ] = new int [ n + 1 ] ; DP [ 0 ] = 0 ; DP [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) DP [ i ] = DP [ i / 2 ] ; else DP [ i ] = DP [ ( i - 1 ) / 2 ] + DP [ ( i + 1 ) / 2 ] ; } return DP [ n ] ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( findSDSFunc ( n ) ) ; } }
Check if any valid sequence is divisible by M | Java implementation of above approach ; Calculate modulo for this call ; Base case ; check if sum is divisible by M ; check if the current state is already computed ; 1. Try placing ' + ' ; 2. Try placing ' - ' ; calculate value of res for recursive case ; store the value for res for current states and return for parent call ; Driver code ; MAX is the Maximum value M can take
class GFG { static int MAX = 100 ; static int isPossible ( int n , int index , int modulo , int M , int arr [ ] , int dp [ ] [ ] ) { modulo = ( ( modulo % M ) + M ) % M ; if ( index == n ) { if ( modulo == 0 ) { return 1 ; } return 0 ; } if ( dp [ index ] [ modulo ] != - 1 ) { return dp [ index ] [ modulo ] ; } int placeAdd = isPossible ( n , index + 1 , modulo + arr [ index ] , M , arr , dp ) ; int placeMinus = isPossible ( n , index + 1 , modulo - arr [ index ] , M , arr , dp ) ; int res = placeAdd ; dp [ index ] [ modulo ] = res ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 6 } ; int n = arr . length ; int M = 4 ; int dp [ ] [ ] = new int [ n + 1 ] [ MAX ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } boolean res ; if ( isPossible ( n , 1 , arr [ 0 ] , M , arr , dp ) == 1 ) { res = true ; } else { res = false ; } System . out . println ( res ? " True " : " False " ) ; } }
Find common elements in three sorted arrays | Java program to find common elements in three arrays ; This function prints common elements in ar1 ; Initialize starting indexes for ar1 [ ] , ar2 [ ] and ar3 [ ] ; Iterate through three arrays while all arrays have elements ; If x = y and y = z , print any of them and move ahead in all arrays ; x < y ; y < z ; We reach here when x > y and z < y , i . e . , z is smallest ; Driver code to test above
class FindCommon { void findCommon ( int ar1 [ ] , int ar2 [ ] , int ar3 [ ] ) { int i = 0 , j = 0 , k = 0 ; while ( i < ar1 . length && j < ar2 . length && k < ar3 . length ) { if ( ar1 [ i ] == ar2 [ j ] && ar2 [ j ] == ar3 [ k ] ) { System . out . print ( ar1 [ i ] + " ▁ " ) ; i ++ ; j ++ ; k ++ ; } else if ( ar1 [ i ] < ar2 [ j ] ) i ++ ; else if ( ar2 [ j ] < ar3 [ k ] ) j ++ ; else k ++ ; } } public static void main ( String args [ ] ) { FindCommon ob = new FindCommon ( ) ; int ar1 [ ] = { 1 , 5 , 10 , 20 , 40 , 80 } ; int ar2 [ ] = { 6 , 7 , 20 , 80 , 100 } ; int ar3 [ ] = { 3 , 4 , 15 , 20 , 30 , 70 , 80 , 120 } ; System . out . print ( " Common ▁ elements ▁ are ▁ " ) ; ob . findCommon ( ar1 , ar2 , ar3 ) ; } }
Dynamic Programming on Trees | Set | Java code to find the maximum path sum ; function for dfs traversal and to store the maximum value in dp [ ] for every node till the leaves ; initially dp [ u ] is always a [ u ] ; stores the maximum value from nodes ; traverse the tree ; if child is parent , then we continue without recursing further ; call dfs for further traversal ; store the maximum of previous visited node and present visited node ; add the maximum value returned to the parent node ; function that returns the maximum value ; Driver Code ; number of nodes ; adjacency list ; create undirected edges initialize the tree given in the diagram ; values of node 1 , 2 , 3. ... 14 ; function call
import java . util . Vector ; class GFG { static int [ ] dp = new int [ 100 ] ; static void dfs ( int [ ] a , Vector < Integer > [ ] v , int u , int parent ) { dp [ u ] = a [ u - 1 ] ; int maximum = 0 ; for ( int child : v [ u ] ) { if ( child == parent ) continue ; dfs ( a , v , child , u ) ; maximum = Math . max ( maximum , dp [ child ] ) ; } dp [ u ] += maximum ; } static int maximumValue ( int [ ] a , Vector < Integer > [ ] v ) { dfs ( a , v , 1 , 0 ) ; return dp [ 1 ] ; } public static void main ( String [ ] args ) { int n = 14 ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] v = new Vector [ n + 1 ] ; for ( int i = 0 ; i < v . length ; i ++ ) v [ i ] = new Vector < > ( ) ; v [ 1 ] . add ( 2 ) ; v [ 2 ] . add ( 1 ) ; v [ 1 ] . add ( 3 ) ; v [ 3 ] . add ( 1 ) ; v [ 1 ] . add ( 4 ) ; v [ 4 ] . add ( 1 ) ; v [ 2 ] . add ( 5 ) ; v [ 5 ] . add ( 2 ) ; v [ 2 ] . add ( 6 ) ; v [ 6 ] . add ( 2 ) ; v [ 3 ] . add ( 7 ) ; v [ 7 ] . add ( 3 ) ; v [ 4 ] . add ( 8 ) ; v [ 8 ] . add ( 4 ) ; v [ 4 ] . add ( 9 ) ; v [ 9 ] . add ( 4 ) ; v [ 4 ] . add ( 10 ) ; v [ 10 ] . add ( 4 ) ; v [ 5 ] . add ( 11 ) ; v [ 11 ] . add ( 5 ) ; v [ 5 ] . add ( 12 ) ; v [ 12 ] . add ( 5 ) ; v [ 7 ] . add ( 13 ) ; v [ 13 ] . add ( 7 ) ; v [ 7 ] . add ( 14 ) ; v [ 14 ] . add ( 7 ) ; int a [ ] = { 3 , 2 , 1 , 10 , 1 , 3 , 9 , 1 , 5 , 3 , 4 , 5 , 9 , 8 } ; System . out . println ( maximumValue ( a , v ) ) ; } }
Jacobsthal and Jacobsthal | A DP based solution to find Jacobsthal and Jacobsthal - Lucas numbers ; Return nth Jacobsthal number . ; base case ; Return nth Jacobsthal - Lucas number . ; base case ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static int Jacobsthal ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + 2 * dp [ i - 2 ] ; return dp [ n ] ; } public static int Jacobsthal_Lucas ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 2 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + 2 * dp [ i - 2 ] ; return dp [ n ] ; } public static void main ( String argc [ ] ) { int n = 5 ; System . out . println ( " Jacobsthal ▁ number : ▁ " + Jacobsthal ( n ) ) ; System . out . println ( " Jacobsthal - Lucas ▁ number : ▁ " + Jacobsthal_Lucas ( n ) ) ; } }
Find position of an element in a sorted array of infinite numbers | Java program to demonstrate working of an algorithm that finds an element in an array of infinite size ; Simple binary search algorithm ; Method takes an infinite size array and a key to be searched and returns its position if found else - 1. We don 't know size of arr[] and we can assume size to be  infinite in this function.  NOTE THAT THIS FUNCTION ASSUMES arr[] TO BE OF INFINITE SIZE  THEREFORE, THERE IS NO INDEX OUT OF BOUND CHECKING ; Find h to do binary search ; store previous high ; check that 2 * h doesn 't exceeds array  length to prevent ArrayOutOfBoundException ; update new val ; at this point we have updated low and high indices , thus use binary search between them ; Driver method to test the above function
class Test { static int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } static int findPos ( int arr [ ] , int key ) { int l = 0 , h = 1 ; int val = arr [ 0 ] ; while ( val < key ) { l = h ; if ( 2 * h < arr . length - 1 ) h = 2 * h ; else h = arr . length - 1 ; val = arr [ h ] ; } return binarySearch ( arr , l , h , key ) ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 } ; int ans = findPos ( arr , 10 ) ; if ( ans == - 1 ) System . out . println ( " Element ▁ not ▁ found " ) ; else System . out . println ( " Element ▁ found ▁ at ▁ index ▁ " + ans ) ; } }
Count of possible hexagonal walks | Java implementation of counting number of possible hexagonal walks ; We initialize our origin with 1 ; For each N = 1 to 14 , we traverse in all possible direction . Using this 3D array we calculate the number of ways at each step and the total ways for a given step shall be found at ways [ step number ] [ 8 ] [ 8 ] because all the steps after that will be used to trace back to the original point index 0 : 0 according to the image . ; This array stores the number of ways possible for a given step ; Driver program to test above function ; Preprocessing all possible ways
import java . util . * ; class GFG { static int depth = 14 ; static int ways [ ] [ ] [ ] = new int [ 16 ] [ 16 ] [ 16 ] ; static int stepNum ; static void preprocess ( int list [ ] ) { ways [ 0 ] [ 8 ] [ 8 ] = 1 ; for ( int N = 1 ; N <= 14 ; N ++ ) { for ( int i = 1 ; i < depth ; i ++ ) { for ( int j = 1 ; j < depth ; j ++ ) { ways [ N ] [ i ] [ j ] = ways [ N - 1 ] [ i ] [ j + 1 ] + ways [ N - 1 ] [ i ] [ j - 1 ] + ways [ N - 1 ] [ i + 1 ] [ j ] + ways [ N - 1 ] [ i - 1 ] [ j ] + ways [ N - 1 ] [ i + 1 ] [ j - 1 ] + ways [ N - 1 ] [ i - 1 ] [ j + 1 ] ; } } list [ N ] = ways [ N ] [ 8 ] [ 8 ] ; } } public static void main ( String [ ] args ) { int list [ ] = new int [ 15 ] ; preprocess ( list ) ; int steps = 4 ; System . out . println ( " Number ▁ of ▁ walks " + " ▁ possible ▁ is / are ▁ " + list [ steps ] ) ; } }
Check if possible to cross the matrix with given power | Java program to find if it is possible to cross the matrix with given power ; Initializing array dp with false value . ; For each value of dp [ i ] [ j ] [ k ] ; For first cell and for each value of k ; For first cell of each row ; For first cell of each column ; For rest of the cell ; Down movement . ; Right movement . ; Diagonal movement . ; Finding maximum k . ; Driver code
class GFG { static final int N = 105 ; static final int R = 3 ; static final int C = 4 ; static int maximumValue ( int n , int m , int p , int grid [ ] [ ] ) { boolean dp [ ] [ ] [ ] = new boolean [ N ] [ N ] [ N ] ; int i , j , k ; for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { for ( k = 0 ; k < N ; k ++ ) dp [ i ] [ j ] [ k ] = false ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { for ( k = grid [ i ] [ j ] ; k <= p ; k ++ ) { if ( i == 0 && j == 0 ) { if ( k == grid [ i ] [ j ] ) dp [ i ] [ j ] [ k ] = true ; } else if ( i == 0 ) { dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) ; } else if ( j == 0 ) { dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i - 1 ] [ j ] [ k - grid [ i ] [ j ] ] ) ; } else { dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) ; dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i - 1 ] [ j ] [ k - grid [ i ] [ j ] ] ) ; dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i - 1 ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) ; } } } } k = p ; int ans = 0 ; for ( ans = k ; ans >= 0 ; ans -- ) if ( dp [ n - 1 ] [ m - 1 ] [ ans ] ) break ; return ans ; } public static void main ( String [ ] args ) { int n = 3 , m = 4 , p = 9 ; int grid [ ] [ ] = { { 2 , 3 , 4 , 1 } , { 6 , 5 , 5 , 3 } , { 5 , 2 , 3 , 4 } } ; System . out . println ( maximumValue ( n , m , p , grid ) ) ; } }
Number of n digit stepping numbers | Java program to calculate the number of n digit stepping numbers . ; function that calculates the answer ; dp [ i ] [ j ] stores count of i digit stepping numbers ending with digit j . ; if n is 1 then answer will be 10. ; Initialize values for count of digits equal to 1. ; Compute values for count of digits more than 1. ; If ending digit is 0 ; If ending digit is 9 ; For other digits . ; stores the final answer ; driver program to test the above function
class GFG { static long answer ( int n ) { int dp [ ] [ ] = new int [ n + 1 ] [ 10 ] ; if ( n == 1 ) return 10 ; for ( int j = 0 ; j <= 9 ; j ++ ) dp [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ; else if ( j == 9 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j + 1 ] ; } } long sum = 0 ; for ( int j = 1 ; j <= 9 ; j ++ ) sum += dp [ n ] [ j ] ; return sum ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . println ( answer ( n ) ) ; } }
Print Longest Palindromic Subsequence | Java program to print longest palindromic subsequence ; Returns LCS X and Y ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; Following code is used to print LCS ; Create a String length index + 1 and fill it with \ 0 ; Start from the right - most - bottom - most corner and one by one store characters in lcs [ ] ; If current character in X [ ] and Y are same , then current character is part of LCS ; Put current character in result ; reduce values of i , j and index ; If not same , then find the larger of two and go in the direction of larger value ; Returns longest palindromic subsequence of str ; Find reverse of str ; Return LCS of str and its reverse ; convert String to character array by using toCharArray ; Driver program to test above function
class GFG { static String lcs ( String a , String b ) { int m = a . length ( ) ; int n = b . length ( ) ; char X [ ] = a . toCharArray ( ) ; char Y [ ] = b . toCharArray ( ) ; int L [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) { L [ i ] [ j ] = 0 ; } else if ( X [ i - 1 ] == Y [ j - 1 ] ) { L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; } else { L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } } int index = L [ m ] [ n ] ; char [ ] lcs = new char [ index + 1 ] ; int i = m , j = n ; while ( i > 0 && j > 0 ) { if ( X [ i - 1 ] == Y [ j - 1 ] ) { lcs [ index - 1 ] = X [ i - 1 ] ; i -- ; j -- ; index -- ; } else if ( L [ i - 1 ] [ j ] > L [ i ] [ j - 1 ] ) { i -- ; } else { j -- ; } } String ans = " " ; for ( int x = 0 ; x < lcs . length ; x ++ ) { ans += lcs [ x ] ; } return ans ; } static String longestPalSubseq ( String str ) { String rev = str ; rev = reverse ( rev ) ; return lcs ( str , rev ) ; } static String reverse ( String str ) { String ans = " " ; char [ ] try1 = str . toCharArray ( ) ; for ( int i = try1 . length - 1 ; i >= 0 ; i -- ) { ans += try1 [ i ] ; } return ans ; } public static void main ( String [ ] args ) { String str = " GEEKSFORGEEKS " ; System . out . println ( longestPalSubseq ( str ) ) ; } }
Count all subsequences having product less than K | Java program to find number of subarrays having product less than k . ; Function to count numbers of such subsequences having product less than k . ; number of subsequence using j - 1 terms ; if arr [ j - 1 ] > i it will surely make product greater thus it won 't contribute  then ; number of subsequence using 1 to j - 1 terms and j - th term ; Driver code
import java . util . * ; class CountSubsequences { public static int productSubSeqCount ( ArrayList < Integer > arr , int k ) { int n = arr . size ( ) ; int dp [ ] [ ] = new int [ k + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= k ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; if ( arr . get ( j - 1 ) <= i && arr . get ( j - 1 ) > 0 ) dp [ i ] [ j ] += dp [ i / arr . get ( j - 1 ) ] [ j - 1 ] + 1 ; } } return dp [ k ] [ n ] ; } public static void main ( String args [ ] ) { ArrayList < Integer > A = new ArrayList < Integer > ( ) ; A . add ( 1 ) ; A . add ( 2 ) ; A . add ( 3 ) ; A . add ( 4 ) ; int k = 10 ; System . out . println ( productSubSeqCount ( A , k ) ) ; } }
Find the element that appears once in an array where every other element appears twice | Java program to find the array element that appears only once ; Return the maximum Sum of difference between consecutive elements . ; Do XOR of all elements and return ; Driver code
class MaxSum { static int findSingle ( int ar [ ] , int ar_size ) { int res = ar [ 0 ] ; for ( int i = 1 ; i < ar_size ; i ++ ) res = res ^ ar [ i ] ; return res ; } public static void main ( String [ ] args ) { int ar [ ] = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = ar . length ; System . out . println ( " Element ▁ occurring ▁ once ▁ is ▁ " + findSingle ( ar , n ) + " ▁ " ) ; } }
Count all triplets whose sum is equal to a perfect cube | JAVA Code for Count all triplets whose sum is equal to a perfect cube ; Function to calculate all occurrence of a number in a given range ; if i == 0 assign 1 to present state ; else add + 1 to current state with previous state ; Function to calculate triplets whose sum is equal to the perfect cube ; Initialize answer ; count all occurrence of third triplet in range from j + 1 to n ; Driver program to test above function
import java . util . * ; class GFG { public static int dp [ ] [ ] ; public static void computeDpArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 1 ; j <= 15000 ; ++ j ) { if ( i == 0 && j == arr [ i ] ) dp [ i ] [ j ] = 1 ; else if ( i == 0 ) dp [ i ] [ j ] = 0 ; else if ( arr [ i ] == j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + 1 ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } public static int countTripletSum ( int arr [ ] , int n ) { computeDpArray ( arr , n ) ; int ans = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { for ( int j = i + 1 ; j < n - 1 ; ++ j ) { for ( int k = 1 ; k <= 24 ; ++ k ) { int cube = k * k * k ; int rem = cube - ( arr [ i ] + arr [ j ] ) ; if ( rem > 0 ) ans += dp [ n - 1 ] [ rem ] - dp [ j ] [ rem ] ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 1 , 20 , 6 } ; int n = arr . length ; dp = new int [ 1001 ] [ 15001 ] ; System . out . println ( countTripletSum ( arr , n ) ) ; } }
Find the element that appears once in an array where every other element appears twice | Java program to find element that appears once ; function which find number ; applying the formula . ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int singleNumber ( int [ ] nums , int n ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; long sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! m . containsKey ( nums [ i ] ) ) { sum1 += nums [ i ] ; m . put ( nums [ i ] , 1 ) ; } sum2 += nums [ i ] ; } return ( int ) ( 2 * ( sum1 ) - sum2 ) ; } public static void main ( String args [ ] ) { int [ ] a = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = 7 ; System . out . println ( singleNumber ( a , n ) ) ; int [ ] b = { 15 , 18 , 16 , 18 , 16 , 15 , 89 } ; System . out . println ( singleNumber ( b , n ) ) ; } }