codeparrot/xlcost-text-to-code huggingface dataset:
Smallest String consisting of a String S exactly K times as a Substring | Java program to implement the above approach ; KMP algorithm ; Function to return the required string ; Finding the longest proper prefix which is also suffix ; ans string ; Update ans appending the substring K - 1 times ; Append the original string ; Returning min length string which contain exactly k substring of given string ; Driver code
import java . util . * ; class GFG { static int [ ] kmp ( String s ) { int n = s . length ( ) ; int [ ] lps = new int [ n ] ; lps [ 0 ] = 0 ; int i = 1 , len = 0 ; while ( i < n ) { if ( s . charAt ( i ) == s . charAt ( len ) ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } return lps ; } static String findString ( String s , int k ) { int n = s . length ( ) ; int [ ] lps = kmp ( s ) ; String ans = " " ; String suff = s . substring ( 0 , n - lps [ n - 1 ] ) ; for ( int i = 0 ; i < k - 1 ; ++ i ) { ans += suff ; } ans += s ; return ans ; } public static void main ( String [ ] args ) { int k = 3 ; String s = " geeksforgeeks " ; System . out . println ( findString ( s , k ) ) ; } }
Check if an Array can be Sorted by picking only the corner Array elements | Java program to implement the above approach ; Function to check if an array can be sorted using given operations ; If sequence becomes increasing after an already non - decreasing to non - increasing pattern ; If a decreasing pattern is observed ; Driver Code
class GFG { static boolean check ( int arr [ ] , int n ) { int i , g ; g = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 0 && g == 1 ) return false ; if ( arr [ i ] - arr [ i - 1 ] < 0 ) g = 1 ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 10 , 4 , 3 , 1 } ; int n = arr . length ; if ( check ( arr , n ) == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Queries to find the count of connected Non | Java program to implement the above approach ; Count of connected cells ; Function to return the representative of the Set to which x belongs ; If x is parent of itself ; x is representative of the Set ; Otherwise ; Path Compression ; Unites the set that includes x and the set that includes y ; Find the representatives ( or the root nodes ) for x an y ; If both are in the same set ; Decrement count ; If x ' s ▁ rank ▁ is ▁ less ▁ than ▁ y ' s rank ; Otherwise ; Then move x under y ( doesn 't matter which one goes where) ; And increment the result tree 's  rank by 1 ; Function to count the number of connected cells in the matrix ; Store result for queries ; Store representative of each element ; Initially , all elements are in their own set ; Stores the rank ( depth ) of each node ; If the grid [ x * m + y ] is already set , store the result ; Set grid [ x * m + y ] to 1 ; Increment count . ; Check for all adjacent cells to do a Union with neighbour 's  set if neighbour is also 1 ; Store result . ; Driver Code
import java . util . * ; class GFG { static int ctr = 0 ; static int find ( int [ ] parent , int x ) { if ( parent [ x ] == x ) return x ; parent [ x ] = find ( parent , parent [ x ] ) ; return parent [ x ] ; } static void setUnion ( int [ ] parent , int [ ] rank , int x , int y ) { int parentx = find ( parent , x ) ; int parenty = find ( parent , y ) ; if ( parenty == parentx ) return ; ctr -- ; if ( rank [ parentx ] < rank [ parenty ] ) { parent [ parentx ] = parenty ; } else if ( rank [ parentx ] > rank [ parenty ] ) { parent [ parenty ] = parentx ; } else { parent [ parentx ] = parenty ; rank [ parenty ] ++ ; } } static int [ ] solve ( int n , int m , int [ ] [ ] query ) { int [ ] result = new int [ query . length ] ; int [ ] parent = new int [ n * m ] ; for ( int i = 0 ; i < n * m ; i ++ ) parent [ i ] = i ; int [ ] rank = new int [ n * m ] ; Arrays . fill ( rank , 1 ) ; boolean [ ] grid = new boolean [ n * m ] ; for ( int i = 0 ; i < query . length ; i ++ ) { int x = query [ i ] [ 0 ] ; int y = query [ i ] [ 1 ] ; if ( grid [ m * x + y ] == true ) { result [ i ] = ctr ; continue ; } grid [ m * x + y ] = true ; ctr ++ ; if ( x > 0 && grid [ m * ( x - 1 ) + y ] == true ) setUnion ( parent , rank , m * x + y , m * ( x - 1 ) + y ) ; if ( y > 0 && grid [ m * ( x ) + y - 1 ] == true ) setUnion ( parent , rank , m * x + y , m * ( x ) + y - 1 ) ; if ( x < n - 1 && grid [ m * ( x + 1 ) + y ] == true ) setUnion ( parent , rank , m * x + y , m * ( x + 1 ) + y ) ; if ( y < m - 1 && grid [ m * ( x ) + y + 1 ] == true ) setUnion ( parent , rank , m * x + y , m * ( x ) + y + 1 ) ; result [ i ] = ctr ; } return result ; } public static void main ( String [ ] args ) { int N = 3 , M = 3 , K = 4 ; int [ ] [ ] query = { { 0 , 0 } , { 1 , 1 } , { 1 , 0 } , { 1 , 2 } } ; int [ ] result = solve ( N , M , query ) ; for ( int i = 0 ; i < K ; i ++ ) System . out . print ( result [ i ] + " ▁ " ) ; } }
Minimum Sum of a pair at least K distance apart from an Array | Java Program to implement the above approach ; Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Find the suffix array ; Iterate in the array ; Update minimum sum ; Print the answer ; Driver Code
import java . util . * ; class GFG { public static void findMinSum ( int A [ ] , int K ) { int n = A . length ; int suffix_min [ ] = new int [ n ] ; suffix_min [ n - 1 ] = A [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_min [ i ] = Math . min ( suffix_min [ i + 1 ] , A [ i ] ) ; int min_sum = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + K < n ) min_sum = Math . min ( min_sum , A [ i ] + suffix_min [ i + K ] ) ; } System . out . println ( min_sum ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 2 ; findMinSum ( A , K ) ; } }
Maximum number of envelopes that can be put inside other bigger envelopes | Java program for the above approach ; Function that returns the maximum number of envelopes that can be inserted into another envelopes ; Number of envelopes ; Sort the envelopes in non - decreasing order ; Initialize dp [ ] array ; To store the result ; Loop through the array ; Find envelopes count for each envelope ; Store maximum envelopes count ; Return the result ; Driver Code ; Given the envelopes ; Function call
import java . util . * ; import java . lang . * ; class GFG { static int maxEnvelopes ( int [ ] [ ] envelopes ) { int N = envelopes . length ; if ( N == 0 ) return N ; Arrays . sort ( envelopes , ( a , b ) -> ( a [ 0 ] != b [ 0 ] ) ? a [ 0 ] - b [ 0 ] : a [ 1 ] - b [ 1 ] ) ; int [ ] dp = new int [ N ] ; int max_envelope = 1 ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < N ; ++ i ) { dp [ i ] = 1 ; for ( int j = 0 ; j < i ; ++ j ) { if ( envelopes [ i ] [ 0 ] > envelopes [ j ] [ 0 ] && envelopes [ i ] [ 1 ] > envelopes [ j ] [ 1 ] && dp [ i ] < dp [ j ] + 1 ) dp [ i ] = dp [ j ] + 1 ; } max_envelope = Math . max ( max_envelope , dp [ i ] ) ; } return max_envelope ; } public static void main ( String [ ] args ) { int [ ] [ ] envelopes = { { 4 , 3 } , { 5 , 3 } , { 5 , 6 } , { 1 , 2 } } ; System . out . println ( maxEnvelopes ( envelopes ) ) ; } }
Check if each element of an Array is the Sum of any two elements of another Array | Java program to implement the above approach ; Function to check if each element of B [ ] can be formed by adding two elements of array A [ ] ; Store each element of B [ ] ; Traverse all possible pairs of array ; If A [ i ] + A [ j ] is present in the set ; Remove A [ i ] + A [ j ] from the set ; If set is empty ; Otherwise ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static String checkPossible ( int A [ ] , int B [ ] , int n ) { Set values = new HashSet ( ) ; for ( int i = 0 ; i < n ; i ++ ) { values . add ( B [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( values . contains ( A [ i ] + A [ j ] ) ) { values . remove ( A [ i ] + A [ j ] ) ; if ( values . size ( ) == 0 ) break ; } } } if ( values . size ( ) == 0 ) return " Yes " ; else return " No " ; } public static void main ( String args [ ] ) { int N = 5 ; int A [ ] = { 3 , 5 , 1 , 4 , 2 } ; int B [ ] = { 3 , 4 , 5 , 6 , 7 } ; System . out . print ( checkPossible ( A , B , N ) ) ; } }
Count of Array elements greater than or equal to twice the Median of K trailing Array elements | Java Program to implement the above approach ; Function to find the count of array elements >= twice the median of K trailing array elements ; Stores frequencies ; Stores the array elements ; Count the frequencies of the array elements ; Iterating from d to n - 1 index means ( d + 1 ) th element to nth element ; To check the median ; Iterate over the frequencies of the elements ; Add the frequencies ; Check if the low_median value is obtained or not , if yes then do not change as it will be minimum ; Check if the high_median value is obtained or not , if yes then do not change it as it will be maximum ; Store 2 * median of K trailing elements ; If the current >= 2 * median ; Decrease the frequency for ( k - 1 ) - th element ; Increase the frequency of the current element ; Print the count ; Driver Code
class GFG { static int N = ( int ) 2e5 ; static int V = 500 ; static void solve ( int n , int d , int input [ ] ) { int [ ] a = new int [ N ] ; int [ ] cnt = new int [ V + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) a [ i ] = input [ i ] ; int answer = 0 ; for ( int i = 0 ; i < d ; ++ i ) cnt [ a [ i ] ] ++ ; for ( int i = d ; i <= n - 1 ; ++ i ) { int acc = 0 ; int low_median = - 1 , high_median = - 1 ; for ( int v = 0 ; v <= V ; ++ v ) { acc += cnt [ v ] ; if ( low_median == - 1 && acc >= ( int ) ( Math . floor ( ( d + 1 ) / 2.0 ) ) ) low_median = v ; if ( high_median == - 1 && acc >= ( int ) ( Math . ceil ( ( d + 1 ) / 2.0 ) ) ) high_median = v ; } int double_median = low_median + high_median ; if ( a [ i ] >= double_median ) answer ++ ; cnt [ a [ i - d ] ] -- ; cnt [ a [ i ] ] ++ ; } System . out . print ( answer + "NEW_LINE"); } public static void main ( String [ ] args ) { int input [ ] = { 1 , 2 , 2 , 4 , 5 } ; int n = input . length ; int k = 3 ; solve ( n , k , input ) ; } }
Smallest positive integer X satisfying the given equation | Java Program to implement the above approach ; Function to find out the smallest positive integer for the equation ; Stores the minimum ; Iterate till K ; Check if n is divisible by i ; Return the answer ; Driver Code
import java . util . * ; class GFG { static int findMinSoln ( int n , int k ) { int minSoln = Integer . MAX_VALUE ; for ( int i = 1 ; i < k ; i ++ ) { if ( n % i == 0 ) minSoln = Math . min ( minSoln , ( n / i ) * k + i ) ; } return minSoln ; } public static void main ( String [ ] args ) { int n = 4 , k = 6 ; System . out . println ( findMinSoln ( n , k ) ) ; } }
Queries to find the Lower Bound of K from Prefix Sum Array with updates using Fenwick Tree | Java program to implement the above approach ; Function to calculate and return the sum of arr [ 0. . index ] ; Traverse ancestors of BITree [ index ] ; Update the sum of current element of BIT to ans ; Update index to that of the parent node in getSum ( ) view by subtracting LSB ( Least Significant Bit ) ; Function to update the Binary Index Tree by replacing all ancestors of index by their respective sum with val ; Traverse all ancestors and sum with ' val ' . ; Add ' val ' to current node of BIT ; Update index to that of the parent node in updateBit ( ) view by adding LSB ( Least Significant Bit ) ; Function to construct the Binary Indexed Tree for the given array ; Initialize the Binary Indexed Tree ; Store the actual values in BITree [ ] using update ( ) ; Function to obtain and return the index of lower_bound of k ; Store the Binary Indexed Tree ; Solve each query in Q ; Update the values of all ancestors of idx ; Driver Code
import java . util . * ; import java . io . * ; class GFG { static int getSum ( int BITree [ ] , int index ) { int ans = 0 ; index += 1 ; while ( index > 0 ) { ans += BITree [ index ] ; index -= index & ( - index ) ; } return ans ; } static void updateBIT ( int BITree [ ] , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } static int [ ] constructBITree ( int arr [ ] , int n ) { int [ ] BITree = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) BITree [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) updateBIT ( BITree , n , i , arr [ i ] ) ; return BITree ; } static int getLowerBound ( int BITree [ ] , int [ ] arr , int n , int k ) { int lb = - 1 ; int l = 0 , r = n - 1 ; while ( l <= r ) { int mid = l + ( r - l ) / 2 ; if ( getSum ( BITree , mid ) >= k ) { r = mid - 1 ; lb = mid ; } else l = mid + 1 ; } return lb ; } static void performQueries ( int A [ ] , int n , int q [ ] [ ] ) { int [ ] BITree = constructBITree ( A , n ) ; for ( int i = 0 ; i < q . length ; i ++ ) { int id = q [ i ] [ 0 ] ; if ( id == 1 ) { int idx = q [ i ] [ 1 ] ; int val = q [ i ] [ 2 ] ; A [ idx ] += val ; updateBIT ( BITree , n , idx , val ) ; } else { int k = q [ i ] [ 1 ] ; int lb = getLowerBound ( BITree , A , n , k ) ; System . out . println ( lb ) ; } } } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 5 , 8 } ; int n = A . length ; int [ ] [ ] q = { { 1 , 0 , 2 } , { 2 , 5 } , { 1 , 3 , 5 } } ; performQueries ( A , n , q ) ; } }
Minimum number of leaves required to be removed from a Tree to satisfy the given condition | Java Program to find the minimum number of leaves to be deleted ; Stores the count of safe nodes ; Function to perform DFS on the Tree to obtain the count of vertices that are not required to be deleted ; Update cost to reach the vertex ; If the vertex does not satisfy the condition ; Otherwise ; Traverse its subtree ; Driver Code ; Stores the Tree ; Perform DFS ; Print the number of nodes to be deleted
import java . util . * ; class GFG { static int cnt = 0 ; static void dfs ( int [ ] val , int [ ] cost , Vector < Integer > [ ] tr , int u , int s ) { s = s + cost [ u ] ; if ( s < 0 ) s = 0 ; if ( s > val [ u ] ) return ; cnt ++ ; for ( int i = 0 ; i < tr [ u ] . size ( ) ; i ++ ) { dfs ( val , cost , tr , tr [ u ] . get ( i ) , s ) ; } } public static void main ( String [ ] args ) { int n = 9 ; int val [ ] = { 88 , 22 , 83 , 14 , 95 , 91 , 98 , 53 , 11 } ; int cost [ ] = { - 1 , 24 , - 8 , 67 , 64 , 65 , 12 , - 80 , 8 } ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] tr = new Vector [ n + 1 ] ; for ( int i = 0 ; i < tr . length ; i ++ ) tr [ i ] = new Vector < Integer > ( ) ; tr [ 0 ] . add ( 3 ) ; tr [ 0 ] . add ( 4 ) ; tr [ 4 ] . add ( 6 ) ; tr [ 6 ] . add ( 2 ) ; tr [ 2 ] . add ( 1 ) ; tr [ 2 ] . add ( 8 ) ; tr [ 8 ] . add ( 5 ) ; tr [ 5 ] . add ( 7 ) ; dfs ( val , cost , tr , 0 , 0 ) ; System . out . print ( n - cnt ) ; } }
Check if an Array is made up of Subarrays of continuous repetitions of every distinct element | Java Program to implement the above approach ; Function to check if the array is made up of subarrays of repetitions ; Base Case ; Stores the size of current subarray ; If a different element is encountered ; If the previous subarray was a single element ; Reset to new subarray ; Increase size of subarray ; If last element differed from the second last element ; Driver code
class GFG { static boolean ContinuousElements ( int a [ ] , int n ) { if ( n == 1 ) return false ; int curr = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] != a [ i - 1 ] ) { if ( curr == 1 ) return false ; else curr = 0 ; } curr ++ ; } if ( curr == 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 1 , 2 , 2 , 1 , 3 , 3 } ; int n = a . length ; if ( ContinuousElements ( a , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Size of all connected non | Java program to implement the above approach ; Function to find size of all the islands from the given matrix ; Initialize a queue for the BFS traversal ; Iterate until the queue is empty ; Top element of queue ; Pop the element ; Check for boundaries ; Check if current element is 0 ; Check if current element is 1 ; Mark the cell visited ; Incrementing the size ; Traverse all neighbors ; Return the answer ; Function to print size of each connections ; Stores the size of each connected non - empty ; Check if the cell is non - empty ; Function call ; Print the answer ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static class pair { int first , second ; pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int BFS ( int [ ] [ ] mat , int row , int col ) { int area = 0 ; Queue < pair > Q = new LinkedList < > ( ) ; Q . add ( new pair ( row , col ) ) ; while ( ! Q . isEmpty ( ) ) { pair it = Q . peek ( ) ; Q . poll ( ) ; int r = it . first , c = it . second ; if ( r < 0 c < 0 r > 4 c > 4 ) continue ; if ( mat [ r ] == 0 ) continue ; if ( mat [ r ] == 1 ) { mat [ r ] = 0 ; area ++ ; } Q . add ( new pair ( r + 1 , c ) ) ; Q . add ( new pair ( r - 1 , c ) ) ; Q . add ( new pair ( r , c + 1 ) ) ; Q . add ( new pair ( r , c - 1 ) ) ; } return area ; } static void sizeOfConnections ( int [ ] [ ] mat ) { ArrayList < Integer > result = new ArrayList < > ( ) ; for ( int row = 0 ; row < 5 ; ++ row ) { for ( int col = 0 ; col < 5 ; ++ col ) { if ( mat [ row ] [ col ] == 1 ) { int area = BFS ( mat , row , col ) ; result . add ( area ) ; } } } for ( int val : result ) System . out . print ( val + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 , 1 } , { 1 , 0 , 0 , 1 , 1 } , { 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 1 , 1 } } ; sizeOfConnections ( mat ) ; } }
Find a pair in Array with second largest product | Java program for the above approach ; Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Sort the array ; Initialize smallest element of the array ; Initialize largest element of the array ; Print second largest product pair ; Driver Code ; Given array ; Function Call
import java . util . * ; class GFG { static void maxProduct ( int arr [ ] , int N ) { if ( N < 3 ) { return ; } Arrays . sort ( arr ) ; int smallest1 = arr [ 0 ] ; int smallest3 = arr [ 2 ] ; int largest1 = arr [ N - 1 ] ; int largest3 = arr [ N - 3 ] ; if ( smallest1 * smallest3 >= largest1 * largest3 ) { System . out . print ( smallest1 + " ▁ " + smallest3 ) ; } else { System . out . print ( largest1 + " ▁ " + largest3 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 67 , 45 , 160 , 78 } ; int N = arr . length ; maxProduct ( arr , N ) ; } }
Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | Java program to implement the above approach ; Function to find the maximum subarray length of ones ; Stores the length , starting index and ending index of the subarrays ; S : starting index of the sub - array ; Traverse only continuous 1 s ; Calculate length of the sub - array ; v [ i ] [ 0 ] : Length of subarray v [ i ] [ 1 ] : Starting Index of subarray v [ i ] [ 2 ] : Ending Index of subarray ; If no such sub - array exists ; Traversing through the subarrays ; Update maximum length ; v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 : Count of zeros between the two sub - arrays ; Update length of both subarrays to the maximum ; Update length of both subarrays - 1 to the maximum ; Check if the last subarray has the maximum length ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int maxLen ( int A [ ] , int N ) { List < List < Integer > > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 1 ) { int s = i , len ; while ( i < N && A [ i ] == 1 ) { i ++ ; } len = i - s ; v . add ( Arrays . asList ( len , s , i - 1 ) ) ; } } if ( v . size ( ) == 0 ) { return - 1 ; } int ans = 0 ; for ( int i = 0 ; i < v . size ( ) - 1 ; i ++ ) { ans = Math . max ( ans , v . get ( i ) . get ( 0 ) ) ; if ( v . get ( i + 1 ) . get ( 1 ) - v . get ( i ) . get ( 2 ) - 1 == 2 ) { ans = Math . max ( ans , v . get ( i ) . get ( 0 ) + v . get ( i + 1 ) . get ( 0 ) ) ; } if ( v . get ( i + 1 ) . get ( 1 ) - v . get ( i ) . get ( 2 ) - 1 == 1 ) { ans = Math . max ( ans , v . get ( i ) . get ( 0 ) + v . get ( i + 1 ) . get ( 0 ) - 1 ) ; } } ans = Math . max ( v . get ( v . size ( ) - 1 ) . get ( 0 ) , ans ) ; return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 1 , 0 , 0 , 1 } ; int N = arr . length ; System . out . println ( maxLen ( arr , N ) ) ; } }
Detect cycle in Directed Graph using Topological Sort | Java program to implement the above approach ; Stack to store the visited vertices in the Topological Sort ; Store Topological Order ; Adjacency list to store edges ; To ensure visited vertex ; Function to perform DFS ; Set the vertex as visited ; Visit connected vertices ; Push into the stack on complete visit of vertex ; Function to check and return if a cycle exists or not ; Stores the position of vertex in topological order ; Pop all elements from stack ; Push element to get Topological Order ; Pop from the stack ; If parent vertex does not appear first ; Cycle exists ; Return false if cycle does not exist ; Function to add edges from u to v ; Driver code ; Insert edges ; If cycle exist
import java . util . * ; class GFG { static int t , n , m , a ; static Stack < Integer > s ; static ArrayList < Integer > tsort ; static ArrayList < ArrayList < Integer > > adj ; static int [ ] visited = new int [ ( int ) 1e5 + 1 ] ; static void dfs ( int u ) { visited [ u ] = 1 ; for ( Integer it : adj . get ( u ) ) { if ( visited [ it ] == 0 ) dfs ( it ) ; } s . push ( u ) ; } static boolean check_cycle ( ) { Map < Integer , Integer > pos = new HashMap < > ( ) ; int ind = 0 ; while ( ! s . isEmpty ( ) ) { pos . put ( s . peek ( ) , ind ) ; tsort . add ( s . peek ( ) ) ; ind += 1 ; s . pop ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( Integer it : adj . get ( i ) ) { if ( pos . get ( i ) > pos . get ( it ) ) { return true ; } } } return false ; } static void addEdge ( int u , int v ) { adj . get ( u ) . add ( v ) ; } public static void main ( String [ ] args ) { n = 4 ; m = 5 ; s = new Stack < > ( ) ; adj = new ArrayList < > ( ) ; tsort = new ArrayList < > ( ) ; for ( int i = 0 ; i < 4 ; i ++ ) adj . add ( new ArrayList < > ( ) ) ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 2 ) ; addEdge ( 2 , 0 ) ; addEdge ( 2 , 3 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( visited [ i ] == 0 ) { dfs ( i ) ; } } if ( check_cycle ( ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Rearrange an Array such that Sum of same | Java program to implement the above approach ; Function to rearrange the array such that no same - indexed subset have sum equal to that in the original array ; Initialize a vector ; Iterate the array ; Sort the vector ; Shift of elements to the index of its next cyclic element ; Print the answer ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static class pair { int first , second ; pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void printNewArray ( List < Integer > a , int n ) { List < pair > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { v . add ( new pair ( a . get ( i ) , i ) ) ; } Collections . sort ( v , ( pair s1 , pair s2 ) -> { return s1 . first - s2 . first ; } ) ; int ans [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { ans [ v . get ( ( i + 1 ) % n ) . second ] = v . get ( i ) . first ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { List < Integer > a = Arrays . asList ( 4 , 1 , 2 , 5 , 3 ) ; int n = a . size ( ) ; printNewArray ( a , n ) ; } }