codeparrot/xlcost-text-to-code huggingface dataset:
Minimum non | Java program for above approach ; Function to find minimum flips required to remove all 0 s from a given binary string ; Length of given string ; Stores the indices of previous 0 s ; Stores the minimum operations ; Traverse string to find minimum operations to obtain required string ; Current character ; If current character is '0' ; Update temp1 with current index , if both temp variables are empty ; Update temp2 with current index , if temp1 contains prev index but temp2 is empty ; If temp1 is not empty ; Reset temp1 to - 1 ; Increase ans ; If temp2 is not empty but temp1 is empty ; Reset temp2 to - 1 ; Increase ans ; If both temp variables are not empty ; Otherwise ; Return the answer ; Driver Code
import java . util . * ; class GFG { static int minOperation ( String s ) { int n = s . length ( ) ; int temp1 = - 1 , temp2 = - 1 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr = s . charAt ( i ) ; if ( curr == '0' ) { if ( temp1 == - 1 && temp2 == - 1 ) { temp1 = i ; } else if ( temp1 != - 1 && temp2 == - 1 && i - temp1 == 1 ) { temp2 = i ; } else if ( temp1 != - 1 ) { temp1 = - 1 ; ans ++ ; } else if ( temp1 == - 1 && temp2 != - 1 && i - temp2 != 1 ) { temp2 = - 1 ; ans ++ ; } } } if ( temp1 != - 1 && temp2 != - 1 ) { ans += 2 ; } else if ( temp1 != - 1 temp2 != - 1 ) { ans += 1 ; } return ans ; } public static void main ( String [ ] args ) { String s = "110010" ; System . out . println ( minOperation ( s ) ) ; } }
Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array | Java program for above approach ; Function to find the minimum length of prefix required to be deleted ; Stores index to be returned ; Iterate until previous element is <= current index ; Decrementing index ; Return index ; Driver code ; Given arr [ ] ; Function call
import java . util . * ; import java . io . * ; class GFG { static int findMinLength ( int [ ] arr ) { int index = ( int ) arr . length - 1 ; while ( index > 0 && arr [ index ] >= arr [ index - 1 ] ) { index -- ; } return index ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 8 , 5 , 0 , - 1 , - 1 , 0 , 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( findMinLength ( arr ) ) ; } }
Count array elements exceeding sum of preceding K elements | Java program for the above approach ; Function to count array elements exceeding sum of preceding K elements ; Iterate over the array ; Update prefix sum ; Check if arr [ K ] > arr [ 0 ] + . . + arr [ K - 1 ] ; Increment count ; Check if arr [ i ] > arr [ i - K - 1 ] + . . + arr [ i - 1 ] ; Increment count ; Driver Code ; Given array
class GFG { static int countPrecedingK ( int a [ ] , int n , int K ) { int [ ] prefix = new int [ n ] ; prefix [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + a [ i ] ; } int ctr = 0 ; if ( prefix [ K - 1 ] < a [ K ] ) ctr ++ ; for ( int i = K + 1 ; i < n ; i ++ ) { if ( prefix [ i - 1 ] - prefix [ i - K - 1 ] < a [ i ] ) ctr ++ ; } return ctr ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 8 , 10 , - 2 , 7 , 5 , 5 , 9 , 15 } ; int N = arr . length ; int K = 2 ; System . out . print ( countPrecedingK ( arr , N , K ) ) ; } }
Count minimum moves required to convert A to B | Java program for the above approach ; Function to find minimum number of moves to obtained B from A ; Stores the minimum number of moves ; Absolute difference ; K is in range [ 0 , 10 ] ; Print the required moves ; Driver Code
import java . io . * ; class GFG { static void convertBfromA ( int a , int b ) { int moves = 0 ; int x = Math . abs ( a - b ) ; for ( int i = 10 ; i > 0 ; i -- ) { moves += x / i ; x = x % i ; } System . out . print ( moves + " ▁ " ) ; } public static void main ( String [ ] args ) { int A = 188 , B = 4 ; convertBfromA ( A , B ) ; } }
Count N digits numbers with sum divisible by K | Java program to implement the above approach ; Function to count the N digit numbers whose sum is divisible by K ; Base case ; If already computed subproblem occurred ; Store the count of N digit numbers whoe sum is divisible by K ; Check if the number does not contain any leading 0. ; Recurrence relation ; Driver code ; Stores the values of overlapping subproblems
import java . util . * ; import java . lang . * ; class GFG { static final int M = 1000 ; static int countNum ( int N , int sum , int K , int st , int dp [ ] [ ] [ ] ) { if ( N == 0 && sum == 0 ) { return 1 ; } if ( N < 0 ) { return 0 ; } if ( dp [ N ] [ sum ] [ st ] != - 1 ) { return dp [ N ] [ sum ] [ st ] ; } int res = 0 ; int start = st == 1 ? 0 : 1 ; for ( int i = start ; i <= 9 ; i ++ ) { res += countNum ( N - 1 , ( sum + i ) % K , K , ( ( st i ) > 0 ) ? 1 : 0 , dp ) ; } return dp [ N ] [ sum ] [ st ] = res ; } public static void main ( String [ ] args ) { int N = 2 , K = 7 ; int [ ] [ ] [ ] dp = new int [ M ] [ M ] [ 2 ] ; for ( int [ ] [ ] i : dp ) for ( int [ ] j : i ) Arrays . fill ( j , - 1 ) ; System . out . print ( countNum ( N , 0 , K , 0 , dp ) ) ; } }
Minimize Nth term of an Arithmetic progression ( AP ) | Java program to implement the above approach ; Function to find the smallest Nth term of an AP possible ; Stores the smallest Nth term ; Check if common difference of AP is an integer ; Store the common difference ; Store the First Term of that AP ; Store the Nth term of that AP ; Check if all elements of an AP are positive ; Return the least Nth term obtained ; Driver Code
import java . io . * ; class GFG { static int smallestNth ( int A , int B , int N ) { int res = Integer . MAX_VALUE ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = N ; j > i ; j -- ) { if ( ( B - A ) % ( j - i ) == 0 ) { int D = ( B - A ) / ( j - i ) ; int FirstTerm = A - ( i - 1 ) * D ; int NthTerm = FirstTerm + ( N - 1 ) * D ; if ( FirstTerm > 0 ) res = Math . min ( res , NthTerm ) ; } } } return res ; } public static void main ( String [ ] args ) { int N = 3 ; int A = 1 ; int B = 6 ; System . out . print ( smallestNth ( A , B , N ) ) ; } }
Subarray of size K with prime sum | Java program to implement the above approach ; Generate all prime numbers in the range [ 1 , 1000000 ] ; Set all numbers as prime initially ; Mark 0 and 1 as non - prime ; If current element is prime ; Mark all its multiples as non - prime ; Function to print the subarray whose sum of elements is prime ; Store the current subarray of size K ; Calculate the sum of first K elements ; Check if currSum is prime ; Store the start and last index of subarray of size K ; Iterate over remaining array ; Check if currSum ; Driver Code
import java . util . * ; class GFG { static void sieve ( boolean prime [ ] ) { for ( int i = 0 ; i < 1000000 ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= 1000000 ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j < 1000000 ; j += i ) { prime [ j ] = false ; } } } } static void subPrimeSum ( int N , int K , int arr [ ] , boolean prime [ ] ) { int currSum = 0 ; for ( int i = 0 ; i < K ; i ++ ) { currSum += arr [ i ] ; } if ( prime [ currSum ] ) { for ( int i = 0 ; i < K ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } return ; } int st = 1 , en = K ; while ( en < N ) { currSum += arr [ en ] - arr [ st - 1 ] ; if ( prime [ currSum ] ) { for ( int i = st ; i <= en ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } return ; } en ++ ; st ++ ; } } public static void main ( String [ ] args ) { int arr [ ] = { 20 , 7 , 5 , 4 , 3 , 11 , 99 , 87 , 23 , 45 } ; int K = 4 ; int N = arr . length ; boolean [ ] prime = new boolean [ 1000000 ] ; sieve ( prime ) ; subPrimeSum ( N , K , arr , prime ) ; } }
Minimum characters required to be removed to sort binary string in ascending order | Java program for the above approach ; Function to find the minimum count of characters to be removed to make the string sorted in ascending order ; Length of given string ; Stores the first occurrence of '1' ; Stores the last occurrence of '0' ; Traverse the string to find the first occurrence of '1' ; Traverse the string to find the last occurrence of '0' ; Return 0 if the str have only one type of character ; Initialize count1 and count0 to count '1' s before lastIdx0 and '0' s after firstIdx1 ; Traverse the string to count0 ; Traverse the string to count1 ; Return the minimum of count0 and count1 ; Driver Code ; Given string str ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static int minDeletion ( String str ) { int n = str . length ( ) ; int firstIdx1 = - 1 ; int lastIdx0 = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '1' ) { firstIdx1 = i ; break ; } } for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == '0' ) { lastIdx0 = i ; break ; } } if ( firstIdx1 == - 1 lastIdx0 == - 1 ) return 0 ; int count1 = 0 , count0 = 0 ; for ( int i = 0 ; i < lastIdx0 ; i ++ ) { if ( str . charAt ( i ) == '1' ) { count1 ++ ; } } for ( int i = firstIdx1 + 1 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '1' ) { count0 ++ ; } } return Math . min ( count0 , count1 ) ; } public static void main ( String [ ] args ) { String str = "1000101" ; System . out . println ( minDeletion ( str ) ) ; } }
Maximum score assigned to a subsequence of numerically consecutive and distinct array elements | Java program for the above approach ; Function to find the maximum score with unique element in the subset ; Base Case ; Check if the previously picked element differs by 1 from the current element ; Calculate score by including the current element ; Calculate score by excluding the current element ; Return maximum of the two possibilities ; Driver Code ; Given arrays ; Function Call
import java . io . * ; class GFG { public static int maximumSum ( int [ ] a , int [ ] b , int n , int index , int lastpicked ) { if ( index == n ) return 0 ; int option1 = 0 , option2 = 0 ; if ( lastpicked == - 1 a [ lastpicked ] != a [ index ] ) option1 = b [ index ] + maximumSum ( a , b , n , index + 1 , index ) ; option2 = maximumSum ( a , b , n , index + 1 , lastpicked ) ; return Math . max ( option1 , option2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 3 , 1 } ; int brr [ ] = { - 1 , 2 , 10 , 20 , - 10 , - 9 } ; int N = arr . length ; System . out . println ( maximumSum ( arr , brr , N , 0 , - 1 ) ) ; } }
Replace every element in a circular array by sum of next K elements | Java program for the above approach ; Function to print the required resultant array ; Reverse the array ; Traverse the range ; Store prefix sum ; Find the prefix sum ; Store the answer ; Calculate the answers ; Count of remaining elements ; Add the sum of all elements y times ; Add the remaining elements ; Update ans [ i ] ; If array is reversed print ans [ ] in reverse ; Driver Code ; Given array arr [ ] ; Given K ; Function
import java . io . * ; class GFG { static void sumOfKElements ( int arr [ ] , int n , int k ) { boolean rev = false ; if ( k < 0 ) { rev = true ; k *= - 1 ; int l = 0 , r = n - 1 ; while ( l < r ) { int tmp = arr [ l ] ; arr [ l ] = arr [ r ] ; arr [ r ] = tmp ; l ++ ; r -- ; } } int dp [ ] = new int [ n ] ; dp [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] += dp [ i - 1 ] + arr [ i ] ; } int ans [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + k < n ) ans [ i ] = dp [ i + k ] - dp [ i ] ; else { int x = k - ( n - 1 - i ) ; int y = x / n ; int rem = x % n ; ans [ i ] = dp [ n - 1 ] - dp [ i ] + y * dp [ n - 1 ] + ( rem - 1 >= 0 ? dp [ rem - 1 ] : 0 ) ; } } if ( rev ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } else { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , - 5 , 11 } ; int N = arr . length ; int K = 3 ; sumOfKElements ( arr , N , K ) ; } }
Count subarrays for every array element in which they are the minimum | Java program for the above approach ; Function to find the boundary of every element within which it is minimum ; Perform Binary Search ; Find mid m ; Update l ; Update r ; Inserting the index ; Function to required count subarrays ; Stores the indices of element ; Initialize the output array ; Left boundary , till the element is smallest ; Right boundary , till the element is smallest ; Calculate the number of subarrays based on its boundary ; Adding cnt to the ans ; Driver code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static int binaryInsert ( ArrayList < Integer > boundary , int i ) { int l = 0 ; int r = boundary . size ( ) - 1 ; while ( l <= r ) { int m = ( l + r ) / 2 ; if ( boundary . get ( m ) < i ) l = m + 1 ; else r = m - 1 ; } boundary . add ( l , i ) ; return l ; } static int [ ] countingSubarray ( int [ ] arr , int n ) { Map < Integer , Integer > index = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) index . put ( arr [ i ] , i ) ; ArrayList < Integer > boundary = new ArrayList < > ( ) ; boundary . add ( - 1 ) ; boundary . add ( n ) ; Arrays . sort ( arr ) ; int [ ] ans = new int [ n ] ; for ( int num : arr ) { int i = binaryInsert ( boundary , index . get ( num ) ) ; int l = boundary . get ( i ) - boundary . get ( i - 1 ) - 1 ; int r = boundary . get ( i + 1 ) - boundary . get ( i ) - 1 ; int cnt = l + r + l * r + 1 ; ans [ index . get ( num ) ] += cnt ; } return ans ; } public static void main ( String [ ] args ) { int N = 5 ; int [ ] arr = { 3 , 2 , 4 , 1 , 5 } ; int [ ] a = countingSubarray ( arr , N ) ; System . out . print ( " [ " ) ; int n = a . length - 1 ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " , ▁ " ) ; System . out . print ( a [ n ] + " ] " ) ; } }
Check if digit cube limit of an integer arrives at fixed point or a limit cycle | Java program for the above approach ; Define the limit ; Function to get the sum of cube of digits of a number ; Convert to String to get sum of the cubes of its digits ; return sum ; Function to check if the number arrives at a fixed point or a cycle ; Stores the values obtained ; Insert N to set s ; Get the next number using F ( N ) ; Check if the next number is repeated or not ; Function to check if digit cube limit of an integer arrives at fixed point or in a limit cycle ; N is a non negative integer ; Function Call ; If the value received is greater than limit ; If the value received is an Armstrong number ; Driver Code ; Function Call
import java . util . * ; class GFG { static final int limit = 1000000000 ; static int F ( int N ) { String str = String . valueOf ( N ) ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int val = ( int ) ( str . charAt ( i ) - '0' ) ; sum += val * val * val ; } return sum ; } static int findDestination ( int N ) { HashSet < Integer > s = new HashSet < > ( ) ; int prev = N , next = 0 ; s . add ( N ) ; while ( N <= limit ) { next = F ( N ) ; if ( s . contains ( next ) ) { return next ; } prev = next ; s . add ( prev ) ; N = next ; } return next ; } static void digitCubeLimit ( int N ) { if ( N < 0 ) System . out . print ( "N cannot be negativeNEW_LINE"); else { int ans = findDestination ( N ) ; if ( ans > limit ) System . out . print ( "Limit exceededNEW_LINE"); else if ( ans == F ( ans ) ) { System . out . print ( N ) ; System . out . print ( " ▁ reaches ▁ to ▁ a " + " ▁ fixed ▁ point : ▁ " ) ; System . out . print ( ans ) ; } else { System . out . print ( N ) ; System . out . print ( " ▁ reaches ▁ to ▁ a " + " ▁ limit ▁ cycle : ▁ " ) ; System . out . print ( ans ) ; } } } public static void main ( String [ ] args ) { int N = 3 ; digitCubeLimit ( N ) ; } }
Count quadruples ( i , j , k , l ) in an array such that i < j < k < l and arr [ i ] = arr [ k ] and arr [ j ] = arr [ l ] | Java program for the above approach ; Function to count total number of required tuples ; Initialize unordered map ; Find the pairs ( j , l ) such that arr [ j ] = arr [ l ] and j < l ; elements are equal ; Update the count ; Add the frequency of arr [ l ] to val ; Update the frequency of element arr [ j ] ; Return the answer ; Driver code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { static int countTuples ( int arr [ ] , int N ) { int ans = 0 , val = 0 ; HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int j = 0 ; j < N - 2 ; j ++ ) { val = 0 ; for ( int l = j + 1 ; l < N ; l ++ ) { if ( arr [ j ] == arr [ l ] ) { ans += val ; } if ( freq . containsKey ( arr [ l ] ) ) val += freq . get ( arr [ l ] ) ; } if ( freq . containsKey ( arr [ j ] ) ) { freq . put ( arr [ j ] , freq . get ( arr [ j ] ) + 1 ) ; } else { freq . put ( arr [ j ] , 1 ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 2 , 2 , 2 } ; int N = arr . length ; System . out . print ( countTuples ( arr , N ) ) ; } }
Minimize subarray increments / decrements required to reduce all array elements to 0 | Java program of the above approach ; Function to count the minimum number of operations required ; Traverse the array ; If array element is negative ; Update minimum negative ; Update maximum positive ; Return minOp ; Driver Code
import java . util . * ; import java . lang . * ; class GFG { static int minOperation ( int [ ] arr ) { int minOp = Integer . MIN_VALUE ; int minNeg = 0 , maxPos = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] < 0 ) { if ( arr [ i ] < minNeg ) minNeg = arr [ i ] ; } else { if ( arr [ i ] > maxPos ) maxPos = arr [ i ] ; } } return Math . abs ( minNeg ) + maxPos ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 4 , 1 } ; System . out . println ( minOperation ( arr ) ) ; } }
Pair of integers having least GCD among all given pairs having GCD exceeding K | Java program for the above approach ; Function to calculate the GCD of two numbers ; Function to print the pair having a gcd value just greater than the given integer ; Initialize variables ; Iterate until low less than equal to high ; Calculate mid ; Reducing the search space ; Print the answer ; Driver Code ; Given array and K ; Function Call
import java . util . * ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) { return a ; } return GCD ( b , a % b ) ; } static void GcdPair ( int [ ] [ ] arr , int k ) { int lo = 0 , hi = arr . length - 1 , mid ; int [ ] ans = { - 1 , 0 } ; while ( lo <= hi ) { mid = lo + ( hi - lo ) / 2 ; if ( GCD ( arr [ mid ] [ 0 ] , arr [ mid ] [ 1 ] ) > k ) { ans = arr [ mid ] ; hi = mid - 1 ; } else lo = mid + 1 ; } if ( ans [ 0 ] == - 1 ) System . out . print ( " - 1" ) ; else System . out . print ( " ( ▁ " + ans [ 0 ] + " , ▁ " + ans [ 1 ] + " ▁ ) " ) ; return ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 3 , 6 } , { 15 , 30 } , { 25 , 75 } , { 30 , 120 } } ; int K = 16 ; GcdPair ( arr , K ) ; } }
Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array | Java implementation of the above approach ; Function to check if it is possible to sort array or not ; Store the smallest element ; Copy the original array ; Iterate over the given array ; Update smallest element ; Copy elements of arr [ ] to array B [ ] ; Sort original array ; Iterate over the given array ; If the i - th element is not in its sorted place ; Not possible to swap ; Driver Code ; Given array ; Function Call
import java . util . * ; class GFG { static void isPossible ( int arr [ ] , int N ) { int mn = Integer . MAX_VALUE ; int [ ] B = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { mn = Math . min ( mn , arr [ i ] ) ; B [ i ] = arr [ i ] ; } Arrays . sort ( arr ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != B [ i ] ) { if ( B [ i ] % mn != 0 ) { System . out . print ( " No " ) ; return ; } } } System . out . print ( " Yes " ) ; return ; } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = { 4 , 3 , 6 , 6 , 2 , 9 } ; isPossible ( arr , N ) ; } }
Check if every vertex triplet in graph contains two vertices connected to third vertex | Java program of the above approach ; Function to add edge into the graph ; Finding the maximum and minimum values in each component ; Function for checking whether the given graph is valid or not ; Checking for intersecting intervals ; If intersection is found ; Graph is not valid ; Function for the DFS Traversal ; Traversing for every vertex ; Storing maximum and minimum values of each component ; 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 void addEdge ( ArrayList < ArrayList < Integer > > adj , int u , int v ) { adj . get ( u ) . add ( v ) ; adj . get ( v ) . add ( u ) ; } static void DFSUtil ( int u , ArrayList < ArrayList < Integer > > adj , boolean [ ] visited , int componentMin , int componentMax ) { visited [ u ] = true ; componentMax = Math . max ( componentMax , u ) ; componentMin = Math . min ( componentMin , u ) ; for ( int i = 0 ; i < adj . get ( u ) . size ( ) ; i ++ ) if ( visited [ adj . get ( u ) . get ( i ) ] == false ) DFSUtil ( adj . get ( u ) . get ( i ) , adj , visited , componentMin , componentMax ) ; } static boolean isValid ( ArrayList < pair > v ) { int MAX = - 1 ; boolean ans = false ; for ( pair i : v ) { if ( i . first <= MAX ) { ans = true ; } MAX = Math . max ( MAX , i . second ) ; } return ( ans == false ? true : false ) ; } static void DFS ( ArrayList < ArrayList < Integer > > adj , int V ) { ArrayList < pair > v = new ArrayList < > ( ) ; boolean [ ] visited = new boolean [ V + 1 ] ; for ( int u = 1 ; u <= V ; u ++ ) { if ( visited [ u ] == false ) { int componentMax = u ; int componentMin = u ; DFSUtil ( u , adj , visited , componentMin , componentMax ) ; v . add ( new pair ( componentMin , componentMax ) ) ; } } boolean check = isValid ( v ) ; if ( check ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; return ; } public static void main ( String [ ] args ) { int N = 4 , K = 3 ; ArrayList < ArrayList < Integer > > adj = new ArrayList < > ( ) ; for ( int i = 0 ; i <= N + 1 ; i ++ ) adj . add ( new ArrayList < > ( ) ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 2 , 3 ) ; addEdge ( adj , 3 , 4 ) ; DFS ( adj , N ) ; } }
Check if all array elements are present in a given stack or not | Java program of the above approach ; Function to check if all array elements is present in the stack ; Store the frequency of array elements ; Loop while the elements in the stack is not empty ; Condition to check if the element is present in the stack ; Driver code
import java . util . * ; class GFG { static boolean checkArrInStack ( Stack < Integer > s , int arr [ ] , int n ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( freq . containsKey ( arr [ i ] ) ) freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; else freq . put ( arr [ i ] , 1 ) ; while ( ! s . isEmpty ( ) ) { int poppedEle = s . peek ( ) ; s . pop ( ) ; if ( freq . containsKey ( poppedEle ) ) freq . put ( poppedEle , freq . get ( poppedEle ) - 1 ) ; } if ( freq . size ( ) == 0 ) return false ; return true ; } public static void main ( String [ ] args ) { Stack < Integer > s = new Stack < Integer > ( ) ; s . add ( 10 ) ; s . add ( 20 ) ; s . add ( 30 ) ; s . add ( 40 ) ; s . add ( 50 ) ; int arr [ ] = { 20 , 30 } ; int n = arr . length ; if ( checkArrInStack ( s , arr , n ) ) System . out . print ( "YESNEW_LINE"); else System . out . print ( "NONEW_LINE"); } }