codeparrot/xlcost-text-to-code huggingface dataset:
Find a number in minimum steps | Java program to Find a number in minimum steps ; Steps sequence ; Current sum ; Sign of the number ; Basic steps required to get sum >= required value . ; If we have reached ahead to destination . ; If the last step was an odd number , then it has following mechanism for negating a particular number and decreasing the sum to required number Also note that it may require 1 more step in order to reach the sum . ; If the current time instance is even and sum is odd than it takes 2 more steps and few negations in previous elements to reach there . ; Driver Program
import java . util . * ; class GFG { static Vector < Integer > find ( int n ) { Vector < Integer > ans = new Vector < > ( ) ; int sum = 0 ; int i = 1 ; int sign = ( n >= 0 ? 1 : - 1 ) ; n = Math . abs ( n ) ; for ( ; sum < n ; ) { ans . add ( sign * i ) ; sum += i ; i ++ ; } if ( sum > sign * n ) { if ( i % 2 != 0 ) { sum -= n ; if ( sum % 2 != 0 ) { ans . add ( sign * i ) ; sum += i ; i ++ ; } int a = ans . get ( ( sum / 2 ) - 1 ) ; ans . remove ( ( sum / 2 ) - 1 ) ; ans . add ( ( ( sum / 2 ) - 1 ) , a * ( - 1 ) ) ; } else { sum -= n ; if ( sum % 2 != 0 ) { sum -- ; ans . add ( sign * i ) ; ans . add ( sign * - 1 * ( i + 1 ) ) ; } ans . add ( ( sum / 2 ) - 1 , ans . get ( ( sum / 2 ) - 1 ) * - 1 ) ; } } return ans ; } public static void main ( String [ ] args ) { int n = 20 ; if ( n == 0 ) System . out . print ( "Minimum number of Steps: 0 Step sequence : 0 "); else { Vector < Integer > a = find ( n ) ; System . out . print ( " Minimum ▁ number ▁ of ▁ Steps : ▁ " + a . size ( ) + " Step sequence : "); for ( int i : a ) System . out . print ( i + " ▁ " ) ; } } }
Maximum range length such that A [ i ] is maximum in given range for all i from [ 1 , N ] | Java program for above approach ; Function to find maximum range for each i such that arr [ i ] is max in range ; Vector to store the left and right index for each i such that left [ i ] > arr [ i ] and right [ i ] > arr [ i ] ; Traverse the array ; While s . top ( ) . first < a [ i ] remove the top element from the stack ; Modify left [ i ] ; Clear the stack ; Traverse the array to find right [ i ] for each i ; While s . top ( ) . first < a [ i ] remove the top element from the stack ; Modify right [ i ] ; Print the value range for each i ; Driver Code ; Given Input ; Function Call
import java . awt . * ; import java . util . * ; class GFG { static class pair < T , V > { T first ; V second ; } static void MaxRange ( ArrayList < Integer > A , int n ) { int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; Stack < pair < Integer , Integer > > s = new Stack < > ( ) ; pair < Integer , Integer > x = new pair < > ( ) ; x . first = Integer . MAX_VALUE ; x . second = - 1 ; s . push ( x ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( s . peek ( ) . first < A . get ( i ) ) s . pop ( ) ; left [ i ] = s . peek ( ) . second ; pair < Integer , Integer > y = new pair < > ( ) ; y . first = A . get ( i ) ; y . second = i ; s . push ( y ) ; } while ( ! s . empty ( ) ) s . pop ( ) ; pair < Integer , Integer > k = new pair < > ( ) ; k . first = Integer . MAX_VALUE ; k . second = n ; s . push ( k ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( s . peek ( ) . first < A . get ( i ) ) s . pop ( ) ; right [ i ] = s . peek ( ) . second ; pair < Integer , Integer > y = new pair < > ( ) ; y . first = A . get ( i ) ; y . second = i ; s . push ( y ) ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( left [ i ] + 1 ) ; System . out . print ( " ▁ " ) ; System . out . println ( right [ i ] - 1 ) ; } } public static void main ( String [ ] args ) { ArrayList < Integer > arr = new ArrayList < > ( ) ; arr . add ( 1 ) ; arr . add ( 3 ) ; arr . add ( 2 ) ; int n = arr . size ( ) ; MaxRange ( arr , n ) ; } }
Find Nth smallest number having exactly 4 divisors | Java program for the above approach ; Function to find the nth number which has exactly 4 divisors ; The divs [ ] array to store number of divisors of every element ; The vis [ ] array to check if given number is considered or not ; The cnt stores number of elements having exactly 4 divisors ; Iterate while cnt less than n ; If i is a prime ; Iterate in the range [ 2 * i , 1000000 ] with increment of i ; If the number j is already considered ; Dividing currNum by i until currNum % i is equal to 0 ; Case a single prime in its factorization ; Case of two distinct primes which divides j exactly once each ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { static int nthNumber ( int n ) { int divs [ ] = new int [ 1000000 ] ; boolean vis [ ] = new boolean [ 1000000 ] ; int cnt = 0 ; for ( int i = 2 ; cnt < n ; i ++ ) { if ( divs [ i ] == 0 ) { for ( int j = 2 * i ; j < 1000000 ; j += i ) { if ( vis [ j ] ) { continue ; } vis [ j ] = true ; int currNum = j ; int count = 0 ; while ( currNum % i == 0 ) { divs [ j ] ++ ; currNum = currNum / i ; count ++ ; } if ( currNum == 1 && count == 3 && divs [ j ] == 3 ) { cnt ++ ; } else if ( currNum != 1 && divs [ currNum ] == 0 && count == 1 && divs [ j ] == 1 ) { cnt ++ ; } if ( cnt == n ) { return j ; } } } } return - 1 ; } public static void main ( String [ ] args ) { int N = 24 ; System . out . println ( nthNumber ( N ) ) ; } }
Maximum number formed from the digits of given three numbers | Java program for the above approach ; Function to find the maximum number formed by taking the maximum digit at the same position from each number ; Stores the result ; Stores the position value of a digit ; Stores the digit at the unit place ; Stores the digit at the unit place ; Stores the digit at the unit place ; Update A , B and C ; Stores the maximum digit ; Increment ans cur * a ; Update cur ; Return ans ; Driver Code ; Given Input ; Function call
public class GFG { static int findkey ( int A , int B , int C ) { int ans = 0 ; int cur = 1 ; while ( A > 0 ) { int a = A % 10 ; int b = B % 10 ; int c = C % 10 ; A = A / 10 ; B = B / 10 ; C = C / 10 ; int m = Math . max ( a , Math . max ( c , b ) ) ; ans += cur * m ; cur = cur * 10 ; } return ans ; } public static void main ( String args [ ] ) { int A = 3521 , B = 2452 , C = 1352 ; System . out . println ( findkey ( A , B , C ) ) ; } }
Count of distinct GCDs among all the non | Java program for the above approach ; Function to calculate gcd ; Function to calculate the number of distinct GCDs among all non - empty subsequences of an array ; Variables to store the largest element in array and the required count ; Map to store whether a number is present in A ; Calculate largest number in A and mapping A to Mp ; Iterate over all possible values of GCD ; Variable to check current GCD ; Iterate over all multiples of i ; If j is present in A ; Calculate gcd of all encountered multiples of i ; Current GCD is possible ; Return answer ; Driver code ; Input ; Function calling
import java . util . * ; class GFG { static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static int distinctGCDs ( int [ ] arr , int N ) { int M = - 1 , ans = 0 ; HashMap < Integer , Integer > Mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { M = Math . max ( M , arr [ i ] ) ; if ( Mp . containsKey ( arr [ i ] ) ) Mp . put ( arr [ i ] , 1 ) ; else Mp . put ( arr [ i ] , 0 ) ; } for ( int i = 1 ; i <= M ; i ++ ) { int currGcd = 0 ; for ( int j = i ; j <= M ; j += i ) { if ( Mp . containsKey ( j ) ) { currGcd = gcd ( currGcd , j ) ; if ( currGcd == i ) { ans ++ ; break ; } } } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 11 , 14 , 6 , 12 } ; int N = arr . length ; System . out . println ( distinctGCDs ( arr , N ) ) ; } }
Egg Dropping Puzzle | DP |  ; Function to get minimum number of trials needed in worst case with n eggs and k floors ; If there are no floors , then no trials needed . OR if there is one floor , one trial needed . ; We need k trials for one egg and k floors ; Consider all droppings from 1 st floor to kth floor and return the minimum of these values plus 1. ; Driver code
public class GFG { static int eggDrop ( int n , int k ) { if ( k == 1 k == 0 ) return k ; if ( n == 1 ) return k ; int min = Integer . MAX_VALUE ; int x , res ; for ( x = 1 ; x <= k ; x ++ ) { res = Math . max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ; if ( res < min ) min = res ; } return min + 1 ; } public static void main ( String args [ ] ) { int n = 2 , k = 10 ; System . out . print ( " Minimum ▁ number ▁ of ▁ " + " trials ▁ in ▁ worst ▁ case ▁ with ▁ " + n + " ▁ eggs ▁ and ▁ " + k + " ▁ floors ▁ is ▁ " + eggDrop ( n , k ) ) ; } }
K | Java program for the above approach ; Function to find the kth digit from last in an integer n ; If k is less than equal to 0 ; Divide the number n by 10 upto k - 1 times ; If the number n is equal 0 ; Print the right most digit ; Driver code ; Given input ; Function call
import java . io . * ; class GFG { public static void kthDigitFromLast ( int n , int k ) { if ( k <= 0 ) { System . out . println ( - 1 ) ; return ; } while ( ( k - 1 ) > 0 && n > 0 ) { n = n / 10 ; k -- ; } if ( n == 0 ) { System . out . println ( - 1 ) ; } else { System . out . println ( n % 10 ) ; } } public static void main ( String [ ] args ) { int n = 2354 ; int k = 2 ; kthDigitFromLast ( n , k ) ; } }
Edit Distance | DP | A Space efficient Dynamic Programming based Java program to find minimum number operations to convert str1 to str2 ; Create a DP array to memoize result of previous computations ; Base condition when second String is empty then we remove all characters ; Start filling the DP This loop run for every character in second String ; This loop compares the char from second String with first String characters ; if first String is empty then we have to perform add character operation to get second String ; if character from both String is same then we do not perform any operation . here i % 2 is for bound the row number . ; if character from both String is not same then we take the minimum from three specified operation ; after complete fill the DP array if the len2 is even then we end up in the 0 th row else we end up in the 1 th row so we take len2 % 2 to get row ; Driver program
import java . util . * ; class GFG { static void EditDistDP ( String str1 , String str2 ) { int len1 = str1 . length ( ) ; int len2 = str2 . length ( ) ; int [ ] [ ] DP = new int [ 2 ] [ len1 + 1 ] ; for ( int i = 0 ; i <= len1 ; i ++ ) DP [ 0 ] [ i ] = i ; for ( int i = 1 ; i <= len2 ; i ++ ) { for ( int j = 0 ; j <= len1 ; j ++ ) { if ( j == 0 ) DP [ i % 2 ] [ j ] = i ; else if ( str1 . charAt ( j - 1 ) == str2 . charAt ( i - 1 ) ) { DP [ i % 2 ] [ j ] = DP [ ( i - 1 ) % 2 ] [ j - 1 ] ; } else { DP [ i % 2 ] [ j ] = 1 + Math . min ( DP [ ( i - 1 ) % 2 ] [ j ] , Math . min ( DP [ i % 2 ] [ j - 1 ] , DP [ ( i - 1 ) % 2 ] [ j - 1 ] ) ) ; } } } System . out . print ( DP [ len2 % 2 ] [ len1 ] + "NEW_LINE"); } public static void main ( String [ ] args ) { String str1 = " food " ; String str2 = " money " ; EditDistDP ( str1 , str2 ) ; } }
Sum of Bitwise XOR of elements of an array with all elements of another array | Java Program for the above approach ; Function to calculate sum of Bitwise XOR of elements of arr [ ] with k ; Initialize sum to be zero ; Iterate over each set bit ; Stores contribution of i - th bet to the sum ; If the i - th bit is set ; Stores count of elements whose i - th bit is not set ; Update value ; Update value ; Add value to sum ; Move to the next power of two ; Stores the count of elements whose i - th bit is set ; Initialize count to 0 for all positions ; Traverse the array ; Iterate over each bit ; If the i - th bit is set ; Increase count ; Driver Code
import java . util . Arrays ; class GFG { static int xorSumOfArray ( int arr [ ] , int n , int k , int count [ ] ) { int sum = 0 ; int p = 1 ; for ( int i = 0 ; i < 31 ; i ++ ) { int val = 0 ; if ( ( k & ( 1 << i ) ) != 0 ) { int not_set = n - count [ i ] ; val = ( ( not_set ) * p ) ; } else { val = ( count [ i ] * p ) ; } sum += val ; p = ( p * 2 ) ; } return sum ; } static void sumOfXors ( int arr [ ] , int n , int queries [ ] , int q ) { int [ ] count = new int [ 32 ] ; Arrays . fill ( count , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 31 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) != 0 ) count [ j ] ++ ; } } for ( int i = 0 ; i < q ; i ++ ) { int k = queries [ i ] ; System . out . print ( xorSumOfArray ( arr , n , k , count ) + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 2 , 3 } ; int queries [ ] = { 3 , 8 , 7 } ; int n = arr . length ; int q = queries . length ; sumOfXors ( arr , n , queries , q ) ; } }
Factor Tree of a given Number | Java program to conFactor Tree for a given number ; Tree node ; Utility function to create a new tree Node ; Constructs factor tree for given value and stores root of tree at given reference . ; the number is factorized ; If we found a factor , we conleft and right subtrees and return . Since we traverse factors starting from smaller to greater , left child will always have smaller factor ; Iterative method to find the height of Binary Tree ; Base Case ; Print front of queue and remove it from queue ; Driver program ; int val = 48 ; sample value
import java . util . * ; class GFG { static class Node { Node left , right ; int key ; } ; static Node root ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return temp ; } static Node createFactorTree ( Node node_ref , int v ) { ( node_ref ) = newNode ( v ) ; for ( int i = 2 ; i < v / 2 ; i ++ ) { if ( v % i != 0 ) continue ; node_ref . left = createFactorTree ( ( ( node_ref ) . left ) , i ) ; node_ref . right = createFactorTree ( ( ( node_ref ) . right ) , v / i ) ; return node_ref ; } return node_ref ; } static void printLevelOrder ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( q . isEmpty ( ) == false ) { Node node = q . peek ( ) ; System . out . print ( node . key + " ▁ " ) ; q . remove ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; } } public static void main ( String [ ] args ) { root = null ; root = createFactorTree ( root , val ) ; System . out . println ( " Level ▁ order ▁ traversal ▁ of ▁ " + " constructed ▁ factor ▁ tree " ) ; printLevelOrder ( root ) ; } }
Maximum height of an elevation possible such that adjacent matrix cells have a difference of at most height 1 | Java program for the above approach ; Utility function to find the matrix having the maximum height ; Stores index pairs for bfs ; Stores info about the visited cells ; Traverse the matrix ; Breadth First Search ; x & y are the row & column of current cell ; Check all the 4 adjacent cells and marking them as visited if not visited yet also marking their height as 1 + height of cell ( x , y ) ; Function to find the matrix having the maximum height ; Stores output matrix ; Calling the helper function ; Print the final output matrix ; Driver Code ; Given matrix ; Function call to find the matrix having the maximum height
import java . util . * ; class GFG { static final int M = 3 ; static final int N = 3 ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findHeightMatrixUtil ( int mat [ ] [ ] , int height [ ] [ ] ) { Queue < pair > q = new LinkedList < > ( ) ; int [ ] [ ] vis = new int [ M ] [ N ] ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { q . add ( new pair ( i , j ) ) ; height [ i ] [ j ] = 0 ; vis [ i ] [ j ] = 1 ; } } } while ( q . isEmpty ( ) == false ) { pair k = q . peek ( ) ; q . remove ( ) ; int x = k . first , y = k . second ; if ( x > 0 && vis [ x - 1 ] [ y ] == 0 ) { height [ x - 1 ] [ y ] = height [ x ] [ y ] + 1 ; vis [ x - 1 ] [ y ] = 1 ; q . add ( new pair ( x - 1 , y ) ) ; } if ( y > 0 && vis [ x ] [ y - 1 ] == 0 ) { height [ x ] [ y - 1 ] = height [ x ] [ y ] + 1 ; vis [ x ] [ y - 1 ] = 1 ; q . add ( new pair ( x , y - 1 ) ) ; } if ( x < M - 1 && vis [ x + 1 ] [ y ] == 0 ) { height [ x + 1 ] [ y ] = height [ x ] [ y ] + 1 ; vis [ x + 1 ] [ y ] = 1 ; q . add ( new pair ( x + 1 , y ) ) ; } if ( y < N - 1 && vis [ x ] [ y + 1 ] == 0 ) { height [ x ] [ y + 1 ] = height [ x ] [ y ] + 1 ; vis [ x ] [ y + 1 ] = 1 ; q . add ( new pair ( x , y + 1 ) ) ; } } } static void findHeightMatrix ( int mat [ ] [ ] ) { int [ ] [ ] height = new int [ M ] [ N ] ; findHeightMatrixUtil ( mat , height ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( height [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 0 } } ; findHeightMatrix ( mat ) ; } }
Median of all nodes from a given range in a Binary Search Tree ( BST ) | Java program for the above approach ; Tree Node structure ; Function to create a new BST node ; Function to insert a new node with given key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Return the node pointer ; Function to find all the nodes that lies over the range [ node1 , node2 ] ; If the tree is empty , return ; Traverse for the left subtree ; If a second node is found , then update the flag as false ; Traverse the right subtree ; Function to find the median of all the values in the given BST that lies over the range [ node1 , node2 ] ; Stores all the nodes in the range [ node1 , node2 ] ; Store the size of the array ; Print the median of array based on the size of array ; Driver Code ; Given BST
import java . util . * ; class GFG { static class Node { Node left , right ; int key ; } ; static Vector < Integer > interNodes = new Vector < Integer > ( ) ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return temp ; } static Node insertNode ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insertNode ( node . left , key ) ; else if ( key > node . key ) node . right = insertNode ( node . right , key ) ; return node ; } static void getIntermediateNodes ( Node root , int node1 , int node2 ) { if ( root == null ) return ; getIntermediateNodes ( root . left , node1 , node2 ) ; if ( root . key <= node2 && root . key >= node1 ) { interNodes . add ( root . key ) ; } getIntermediateNodes ( root . right , node1 , node2 ) ; } static float findMedian ( Node root , int node1 , int node2 ) { getIntermediateNodes ( root , node1 , node2 ) ; int nSize = interNodes . size ( ) ; return ( nSize % 2 == 1 ) ? ( float ) interNodes . get ( nSize / 2 ) : ( float ) ( interNodes . get ( ( nSize - 1 ) / 2 ) + interNodes . get ( nSize / 2 ) ) / 2 ; } public static void main ( String [ ] args ) { Node root = null ; root = insertNode ( root , 8 ) ; insertNode ( root , 3 ) ; insertNode ( root , 1 ) ; insertNode ( root , 6 ) ; insertNode ( root , 4 ) ; insertNode ( root , 11 ) ; insertNode ( root , 15 ) ; System . out . print ( findMedian ( root , 3 , 11 ) ) ; } }
Count number of pairs ( i , j ) up to N that can be made equal on multiplying with a pair from the range [ 1 , N / 2 ] | Java program for the above approach ; Function to compute totient of all numbers smaller than or equal to N ; Iterate over the range [ 2 , N ] ; If phi [ p ] is not computed already , then p is prime ; Phi of a prime number p is ( p - 1 ) ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Function to count the pairs ( i , j ) from the range [ 1 , N ] , satisfying the given condition ; Stores the counts of first and second type of pairs respectively ; Count of first type of pairs ; Stores the phi or totient values ; Calculate the Phi values ; Iterate over the range [ N / 2 + 1 , N ] ; Update the value of cnt_type2 ; Print the total count ; Driver Code
import java . util . * ; class GFG { static void computeTotient ( int N , int phi [ ] ) { for ( int p = 2 ; p <= N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i <= N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } static void countPairs ( int N ) { int cnt_type1 = 0 , cnt_type2 = 0 ; int half_N = N / 2 ; cnt_type1 = ( half_N * ( half_N - 1 ) ) / 2 ; int [ ] phi = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { phi [ i ] = i ; } computeTotient ( N , phi ) ; for ( int i = ( N / 2 ) + 1 ; i <= N ; i ++ ) cnt_type2 += ( i - phi [ i ] - 1 ) ; System . out . print ( cnt_type1 + cnt_type2 ) ; } public static void main ( String [ ] args ) { int N = 6 ; countPairs ( N ) ; } }
Sum of subsets nearest to K possible from two given arrays | java program for the above approach ; Stores the sum closest to K ; Stores the minimum absolute difference ; Function to choose the elements from the array B [ ] ; If absolute difference is less then minimum value ; Update the minimum value ; Update the value of ans ; If absolute difference between curr and K is equal to minimum ; Update the value of ans ; If i is greater than M - 1 ; Includes the element B [ i ] once ; Includes the element B [ i ] twice ; Excludes the element B [ i ] ; Function to find a subset sum whose sum is closest to K ; Traverse the array A [ ] ; Function Call ; Return the ans ; Driver Code ; Input ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int ans = Integer . MAX_VALUE ; static int mini = Integer . MAX_VALUE ; static void findClosestTarget ( int i , int curr , int B [ ] , int M , int K ) { if ( Math . abs ( curr - K ) < mini ) { mini = Math . abs ( curr - K ) ; ans = curr ; } if ( Math . abs ( curr - K ) == mini ) { ans = Math . min ( ans , curr ) ; } if ( i >= M ) return ; findClosestTarget ( i + 1 , curr + B [ i ] , B , M , K ) ; findClosestTarget ( i + 1 , curr + 2 * B [ i ] , B , M , K ) ; findClosestTarget ( i + 1 , curr , B , M , K ) ; } static int findClosest ( int A [ ] , int B [ ] , int N , int M , int K ) { for ( int i = 0 ; i < N ; i ++ ) { findClosestTarget ( 0 , A [ i ] , B , M , K ) ; } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 3 } ; int B [ ] = { 4 , 5 , 30 } ; int N = A . length ; int M = B . length ; int K = 18 ; System . out . print ( findClosest ( A , B , N , M , K ) ) ; } }
Check if every node can me made accessible from a node of a Tree by at most N / 2 given operations | Java program for the above approach ; Function to check if there is a node in tree from where all other nodes are accessible or not ; Store the indegree of every node ; Store the nodes having indegree equal to 0 ; Traverse the array ; If the indegree of i - th node is 0 ; Increment count0 by 1 ; If the number of operations needed is at most floor ( n / 2 ) ; Otherwise ; Driver Code ; Given number of nodes ; Given Directed Tree
import java . io . * ; import java . util . HashMap ; class GFG { public static void findNode ( HashMap < Integer , Integer > map , int n ) { int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = map . getOrDefault ( i + 1 , 0 ) ; } int count0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count0 ++ ; } } count0 -= 1 ; if ( count0 <= Math . floor ( ( ( double ) n ) / ( ( double ) 2 ) ) ) { System . out . println ( " Yes " ) ; } else System . out . println ( " No ▁ " ) ; } public static void main ( String [ ] args ) { int N = 3 ; HashMap < Integer , Integer > map = new HashMap < > ( ) ; map . put ( 1 , 0 ) ; map . put ( 2 , 2 ) ; map . put ( 3 , 0 ) ; findNode ( map , N ) ; } }