codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of swaps required to sort an array | Java program to find minimum number of swaps required to sort an array ; swap function ; indexOf function ; Return the minimum number of swaps required to sort the array ; This is checking whether the current element is at the right place or not ; Swap the current element with the right index so that arr [ 0 ] to arr [ i ] is sorted ; Driver class ; Driver program to test the above function ; Output will be 5
import java . util . * ; import java . io . * ; class GfG { public void swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } public int indexOf ( int [ ] arr , int ele ) { for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == ele ) { return i ; } } return - 1 ; } public int minSwaps ( int [ ] arr , int N ) { int ans = 0 ; int [ ] temp = Arrays . copyOfRange ( arr , 0 , N ) ; Arrays . sort ( temp ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != temp [ i ] ) { ans ++ ; swap ( arr , i , indexOf ( arr , temp [ i ] ) ) ; } } return ans ; } } class Main { public static void main ( String [ ] args ) throws Exception { int [ ] a = { 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 } ; int n = a . length ; System . out . println ( new GfG ( ) . minSwaps ( a , n ) ) ; } }
Minimum number of swaps required to sort an array | Java program to find minimum number of swaps required to sort an array ; Return the minimum number of swaps required to sort the array ; Hashmap which stores the indexes of the input array ; This is checking whether the current element is at the right place or not ; If not , swap this element with the index of the element which should come here ; Update the indexes in the hashmap accordingly ; Driver class ; Driver program to test the above function ; Output will be 5
import java . util . * ; import java . io . * ; class GfG { public int minSwaps ( int [ ] arr , int N ) { int ans = 0 ; int [ ] temp = Arrays . copyOfRange ( arr , 0 , N ) ; HashMap < Integer , Integer > h = new HashMap < Integer , Integer > ( ) ; Arrays . sort ( temp ) ; for ( int i = 0 ; i < N ; i ++ ) { h . put ( arr [ i ] , i ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != temp [ i ] ) { ans ++ ; int init = arr [ i ] ; swap ( arr , i , h . get ( temp [ i ] ) ) ; h . put ( init , h . get ( temp [ i ] ) ) ; h . put ( temp [ i ] , i ) ; } } return ans ; } public void swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } class Main { public static void main ( String [ ] args ) throws Exception { int [ ] a = { 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 } ; int n = a . length ; System . out . println ( new GfG ( ) . minSwaps ( a , n ) ) ; } }
Union and Intersection of two sorted arrays | Java program to find union of two sorted arrays ; Function prints union of arr1 [ ] and arr2 [ ] m is the number of elements in arr1 [ ] n is the number of elements in arr2 [ ] ; Print remaining elements of the larger array ; Driver program to test above function
class FindUnion { static int printUnion ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) System . out . print ( arr1 [ i ++ ] + " ▁ " ) ; else if ( arr2 [ j ] < arr1 [ i ] ) System . out . print ( arr2 [ j ++ ] + " ▁ " ) ; else { System . out . print ( arr2 [ j ++ ] + " ▁ " ) ; i ++ ; } } while ( i < m ) System . out . print ( arr1 [ i ++ ] + " ▁ " ) ; while ( j < n ) System . out . print ( arr2 [ j ++ ] + " ▁ " ) ; return 0 ; } public static void main ( String args [ ] ) { int arr1 [ ] = { 1 , 2 , 4 , 5 , 6 } ; int arr2 [ ] = { 2 , 3 , 5 , 7 } ; int m = arr1 . length ; int n = arr2 . length ; printUnion ( arr1 , arr2 , m , n ) ; } }
Check if N can be converted to the form K power K by the given operation | Java implementation to Check whether a given number N can be converted to the form K power K by the given operation ; Function to check if a number is converatable ; Check if n is of the form k ^ k ; Check if the subproblem has been solved before ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Store and return the answer to this subproblem ; Function to check the above method ; Initialise the dp table ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7
import java . util . * ; class GFG { static HashSet < Integer > kPowKform = new HashSet < > ( ) ; static int [ ] dp = new int [ 100005 ] ; static int func ( int n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . contains ( n ) ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; int answer = 0 ; int x = n ; while ( x > 0 ) { int d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) != 0 ) { answer = 1 ; break ; } } x /= 10 ; } return dp [ n ] = answer ; } static void canBeConverted ( int n ) { for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = - 1 ; if ( func ( n ) == 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int N = 13 ; for ( int i = 1 ; i <= 8 ; i ++ ) { int val = 1 ; for ( int j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . add ( val ) ; } canBeConverted ( N ) ; } }
Number of ways to arrange N numbers which are in a range from 1 to K under given constraints . | Java program to calculate number of ways to arrange N numbers under given constraints . ; Function to print Total number of ways ; If the First number and the last number is same . ; DP approach to find current state with the help of previous state . ; Driver Code ; Function call
import java . io . * ; import java . util . * ; class GFG { static void NumberOfWays ( int n , int k , int p , int q ) { int [ ] [ ] dp = new int [ n ] [ 2 ] ; if ( p == q ) { dp [ 0 ] [ 0 ] = 1 ; dp [ 0 ] [ 1 ] = 0 ; } else { dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] * ( k - 1 ) ) + ( dp [ i - 1 ] [ 1 ] * ( k - 2 ) ) ; } System . out . println ( dp [ n - 1 ] [ 0 ] ) ; } public static void main ( String args [ ] ) { int N = 5 ; int K = 3 ; int P = 2 ; int Q = 1 ; NumberOfWays ( N , K , P , Q ) ; } }
Union and Intersection of two sorted arrays | Java program to find union of two sorted arrays ( Handling Duplicates ) ; Taking max element present in either array ; Finding elements from 1 st array ( non duplicates only ) . Using another array for storing union elements of both arrays Assuming max element present in array is not more than 10 ^ 7 ; First element is always present in final answer ; Incrementing the First element ' s ▁ count ▁ ▁ in ▁ it ' s corresponding index in newtable ; Starting traversing the first array from 1 st index till last ; Checking whether current element is not equal to it 's previous element ; Finding only non common elements from 2 nd array ; By checking whether it 's already  present in newtable or not ; Driver Code
class FindUnion { static void UnionArray ( int arr1 [ ] , int arr2 [ ] ) { int m = arr1 [ arr1 . length - 1 ] ; int n = arr2 [ arr2 . length - 1 ] ; int ans = 0 ; if ( m > n ) { ans = m ; } else ans = n ; int newtable [ ] = new int [ ans + 1 ] ; System . out . print ( arr1 [ 0 ] + " ▁ " ) ; ++ newtable [ arr1 [ 0 ] ] ; for ( int i = 1 ; i < arr1 . length ; i ++ ) { if ( arr1 [ i ] != arr1 [ i - 1 ] ) { System . out . print ( arr1 [ i ] + " ▁ " ) ; ++ newtable [ arr1 [ i ] ] ; } } for ( int j = 0 ; j < arr2 . length ; j ++ ) { if ( newtable [ arr2 [ j ] ] == 0 ) { System . out . print ( arr2 [ j ] + " ▁ " ) ; ++ newtable [ arr2 [ j ] ] ; } } } public static void main ( String args [ ] ) { int arr1 [ ] = { 1 , 2 , 2 , 2 , 3 } ; int arr2 [ ] = { 2 , 3 , 4 , 5 } ; UnionArray ( arr1 , arr2 ) ; } }
Union and Intersection of two sorted arrays | Java program to find intersection of two sorted arrays ; Function prints Intersection of arr1 [ ] and arr2 [ ] m is the number of elements in arr1 [ ] n is the number of elements in arr2 [ ] ; Driver program to test above function ; Function calling
class FindIntersection { static void printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) i ++ ; else if ( arr2 [ j ] < arr1 [ i ] ) j ++ ; else { System . out . print ( arr2 [ j ++ ] + " ▁ " ) ; i ++ ; } } } public static void main ( String args [ ] ) { int arr1 [ ] = { 1 , 2 , 4 , 5 , 6 } ; int arr2 [ ] = { 2 , 3 , 5 , 7 } ; int m = arr1 . length ; int n = arr2 . length ; printIntersection ( arr1 , arr2 , m , n ) ; } }
Find the Nth Pure number | Java program to find the Nth pure number ; Function to check if it is a power of 2 or not ; if a number belongs to 4 series it should lie between 2 ^ blocks - 1 to 2 ^ blocks + 2 ^ ( blocks - 1 ) - 1 ; Method to find pure number ; Iterate from 1 to N ; Check if number is power of two ; Distance to previous block numbers ; Distance to previous block numbers ; Driver Code ; Create an object of the class ; Function call to find the Nth pure number
import java . io . * ; class PureNumbers { public boolean isPowerOfTwo ( int N ) { double number = Math . log ( N ) / Math . log ( 2 ) ; int checker = ( int ) number ; return number - checker == 0 ; } public boolean isSeriesFour ( int N , int digits ) { int upperBound = ( int ) ( Math . pow ( 2 , digits ) + Math . pow ( 2 , digits - 1 ) - 1 ) ; int lowerBound = ( int ) ( Math . pow ( 2 , digits ) - 1 ) ; return ( N >= lowerBound ) && ( N < upperBound ) ; } public String getPureNumber ( int N ) { String [ ] numbers = new String [ N + 1 ] ; numbers [ 0 ] = " " ; int blocks = 0 ; int displacement = 0 ; for ( int i = 1 ; i < N + 1 ; i ++ ) { if ( isPowerOfTwo ( i + 1 ) ) { blocks = blocks + 1 ; } if ( isSeriesFour ( i , blocks ) ) { displacement = ( int ) Math . pow ( 2 , blocks - 1 ) ; numbers [ i ] = "4" + numbers [ i - displacement ] + "4" ; } else { displacement = ( int ) Math . pow ( 2 , blocks ) ; numbers [ i ] = "5" + numbers [ i - displacement ] + "5" ; } } return numbers [ N ] ; } public static void main ( String [ ] args ) throws Exception { int N = 5 ; PureNumbers ob = new PureNumbers ( ) ; String pure = ob . getPureNumber ( N ) ; System . out . println ( pure ) ; } }
Count numbers less than N containing digits from the given set : Digit DP | Java implementation to find the count of numbers possible less than N , such that every digit is from the given set of digits ; Function to convert integer into the String ; Recursive function to find the count of numbers possible less than N , such that every digit is from the given set of digits ; Base case ; Condition when the subproblem is computed previously ; Condition when the number chosen till now is definietly smaller than the given number N ; Loop to traverse all the digits of the given set ; Loop to traverse all the digits from the given set ; Store the solution for current subproblem ; Function to count the numbers less then N from given set of digits ; Converting the number to String ; Initially no subproblem is solved till now ; Find the solution of all the number equal to the length of the given number N ; Loop to find the number less in in the length of the given number ; Driver Code ; Function Call
import java . util . * ; class GFG { static int [ ] [ ] dp = new int [ 15 ] [ 2 ] ; static String convertToString ( int num ) { return String . valueOf ( num ) ; } static int calculate ( int pos , int tight , int D [ ] , int sz , String num ) { if ( pos == num . length ( ) ) return 1 ; if ( dp [ pos ] [ tight ] != - 1 ) return dp [ pos ] [ tight ] ; int val = 0 ; if ( tight == 0 ) { for ( int i = 0 ; i < sz ; i ++ ) { if ( D [ i ] < ( num . charAt ( pos ) - '0' ) ) { val += calculate ( pos + 1 , 1 , D , sz , num ) ; } else if ( D [ i ] == num . charAt ( pos ) - '0' ) val += calculate ( pos + 1 , tight , D , sz , num ) ; } } else { for ( int i = 0 ; i < sz ; i ++ ) { val += calculate ( pos + 1 , tight , D , sz , num ) ; } } return dp [ pos ] [ tight ] = val ; } static int countNumbers ( int D [ ] , int N , int sz ) { String num = convertToString ( N ) ; int len = num . length ( ) ; for ( int i = 0 ; i < 15 ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) dp [ i ] [ j ] = - 1 ; int ans = calculate ( 0 , 0 , D , sz , num ) ; for ( int i = 1 ; i < len ; i ++ ) ans += calculate ( i , 1 , D , sz , num ) ; return ans ; } public static void main ( String [ ] args ) { int sz = 3 ; int D [ ] = { 1 , 4 , 9 } ; int N = 10 ; System . out . print ( countNumbers ( D , N , sz ) ) ; } }
Find Union and Intersection of two unsorted arrays | Java program for the above approach ; Defining map container mp ; Inserting array elements in mp ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void printUnion ( int [ ] a , int n , int [ ] b , int m ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . put ( a [ i ] , i ) ; } for ( int i = 0 ; i < m ; i ++ ) { mp . put ( b [ i ] , i ) ; } System . out . println ( " The ▁ union ▁ set ▁ of ▁ both ▁ arrays ▁ is ▁ : " ) ; for ( Map . Entry mapElement : mp . entrySet ( ) ) { System . out . print ( mapElement . getKey ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 5 , 6 , 2 , 3 , 5 } ; int b [ ] = { 2 , 4 , 5 , 6 , 8 , 9 , 4 , 6 , 5 } ; printUnion ( a , 7 , b , 9 ) ; } }
Maximum sum of elements divisible by K from the given array |  ; Function to return the maximum sum divisible by k from elements of v ; check if sum of elements excluding the current one is divisible by k ; check if sum of elements including the current one is divisible by k ; Store the maximum ; Driver code
class GFG { static int [ ] [ ] dp = new int [ 1001 ] [ 1001 ] ; static int find_max ( int i , int sum , int [ ] v , int k ) { if ( i == v . length ) return 0 ; if ( dp [ i ] [ sum ] != - 1 ) return dp [ i ] [ sum ] ; int ans = 0 ; if ( ( sum + find_max ( i + 1 , sum , v , k ) ) % k == 0 ) ans = find_max ( i + 1 , sum , v , k ) ; if ( ( sum + v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) % k == 0 ) ans = Math . max ( ans , v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) ; return dp [ i ] [ sum ] = ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 43 , 1 , 17 , 26 , 15 } ; int k = 16 ; for ( int i = 0 ; i < 1001 ; i ++ ) for ( int j = 0 ; j < 1001 ; j ++ ) dp [ i ] [ j ] = - 1 ; System . out . print ( find_max ( 0 , 0 , arr , k ) ) ; } }
Find Union and Intersection of two unsorted arrays | A Java program to print union and intersection of two unsorted arrays ; Prints union of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding union , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort the first array and print its elements ( these two steps can be swapped as order in output is not important ) ; Search every element of bigger array in smaller array and print the element if not found ; Prints intersection of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding intersection , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort smaller array arr1 [ 0. . m - 1 ] ; Search every element of bigger array in smaller array and print the element if found ; A recursive binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver code ; Function call
import java . util . Arrays ; class UnionAndIntersection { void printUnion ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { if ( m > n ) { int tempp [ ] = arr1 ; arr1 = arr2 ; arr2 = tempp ; int temp = m ; m = n ; n = temp ; } Arrays . sort ( arr1 ) ; for ( int i = 0 ; i < m ; i ++ ) System . out . print ( arr1 [ i ] + " ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) == - 1 ) System . out . print ( arr2 [ i ] + " ▁ " ) ; } } void printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { if ( m > n ) { int tempp [ ] = arr1 ; arr1 = arr2 ; arr2 = tempp ; int temp = m ; m = n ; n = temp ; } Arrays . sort ( arr1 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) != - 1 ) System . out . print ( arr2 [ i ] + " ▁ " ) ; } } int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } public static void main ( String [ ] args ) { UnionAndIntersection u_i = new UnionAndIntersection ( ) ; int arr1 [ ] = { 7 , 1 , 5 , 2 , 3 , 6 } ; int arr2 [ ] = { 3 , 8 , 6 , 20 , 7 } ; int m = arr1 . length ; int n = arr2 . length ; System . out . println ( " Union ▁ of ▁ two ▁ arrays ▁ is ▁ " ) ; u_i . printUnion ( arr1 , arr2 , m , n ) ; System . out . println ( " " ) ; System . out . println ( " Intersection ▁ of ▁ two ▁ arrays ▁ is ▁ " ) ; u_i . printIntersection ( arr1 , arr2 , m , n ) ; } }
Find the maximum sum leaf to root path in a Binary Tree | A Binary Tree node ; A wrapper class is used so that max_no can be updated among function calls . ; A utility function that prints all nodes on the path from root to target_leaf ; base case ; return true if this node is the target_leaf or target leaf is present in one of its descendants ; This function Sets the target_leaf_ref to refer the leaf node of the maximum path sum . Also , returns the max_sum using max_sum_ref ; Update current sum to hold sum of nodes on path from root to this node ; If this is a leaf node and path to this node has maximum sum so far , the n make this node target_leaf ; If this is not a leaf node , then recur down to find the target_leaf ; Returns the maximum sum and prints the nodes on max sum path ; base case ; find the target leaf and maximum sum ; print the path from root to the target leaf ; return maximum sum ; driver function to test the above functions
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class Maximum { int max_no = Integer . MIN_VALUE ; } class BinaryTree { Node root ; Maximum max = new Maximum ( ) ; Node target_leaf = null ; boolean printPath ( Node node , Node target_leaf ) { if ( node == null ) return false ; if ( node == target_leaf || printPath ( node . left , target_leaf ) || printPath ( node . right , target_leaf ) ) { System . out . print ( node . data + " ▁ " ) ; return true ; } return false ; } void getTargetLeaf ( Node node , Maximum max_sum_ref , int curr_sum ) { if ( node == null ) return ; curr_sum = curr_sum + node . data ; if ( node . left == null && node . right == null ) { if ( curr_sum > max_sum_ref . max_no ) { max_sum_ref . max_no = curr_sum ; target_leaf = node ; } } getTargetLeaf ( node . left , max_sum_ref , curr_sum ) ; getTargetLeaf ( node . right , max_sum_ref , curr_sum ) ; } int maxSumPath ( ) { if ( root == null ) return 0 ; getTargetLeaf ( root , max , 0 ) ; printPath ( root , target_leaf ) ; return max . max_no ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( - 2 ) ; tree . root . right = new Node ( 7 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( - 4 ) ; System . out . println ( " Following ▁ are ▁ the ▁ nodes ▁ " + " on ▁ maximum ▁ sum ▁ path " ) ; int sum = tree . maxSumPath ( ) ; System . out . println ( " " ) ; System . out . println ( " Sum ▁ of ▁ nodes ▁ is ▁ : ▁ " + sum ) ; } }
Find Union and Intersection of two unsorted arrays | Java code to find intersection when elements may not be distinct ; Function to find intersection ; when both are equal ; Driver Code ; sort ; Function call
import java . io . * ; import java . util . Arrays ; class GFG { static void intersection ( int a [ ] , int b [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( a [ i ] > b [ j ] ) { j ++ ; } else if ( b [ j ] > a [ i ] ) { i ++ ; } else { System . out . print ( a [ i ] + " ▁ " ) ; i ++ ; j ++ ; } } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 2 , 3 , 4 , 5 , 5 , 6 } ; int b [ ] = { 3 , 3 , 5 } ; int n = a . length ; int m = b . length ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; intersection ( a , b , n , m ) ; } }
Count of 1 's in any path in a Binary Tree | Java implementation of the above approach ; A binary tree node ; A utility function to allocate a new node ; This function updates overall count of 1 in ' res ' And returns count 1 s going through root . ; Base Case ; l and r store count of 1 s going through left and right child of root respectively ; maxCount represents the count of 1 s when the Node under consideration is the root of the maxCount path and no ancestors of the root are there in maxCount path ; if the value at node is 1 then its count will be considered including the leftCount and the rightCount ; Store the Maximum Result . ; if the value at node is 1 then its count will be considered including the maximum of leftCount or the rightCount ; Returns maximum count of 1 in any path in tree with given root ; Initialize result ; Compute and return result ; Driver program
class GFG { static class Node { int data ; Node left , right ; } ; static int res ; static Node newNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return ( newNode ) ; } static int countUntil ( Node root ) { if ( root == null ) return 0 ; int l = countUntil ( root . left ) ; int r = countUntil ( root . right ) ; int maxCount ; if ( root . data == 1 ) maxCount = l + r + 1 ; else maxCount = l + r ; res = Math . max ( res , maxCount ) ; if ( root . data == 1 ) return Math . max ( l , r ) + 1 ; else return Math . max ( l , r ) ; } static int findMaxCount ( Node root ) { res = Integer . MIN_VALUE ; countUntil ( root ) ; return res ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 0 ) ; root . right = newNode ( 1 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 1 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 0 ) ; System . out . print ( findMaxCount ( root ) ) ; } }
Find Union and Intersection of two unsorted arrays | Java program to find union and intersection using Hashing ; Prints union of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Prints intersection of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Driver code ; Function call
import java . util . HashSet ; class Test { static void printUnion ( int arr1 [ ] , int arr2 [ ] ) { HashSet < Integer > hs = new HashSet < > ( ) ; for ( int i = 0 ; i < arr1 . length ; i ++ ) hs . add ( arr1 [ i ] ) ; for ( int i = 0 ; i < arr2 . length ; i ++ ) hs . add ( arr2 [ i ] ) ; System . out . println ( hs ) ; } static void printIntersection ( int arr1 [ ] , int arr2 [ ] ) { HashSet < Integer > hs = new HashSet < > ( ) ; HashSet < Integer > hs1 = new HashSet < > ( ) ; for ( int i = 0 ; i < arr1 . length ; i ++ ) hs . add ( arr1 [ i ] ) ; for ( int i = 0 ; i < arr2 . length ; i ++ ) if ( hs . contains ( arr2 [ i ] ) ) System . out . print ( arr2 [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 7 , 1 , 5 , 2 , 3 , 6 } ; int arr2 [ ] = { 3 , 8 , 6 , 20 , 7 } ; System . out . println ( " Union ▁ of ▁ two ▁ arrays ▁ is ▁ : ▁ " ) ; printUnion ( arr1 , arr2 ) ; System . out . println ( " Intersection ▁ of ▁ two ▁ arrays ▁ is ▁ : ▁ " ) ; printIntersection ( arr1 , arr2 ) ; } }