codeparrot/xlcost-text-to-code huggingface dataset:
Count of Distinct Substrings occurring consecutively in a given String | Java program to implement the above approach ; Function to count the distinct substrings placed consecutively in the given string ; Length of the string ; If length of the string does not exceed 1 ; Initialize a DP - table ; Declaring ArrayList to store strings ; Iterate from end of the string ; Iterate backward until dp table is all computed ; If character at i - th index is same as character at j - th index ; Update dp [ i ] [ j ] based on previously computed value ; Otherwise ; Condition for consecutively placed similar substring ; Return the count ; Driver Code
import java . io . * ; import java . util . ArrayList ; class GFG { static int distinctSimilarSubstrings ( String str ) { int n = str . length ( ) ; if ( n <= 1 ) return 0 ; long dp [ ] [ ] = new long [ n + 1 ] [ n + 1 ] ; ArrayList < String > list = new ArrayList < String > ( ) ; for ( int j = n - 1 ; j >= 0 ; j -- ) { for ( int i = j - 1 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == str . charAt ( j ) ) { dp [ i ] [ j ] = dp [ i + 1 ] [ j + 1 ] + 1 ; } else { dp [ i ] [ j ] = 0 ; } if ( dp [ i ] [ j ] >= j - i ) { list . add ( str . substring ( j - i , i ) ) ; } } } return list . size ( ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( distinctSimilarSubstrings ( str ) ) ; } }
Finding shortest path between any two nodes using Floyd Warshall Algorithm | Java program to find the shortest path between any two nodes using Floyd Warshall Algorithm . ; Infinite value for array ; Initializing the distance and Next array ; No edge between node i and j ; Function construct the shortest path between u and v ; If there 's no path between  node u and v, simply return  an empty array ; Storing the path in a vector ; Standard Floyd Warshall Algorithm with little modification Now if we find that dis [ i ] [ j ] > dis [ i ] [ k ] + dis [ k ] [ j ] then we modify next [ i ] [ j ] = next [ i ] [ k ] ; We cannot travel through edge that doesn 't exist ; Print the shortest path ; Driver code ; Function to initialise the distance and Next array ; Calling Floyd Warshall Algorithm , this will update the shortest distance as well as Next array ; Path from node 1 to 3 ; Path from node 0 to 2 ; Path from node 3 to 2
import java . util . * ; class GFG { static final int MAXN = 100 ; static int INF = ( int ) 1e7 ; static int [ ] [ ] dis = new int [ MAXN ] [ MAXN ] ; static int [ ] [ ] Next = new int [ MAXN ] [ MAXN ] ; static void initialise ( int V , int [ ] [ ] graph ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { dis [ i ] [ j ] = graph [ i ] [ j ] ; if ( graph [ i ] [ j ] == INF ) Next [ i ] [ j ] = - 1 ; else Next [ i ] [ j ] = j ; } } } static Vector < Integer > constructPath ( int u , int v ) { if ( Next [ u ] [ v ] == - 1 ) return null ; Vector < Integer > path = new Vector < Integer > ( ) ; path . add ( u ) ; while ( u != v ) { u = Next [ u ] [ v ] ; path . add ( u ) ; } return path ; } static void floydWarshall ( int V ) { for ( int k = 0 ; k < V ; k ++ ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { if ( dis [ i ] [ k ] == INF dis [ k ] [ j ] == INF ) continue ; if ( dis [ i ] [ j ] > dis [ i ] [ k ] + dis [ k ] [ j ] ) { dis [ i ] [ j ] = dis [ i ] [ k ] + dis [ k ] [ j ] ; Next [ i ] [ j ] = Next [ i ] [ k ] ; } } } } } static void printPath ( Vector < Integer > path ) { int n = path . size ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) System . out . print ( path . get ( i ) + " ▁ - > ▁ " ) ; System . out . print ( path . get ( n - 1 ) + "NEW_LINE"); } public static void main ( String [ ] args ) { int V = 4 ; int [ ] [ ] graph = { { 0 , 3 , INF , 7 } , { 8 , 0 , 2 , INF } , { 5 , INF , 0 , 1 } , { 2 , INF , INF , 0 } } ; initialise ( V , graph ) ; floydWarshall ( V ) ; Vector < Integer > path ; System . out . print ( " Shortest ▁ path ▁ from ▁ 1 ▁ to ▁ 3 : ▁ " ) ; path = constructPath ( 1 , 3 ) ; printPath ( path ) ; System . out . print ( " Shortest ▁ path ▁ from ▁ 0 ▁ to ▁ 2 : ▁ " ) ; path = constructPath ( 0 , 2 ) ; printPath ( path ) ; System . out . print ( " Shortest ▁ path ▁ from ▁ 3 ▁ to ▁ 2 : ▁ " ) ; path = constructPath ( 3 , 2 ) ; printPath ( path ) ; } }
Count sequences of length K having each term divisible by its preceding term | Java program to implement the above approach ; Stores the factors of i - th element in v [ i ] ; Function to find all the factors of N ; Iterate upto Math . sqrt ( N ) ; Function to return the count of sequences of length K having aint terms divisible by its preceding term ; Calculate factors of i ; Initialize dp [ 0 ] [ i ] = 0 : No subsequence of length 0 ending with i - th element exists ; Initialize dp [ 0 ] [ i ] = 1 : Only 1 subsequence of length 1 ending with i - th element exists ; Iterate [ 2 , K ] to obtain sequences of each length ; Calculate sum of aint dp [ i - 1 ] [ vp [ j ] [ k ] ] ; vp [ j ] [ k ] stores aint factors of j ; Store the sum in A [ i ] [ j ] ; Sum of aint dp [ K ] [ j ] obtain all K length sequences ending with j ; Driver code
import java . util . * ; class GFG { @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] vp = new Vector [ 2009 ] ; static void finding_factors ( int n ) { int i , a ; for ( i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i == n ) { vp [ n ] . add ( i ) ; } else { vp [ n ] . add ( i ) ; vp [ n ] . add ( n / i ) ; } } } } static int countSeq ( int N , int K ) { int i , j , k ; int dp [ ] [ ] = new int [ 109 ] [ 109 ] ; for ( i = 1 ; i <= N ; i ++ ) { finding_factors ( i ) ; dp [ 0 ] [ i ] = 0 ; dp [ 1 ] [ i ] = 1 ; } for ( i = 2 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { int sum = 0 ; for ( k = 0 ; k < vp [ j ] . size ( ) ; k ++ ) { sum = ( sum + dp [ i - 1 ] [ vp [ j ] . get ( k ) ] ) ; } dp [ i ] [ j ] = sum ; } } int ans = 0 ; for ( j = 1 ; j <= N ; j ++ ) { ans = ( ans + dp [ K ] [ j ] ) ; } return ans ; } public static void main ( String [ ] args ) { int N , K ; N = 3 ; K = 2 ; for ( int i = 0 ; i < vp . length ; i ++ ) vp [ i ] = new Vector < Integer > ( ) ; System . out . print ( countSeq ( N , K ) + "NEW_LINE"); } }
Largest possible square submatrix with maximum AND value | Java program to find the length of longest possible square submatrix with maximum AND value from the given matrix ; Function to calculate and return the length of square submatrix with maximum AND value ; Extract dimensions ; Auxiliary array ; c : Stores the maximum value in the matrix p : Stores the number of elements in the submatrix having maximum AND value ; Iterate over the matrix to fill the auxiliary matrix ; Find the max element in the matrix side by side ; Fill first row and column with 1 's ; For every cell , check if the elements at the left , top and top left cells from the current cell are equal or not ; Store the minimum possible submatrix size these elements are part of ; Store 1 otherwise ; Checking maximum value ; If the maximum AND value occurs more than once ; Update the maximum size of submatrix ; Final output ; Driver code
import java . util . * ; class GFG { static int MAX_value ( int [ ] [ ] arr ) { int row = arr . length ; int col = arr [ 0 ] . length ; int [ ] [ ] dp = new int [ row ] [ col ] ; int i = 0 , j = 0 ; int c = arr [ 0 ] [ 0 ] , p = 0 ; int d = row ; for ( i = 0 ; i < d ; i ++ ) { for ( j = 0 ; j < d ; j ++ ) { if ( c < arr [ i ] [ j ] ) { c = arr [ i ] [ j ] ; } if ( i == 0 j == 0 ) { dp [ i ] [ j ] = 1 ; } else { if ( arr [ i - 1 ] [ j - 1 ] == arr [ i ] [ j ] && arr [ i - 1 ] [ j ] == arr [ i ] [ j ] && arr [ i ] [ j - 1 ] == arr [ i ] [ j ] ) { dp [ i ] [ j ] = Math . min ( dp [ i - 1 ] [ j - 1 ] , Math . min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ) + 1 ; } else { dp [ i ] [ j ] = 1 ; } } } } for ( i = 0 ; i < d ; i ++ ) { for ( j = 0 ; j < d ; j ++ ) { if ( arr [ i ] [ j ] == c ) { if ( p < dp [ i ] [ j ] ) { p = dp [ i ] [ j ] ; } } } } return p * p ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 9 , 9 , 3 , 3 , 4 , 4 } , { 9 , 9 , 7 , 7 , 7 , 4 } , { 1 , 2 , 7 , 7 , 7 , 4 } , { 4 , 4 , 7 , 7 , 7 , 4 } , { 5 , 5 , 1 , 1 , 2 , 7 } , { 2 , 7 , 1 , 1 , 4 , 4 } } ; System . out . print ( MAX_value ( arr ) + "NEW_LINE"); } }
Smallest power of 2 consisting of N digits | Java program to implement the above approach ; Function to return smallest power of 2 with N digits ; Iterate through all powers of 2 ; Driver Code
import java . io . * ; class GFG { static int smallestNum ( int n ) { int res = 1 ; for ( int i = 2 ; ; i *= 2 ) { int length = ( int ) ( Math . log10 ( i ) ) + 1 ; if ( length == n ) return ( int ) ( Math . log ( i ) / Math . log ( 2 ) ) ; } } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( smallestNum ( n ) ) ; } }
Count full nodes in a Binary tree ( Iterative and Recursive ) | Java program to count full nodes in a Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of full Nodes in a binary tree ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ; Driver program ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree as shown
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static int getfullCount ( Node root ) { if ( root == null ) return 0 ; int res = 0 ; if ( root . left != null && root . right != null ) res ++ ; res += ( getfullCount ( root . left ) + getfullCount ( root . right ) ) ; return res ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 2 ) ; root . left = newNode ( 7 ) ; root . right = newNode ( 5 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 11 ) ; root . right . right = newNode ( 9 ) ; root . right . right . left = newNode ( 4 ) ; System . out . println ( getfullCount ( root ) ) ; } }
Count Possible Decodings of a given Digit Sequence in O ( N ) time and Constant Auxiliary space | Java implementation to count decodings ; A Dynamic programming based function to count decodings in digit sequence ; For base condition "01123" should return 0 ; Using last two calculated values , calculate for ith index ; Change boolean to int ; Return the required answer ; Driver Code ; Function call
class GFG { static int countDecodingDP ( String digits , int n ) { if ( digits . charAt ( 0 ) == '0' ) { return 0 ; } int count0 = 1 , count1 = 1 , count2 ; for ( int i = 2 ; i <= n ; i ++ ) { int dig1 = 0 , dig2 , dig3 = 0 ; if ( digits . charAt ( i - 1 ) != '0' ) { dig1 = 1 ; } if ( digits . charAt ( i - 2 ) == '1' ) { dig2 = 1 ; } else dig2 = 0 ; if ( digits . charAt ( i - 2 ) == '2' && digits . charAt ( i - 1 ) < '7' ) { dig3 = 1 ; } count2 = dig1 * count1 + dig2 + dig3 * count0 ; count0 = count1 ; count1 = count2 ; } return count1 ; } public static void main ( String [ ] args ) { String digits = "1234" ; int n = digits . length ( ) ; System . out . print ( countDecodingDP ( digits , n ) ) ; } }
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | Java program to find the maximum distance from a 0 - cell to a 1 - cell ; Function to find the maximum distance ; Set up top row and left column ; Pass one : top left to bottom right ; Check if there was no " One " Cell ; Set up top row and left column ; Past two : bottom right to top left ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int maxDistance ( int [ ] [ ] grid ) { if ( grid . length == 0 ) return - 1 ; int N = grid . length ; int INF = 1000000 ; grid [ 0 ] [ 0 ] = grid [ 0 ] [ 0 ] == 1 ? 0 : INF ; for ( int i = 1 ; i < N ; i ++ ) grid [ 0 ] [ i ] = grid [ 0 ] [ i ] == 1 ? 0 : grid [ 0 ] [ i - 1 ] + 1 ; for ( int i = 1 ; i < N ; i ++ ) grid [ i ] [ 0 ] = grid [ i ] [ 0 ] == 1 ? 0 : grid [ i - 1 ] [ 0 ] + 1 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < N ; j ++ ) { grid [ i ] [ j ] = grid [ i ] [ j ] == 1 ? 0 : Math . min ( grid [ i - 1 ] [ j ] , grid [ i ] [ j - 1 ] ) + 1 ; } } if ( grid [ N - 1 ] [ N - 1 ] >= INF ) return - 1 ; int maxi = grid [ N - 1 ] [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { grid [ N - 1 ] [ i ] = Math . min ( grid [ N - 1 ] [ i ] , grid [ N - 1 ] [ i + 1 ] + 1 ) ; maxi = Math . max ( grid [ N - 1 ] [ i ] , maxi ) ; } for ( int i = N - 2 ; i >= 0 ; i -- ) { grid [ i ] [ N - 1 ] = Math . min ( grid [ i ] [ N - 1 ] , grid [ i + 1 ] [ N - 1 ] + 1 ) ; maxi = Math . max ( grid [ i ] [ N - 1 ] , maxi ) ; } for ( int i = N - 2 ; i >= 0 ; i -- ) { for ( int j = N - 2 ; j >= 0 ; j -- ) { grid [ i ] [ j ] = Math . min ( grid [ i ] [ j ] , Math . min ( grid [ i + 1 ] [ j ] + 1 , grid [ i ] [ j + 1 ] + 1 ) ) ; maxi = Math . max ( grid [ i ] [ j ] , maxi ) ; } } return maxi == 0 ? - 1 : maxi ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 0 , 0 , 1 } , { 0 , 0 , 0 } , { 0 , 0 , 0 } } ; System . out . println ( maxDistance ( arr ) ) ; } }
Minimize total cost without repeating same task in two consecutive iterations | Java implementation of the above approach ; Function to return the minimum cost for N iterations ; Construct the dp table ; 1 st row of dp table will be equal to the 1 st of cost matrix ; Iterate through all the rows ; To iterate through the columns of current row ; Initialize val as infinity ; To iterate through the columns of previous row ; Fill the dp matrix ; Returning the minimum value ; Driver code ; Number of iterations ; Number of tasks ; Cost matrix
import java . io . * ; class GFG { static int findCost ( int cost_mat [ ] [ ] , int N , int M ) { int dp [ ] [ ] = new int [ N ] [ M ] ; for ( int i = 0 ; i < M ; i ++ ) dp [ 0 ] [ i ] = cost_mat [ 0 ] [ i ] ; for ( int row = 1 ; row < N ; row ++ ) { for ( int curr_col = 0 ; curr_col < M ; curr_col ++ ) { int val = 999999999 ; for ( int prev_col = 0 ; prev_col < M ; prev_col ++ ) { if ( curr_col != prev_col ) val = Math . min ( val , dp [ row - 1 ] [ prev_col ] ) ; } dp [ row ] [ curr_col ] = val + cost_mat [ row ] [ curr_col ] ; } } int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < M ; i ++ ) ans = Math . min ( ans , dp [ N - 1 ] [ i ] ) ; return ans ; } public static void main ( String [ ] args ) { int N = 4 ; int M = 4 ; int cost_mat [ ] [ ] = { { 4 , 5 , 3 , 2 } , { 6 , 2 , 8 , 1 } , { 6 , 2 , 2 , 1 } , { 0 , 5 , 5 , 1 } } ; System . out . println ( findCost ( cost_mat , N , M ) ) ; } }
Split the given string into Primes : Digit DP | Java implementation of the above approach ; Function to precompute all the primes upto 1000000 and store it in a set using Sieve of Eratosthenes ; Here to_String ( ) is used for converting int to String ; A function to find the minimum number of segments the given String can be divided such that every segment is a prime ; Declare a splitdp [ ] array and initialize to - 1 ; Call sieve function to store primes in primes array ; Build the DP table in a bottom - up manner ; If the prefix is prime then the prefix will be found in the prime set ; If the Given Prefix can be split into Primes then for the remaining String from i to j Check if Prime . If yes calculate the minimum split till j ; To check if the subString from i to j is a prime number or not ; If it is a prime , then update the dp array ; Return the minimum number of splits for the entire String ; Driver Code
import java . util . * ; class GFG { static void getPrimesFromSeive ( HashSet < String > primes ) { boolean [ ] prime = new boolean [ 1000001 ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= 1000000 ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * i ; j <= 1000000 ; j += i ) prime [ j ] = false ; } } for ( int i = 2 ; i <= 1000000 ; i ++ ) { if ( prime [ i ] == true ) primes . add ( String . valueOf ( i ) ) ; } } static int splitIntoPrimes ( String number ) { int numLen = number . length ( ) ; int [ ] splitDP = new int [ numLen + 1 ] ; Arrays . fill ( splitDP , - 1 ) ; HashSet < String > primes = new HashSet < String > ( ) ; getPrimesFromSeive ( primes ) ; for ( int i = 1 ; i <= numLen ; i ++ ) { if ( i <= 6 && ( primes . contains ( number . substring ( 0 , i ) ) ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( int j = 1 ; j <= 6 && i + j <= numLen ; j ++ ) { if ( primes . contains ( number . substring ( i , i + j ) ) ) { if ( splitDP [ i + j ] == - 1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = Math . min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } public static void main ( String [ ] args ) { System . out . print ( splitIntoPrimes ( "13499315" ) + "NEW_LINE"); System . out . print ( splitIntoPrimes ( "43" ) + "NEW_LINE"); } }
Connect Nodes at same Level ( Level Order Traversal ) | Connect nodes at same level using level order traversal . ; Node class ; Sets nextRight of all nodes of a tree ; null marker to represent end of current level ; Do Level order of tree using NULL markers ; next element in queue represents next node at current Level ; push left and right children of current node ; if queue is not empty , push NULL to mark nodes at this level are visited ; Driver program to test above functions ; Constructed binary tree is 10 / \ 8 2 / \ 3 90 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers
import java . util . LinkedList ; import java . util . Queue ; public class Connect_node_same_level { static class Node { int data ; Node left , right , nextRight ; Node ( int data ) { this . data = data ; left = null ; right = null ; nextRight = null ; } } ; static void connect ( Node root ) { Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; q . add ( null ) ; while ( ! q . isEmpty ( ) ) { Node p = q . peek ( ) ; q . remove ( ) ; if ( p != null ) { p . nextRight = q . peek ( ) ; if ( p . left != null ) q . add ( p . left ) ; if ( p . right != null ) q . add ( p . right ) ; } else if ( ! q . isEmpty ( ) ) q . add ( null ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 10 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; root . right . right = new Node ( 90 ) ; connect ( root ) ; System . out . println ( "Following are populated nextRight pointers in NEW_LINE" + " the ▁ tree ▁ ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; System . out . println ( " nextRight ▁ of ▁ " + root . data + " ▁ is ▁ " + ( ( root . nextRight != null ) ? root . nextRight . data : - 1 ) ) ; System . out . println ( " nextRight ▁ of ▁ " + root . left . data + " ▁ is ▁ " + ( ( root . left . nextRight != null ) ? root . left . nextRight . data : - 1 ) ) ; System . out . println ( " nextRight ▁ of ▁ " + root . right . data + " ▁ is ▁ " + ( ( root . right . nextRight != null ) ? root . right . nextRight . data : - 1 ) ) ; System . out . println ( " nextRight ▁ of ▁ " + root . left . left . data + " ▁ is ▁ " + ( ( root . left . left . nextRight != null ) ? root . left . left . nextRight . data : - 1 ) ) ; System . out . println ( " nextRight ▁ of ▁ " + root . right . right . data + " ▁ is ▁ " + ( ( root . right . right . nextRight != null ) ? root . right . right . nextRight . data : - 1 ) ) ; } }
Number of ways to convert a character X to a string Y | Java implementation of the approach ; Function to find the modular - inverse ; While power > 1 ; Updating s and a ; Updating power ; Return the final answer ; Function to return the count of ways ; To store the final answer ; To store pre - computed factorials ; Computing factorials ; Loop to find the occurrences of x and update the ans ; Multiplying the answer by ( n - 1 ) ! ; Return the final answer ; Driver code
class GFG { final static int MOD = 1000000007 ; static long modInv ( long a ) { long p = MOD - 2 ; long s = 1 ; while ( p != 1 ) { if ( p % 2 == 1 ) s = ( s * a ) % MOD ; a = ( a * a ) % MOD ; p /= 2 ; } return ( a * s ) % MOD ; } static long findCnt ( char x , String y ) { long ans = 0 ; long fact [ ] = new long [ y . length ( ) + 1 ] ; for ( int i = 0 ; i < y . length ( ) + 1 ; i ++ ) fact [ i ] = 1 ; for ( int i = 1 ; i <= y . length ( ) ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; for ( int i = 0 ; i < y . length ( ) ; i ++ ) { if ( y . charAt ( i ) == x ) { ans += ( modInv ( fact [ i ] ) * modInv ( fact [ y . length ( ) - i - 1 ] ) ) % MOD ; ans %= MOD ; } } ans *= fact [ ( y . length ( ) - 1 ) ] ; ans %= MOD ; return ans ; } public static void main ( String [ ] args ) { char x = ' a ' ; String y = " xxayy " ; System . out . println ( findCnt ( x , y ) ) ; } }
Find the Largest divisor Subset in the Array | Java implementation of the approach ; Function to find the required subsequence ; Sort the array ; Keep a count of the length of the subsequence and the previous element ; Set the initial values ; Maximum length of the subsequence and the last element ; Run a loop for every element ; Check for all the divisors ; If the element is a divisor and the length of subsequence will increase by adding j as previous element of i ; Increase the count ; Update the max count ; Get the last index of the subsequence ; Print the element ; Move the index to the previous element ; Driver code
import java . util . * ; class GFG { static void findSubSeq ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count [ ] = new int [ n ] ; int prev [ ] = new int [ n ] ; int i , j ; for ( i = 0 ; i < n ; i ++ ) count [ i ] = 1 ; for ( j = 0 ; j < n ; j ++ ) prev [ j ] = - 1 ; int max = 0 ; int maxprev = - 1 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] % arr [ j ] == 0 && count [ j ] + 1 > count [ i ] ) { count [ i ] = count [ j ] + 1 ; prev [ i ] = j ; } } if ( max < count [ i ] ) { max = count [ i ] ; maxprev = i ; } } i = maxprev ; while ( i >= 0 ) { if ( arr [ i ] != - 1 ) System . out . print ( arr [ i ] + " ▁ " ) ; i = prev [ i ] ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; findSubSeq ( arr , n ) ; } }
Minimum number of integers required such that each Segment contains at least one of them | Java program for the above approach ; Function to compute minimum number of points which cover all segments ; Sort the list of tuples by their second element . ; To store the solution ; Iterate over all the segments ; Get the start point of next segment ; Loop over all those segments whose start point is less than the end point of current segment ; Print the possibles values of M ; Driver code ; Starting points of segments ; Ending points of segments ; Insert ranges in points [ ] ; Function call
import java . util . * ; class GFG { static void minPoints ( int [ ] [ ] points , int n ) { Arrays . sort ( points , ( a , b ) -> a [ 1 ] - b [ 1 ] ) ; ArrayList < Integer > coordinates = new ArrayList < > ( ) ; int i = 0 ; while ( i < n ) { int seg = points [ i ] [ 1 ] ; coordinates . add ( seg ) ; int p = i + 1 ; if ( p >= n ) break ; int arrived = points [ p ] [ 0 ] ; while ( seg >= arrived ) { p += 1 ; if ( p >= n ) break ; arrived = points [ p ] [ 0 ] ; } i = p ; } for ( Integer point : coordinates ) System . out . print ( point + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 4 ; int [ ] start = { 4 , 1 , 2 , 5 } ; int [ ] end = { 7 , 3 , 5 , 6 } ; int [ ] [ ] points = new int [ n ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { points [ i ] [ 0 ] = start [ i ] ; points [ i ] [ 1 ] = end [ i ] ; } minPoints ( points , n ) ; } }