codeparrot/xlcost-text-to-code huggingface dataset:
Abstraction of Binary Search | ''Function to find X such that it is less than the target value and function is f(x) = x ^ 2 ; '' Initialise start and end ; '' Loop till start <= end ; '' Find the mid ; '' Check for the left half ; Store the result ; Reinitialize the start point ; '' Check for the right half ; '' Print maximum value of x  such that x ^ 2 is less than the  targetValue ; ''Driver Code ; '' Given targetValue; ; '' Function Call
def findX ( targetValue ) : NEW_LINE INDENT start = 0 ; NEW_LINE end = targetValue ; NEW_LINE mid = 0 ; NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = start + ( end - start ) // 2 ; NEW_LINE if ( mid * mid <= targetValue ) : NEW_LINE INDENT result = mid NEW_LINE start = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 ; NEW_LINE DEDENT DEDENT print ( result ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT targetValue = 81 ; NEW_LINE findX ( targetValue ) ; NEW_LINE DEDENT
Two elements whose sum is closest to zero | Python3 implementation using STL ; Modified to sort by abolute values ; Absolute value shows how close it is to zero ; If found an even close value update min and store the index ; Driver code
import sys NEW_LINE def findMinSum ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( not abs ( arr [ i - 1 ] ) < abs ( arr [ i ] ) ) : NEW_LINE INDENT arr [ i - 1 ] , arr [ i ] = arr [ i ] , arr [ i - 1 ] NEW_LINE DEDENT DEDENT Min = sys . maxsize NEW_LINE x = 0 NEW_LINE y = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( abs ( arr [ i - 1 ] + arr [ i ] ) <= Min ) : NEW_LINE INDENT Min = abs ( arr [ i - 1 ] + arr [ i ] ) NEW_LINE x = i - 1 NEW_LINE y = i NEW_LINE DEDENT DEDENT print ( " The ▁ two ▁ elements ▁ whose ▁ sum ▁ is ▁ minimum ▁ are " , arr [ x ] , " and " , arr [ y ] ) NEW_LINE DEDENT arr = [ 1 , 60 , - 10 , 70 , - 80 , 85 ] NEW_LINE n = len ( arr ) NEW_LINE findMinSum ( arr , n ) NEW_LINE
Convert undirected connected graph to strongly connected directed graph | To store the assigned Edges ; Flag variable to check Bridges ; Function to implement DFS Traversal ; Mark the current node as visited ; Update the order of node v ; Update the bridge_detect for node v ; Traverse the adjacency list of Node v ; Ignores if same edge is traversed ; Ignores the edge u -- > v as v -- > u is already processed ; Finds a back Edges , cycle present ; Update the bridge_detect [ v ] ; Else DFS traversal for current node in the adjacency list ; Update the bridge_detect [ v ] ; Store the current directed Edge ; Condition for Bridges ; Return flag ; Function to print the direction of edges to make graph SCCs ; Arrays to store the visited , bridge_detect and order of Nodes ; DFS Traversal from vertex 1 ; If flag is zero , then Bridge is present in the graph ; Else print the direction of Edges assigned ; Function to create graph ; Traverse the Edges ; Push the edges in an adjacency list ; Driver code ; N vertices and M Edges ; To create Adjacency List ; Create an undirected graph ; Function Call
ans = [ ] NEW_LINE flag = 1 ; NEW_LINE def dfs ( adj , order , bridge_detect , mark , v , l ) : NEW_LINE INDENT global flag NEW_LINE mark [ v ] = 1 ; NEW_LINE order [ v ] = order [ l ] + 1 ; NEW_LINE bridge_detect [ v ] = order [ v ] ; NEW_LINE for i in range ( len ( adj [ v ] ) ) : NEW_LINE INDENT u = adj [ v ] [ i ] ; NEW_LINE if ( u == l ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT if ( order [ v ] < order [ u ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT if ( mark [ u ] ) : NEW_LINE INDENT bridge_detect [ v ] = min ( order [ u ] , bridge_detect [ v ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT dfs ( adj , order , bridge_detect , mark , u , v ) ; NEW_LINE DEDENT bridge_detect [ v ] = min ( bridge_detect [ u ] , bridge_detect [ v ] ) ; NEW_LINE ans . append ( [ v , u ] ) ; NEW_LINE DEDENT if ( bridge_detect [ v ] == order [ v ] and l != 0 ) : NEW_LINE INDENT flag = 0 ; NEW_LINE DEDENT return flag ; NEW_LINE DEDENT def convert ( adj , n ) : NEW_LINE INDENT order = [ 0 for i in range ( n ) ] NEW_LINE bridge_detect = [ 0 for i in range ( n ) ] NEW_LINE mark = [ False for i in range ( n ) ] NEW_LINE flag = dfs ( adj , order , bridge_detect , mark , 1 , 0 ) ; NEW_LINE if ( flag == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT for it in ans : NEW_LINE INDENT print ( " { } ▁ - > ▁ { } " . format ( it [ 0 ] , it [ 1 ] ) ) NEW_LINE DEDENT DEDENT DEDENT def createGraph ( Edges , adj , M ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT u = Edges [ i ] [ 0 ] ; NEW_LINE v = Edges [ i ] [ 1 ] ; NEW_LINE adj [ u ] . append ( v ) ; NEW_LINE adj [ v ] . append ( u ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE M = 6 ; NEW_LINE Edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 1 , 2 ] , [ 1 , 4 ] , [ 2 , 3 ] , [ 3 , 4 ] ] ; NEW_LINE adj = [ [ ] for i in range ( N ) ] NEW_LINE createGraph ( Edges , adj , M ) ; NEW_LINE convert ( adj , N ) ; NEW_LINE DEDENT
Shortest Un | Bool function for checking an array elements are in increasing ; Bool function for checking an array elements are in decreasing ; increasing and decreasing are two functions . if function return True value then print 0 otherwise 3. ; Driver code
def increasing ( a , n ) : NEW_LINE INDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( a [ i ] >= a [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def decreasing ( a , n ) : NEW_LINE INDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( a [ i ] < a [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def shortestUnsorted ( a , n ) : NEW_LINE INDENT if ( increasing ( a , n ) == True or decreasing ( a , n ) == True ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return 3 NEW_LINE DEDENT DEDENT ar = [ 7 , 9 , 10 , 8 , 11 ] NEW_LINE n = len ( ar ) NEW_LINE print ( shortestUnsorted ( ar , n ) ) NEW_LINE
Find the maximum path sum between two leaves of a binary tree | Python program to find maximumpath sum between two leaves of a binary tree ; A binary tree node ; Utility function to find maximum sum between anytwo leaves . This function calculates two values : 1 ) Maximum path sum between two leaves which are stored in res2 ) The maximum root to leaf path sum which is returned If one side of root is empty , then it returns INT_MIN ; Base Case ; Find maximumsum in left and righ subtree . Also find maximum root to leaf sums in left and righ subtrees ans store them in ls and rs ; If both left and right children exist ; update result if needed ; Return maximum possible value for root being on one side ; If any of the two children is empty , return root sum for root being on one side ; The main function which returns sum of the maximum sum path betwee ntwo leaves . THis function mainly uses maxPathSumUtil ( ) ; Driver program to test above function
INT_MIN = - 2 ** 32 NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def maxPathSumUtil ( root , res ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT ls = maxPathSumUtil ( root . left , res ) NEW_LINE rs = maxPathSumUtil ( root . right , res ) NEW_LINE if root . left is not None and root . right is not None : NEW_LINE INDENT res [ 0 ] = max ( res [ 0 ] , ls + rs + root . data ) NEW_LINE return max ( ls , rs ) + root . data NEW_LINE DEDENT if root . left is None : NEW_LINE INDENT return rs + root . data NEW_LINE DEDENT else : NEW_LINE INDENT return ls + root . data NEW_LINE DEDENT DEDENT def maxPathSum ( root ) : NEW_LINE INDENT res = [ INT_MIN ] NEW_LINE maxPathSumUtil ( root , res ) NEW_LINE return res [ 0 ] NEW_LINE DEDENT root = Node ( - 15 ) NEW_LINE root . left = Node ( 5 ) NEW_LINE root . right = Node ( 6 ) NEW_LINE root . left . left = Node ( - 8 ) NEW_LINE root . left . right = Node ( 1 ) NEW_LINE root . left . left . left = Node ( 2 ) NEW_LINE root . left . left . right = Node ( 6 ) NEW_LINE root . right . left = Node ( 3 ) NEW_LINE root . right . right = Node ( 9 ) NEW_LINE root . right . right . right = Node ( 0 ) NEW_LINE root . right . right . right . left = Node ( 4 ) NEW_LINE root . right . right . right . right = Node ( - 1 ) NEW_LINE root . right . right . right . right . left = Node ( 10 ) NEW_LINE print " Max ▁ pathSum ▁ of ▁ the ▁ given ▁ binary ▁ tree ▁ is " , maxPathSum ( root ) NEW_LINE
Minimum number of swaps required to sort an array | Function returns the minimum number of swaps required to sort the array ; Create two arrays and use as pairs where first array is element and second array is position of first element ; Sort the array by array element values to get right position of every element as the elements of second array . ; To keep track of visited elements . Initialize all elements as not visited or false . ; Initialize result ; Traverse array elements ; alreadt swapped or alreadt present at correct position ; find number of nodes in this cycle and add it to ans ; move to next node ; update answer by adding current cycle ; return answer ; Driver Code
def minSwaps ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE arrpos = [ * enumerate ( arr ) ] NEW_LINE arrpos . sort ( key = lambda it : it [ 1 ] ) NEW_LINE vis = { k : False for k in range ( n ) } NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if vis [ i ] or arrpos [ i ] [ 0 ] == i : NEW_LINE INDENT continue NEW_LINE DEDENT cycle_size = 0 NEW_LINE j = i NEW_LINE while not vis [ j ] : NEW_LINE INDENT vis [ j ] = True NEW_LINE j = arrpos [ j ] [ 0 ] NEW_LINE cycle_size += 1 NEW_LINE DEDENT if cycle_size > 0 : NEW_LINE INDENT ans += ( cycle_size - 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 5 , 4 , 3 , 2 ] NEW_LINE print ( minSwaps ( arr ) ) NEW_LINE
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 code ; Output will be 5
def swap ( arr , i , j ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE arr [ i ] = arr [ j ] NEW_LINE arr [ j ] = temp NEW_LINE DEDENT def indexOf ( arr , ele ) : NEW_LINE INDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] == ele ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def minSwaps ( arr , N ) : NEW_LINE INDENT ans = 0 NEW_LINE temp = arr . copy ( ) NEW_LINE temp . sort ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] != temp [ i ] ) : NEW_LINE INDENT ans += 1 NEW_LINE swap ( arr , i , indexOf ( arr , temp [ i ] ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 ] NEW_LINE n = len ( a ) NEW_LINE print ( minSwaps ( a , n ) ) NEW_LINE DEDENT
Minimum number of swaps required to sort an array | Return the minimum number of swaps required to sort the array ; Dictionary 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 code ; Output will be 5
def minSwap ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE temp = arr . copy ( ) NEW_LINE h = { } NEW_LINE temp . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT h [ arr [ i ] ] = i NEW_LINE DEDENT init = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != temp [ i ] ) : NEW_LINE INDENT ans += 1 NEW_LINE init = arr [ i ] NEW_LINE arr [ i ] , arr [ h [ temp [ i ] ] ] = arr [ h [ temp [ i ] ] ] , arr [ i ] NEW_LINE h [ init ] = h [ temp [ i ] ] NEW_LINE h [ temp [ i ] ] = i NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT a = [ 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 ] NEW_LINE n = len ( a ) NEW_LINE print ( minSwap ( a , n ) ) NEW_LINE
Union and Intersection 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
def printUnion ( arr1 , arr2 , m , n ) : NEW_LINE INDENT i , j = 0 , 0 NEW_LINE while i < m and j < n : NEW_LINE INDENT if arr1 [ i ] < arr2 [ j ] : NEW_LINE INDENT print ( arr1 [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT elif arr2 [ j ] < arr1 [ i ] : NEW_LINE INDENT print ( arr2 [ j ] ) NEW_LINE j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( arr2 [ j ] ) NEW_LINE j += 1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT while i < m : NEW_LINE INDENT print ( arr1 [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT while j < n : NEW_LINE INDENT print ( arr2 [ j ] ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT arr1 = [ 1 , 2 , 4 , 5 , 6 ] NEW_LINE arr2 = [ 2 , 3 , 5 , 7 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE printUnion ( arr1 , arr2 , m , n ) NEW_LINE
Union and Intersection of two sorted arrays | Python3 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
def UnionArray ( arr1 , arr2 ) : NEW_LINE INDENT m = arr1 [ - 1 ] NEW_LINE n = arr2 [ - 1 ] NEW_LINE ans = 0 NEW_LINE if m > n : NEW_LINE INDENT ans = m NEW_LINE DEDENT else : NEW_LINE INDENT ans = n NEW_LINE DEDENT newtable = [ 0 ] * ( ans + 1 ) NEW_LINE print ( arr1 [ 0 ] , end = " ▁ " ) NEW_LINE newtable [ arr1 [ 0 ] ] += 1 NEW_LINE for i in range ( 1 , len ( arr1 ) ) : NEW_LINE INDENT if arr1 [ i ] != arr1 [ i - 1 ] : NEW_LINE INDENT print ( arr1 [ i ] , end = " ▁ " ) NEW_LINE newtable [ arr1 [ i ] ] += 1 NEW_LINE DEDENT DEDENT for j in range ( 0 , len ( arr2 ) ) : NEW_LINE INDENT if newtable [ arr2 [ j ] ] == 0 : NEW_LINE INDENT print ( arr2 [ j ] , end = " ▁ " ) NEW_LINE newtable [ arr2 [ j ] ] += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 1 , 2 , 2 , 2 , 3 ] NEW_LINE arr2 = [ 2 , 3 , 4 , 5 ] NEW_LINE UnionArray ( arr1 , arr2 ) NEW_LINE DEDENT
Union and 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
def printIntersection ( arr1 , arr2 , m , n ) : NEW_LINE INDENT i , j = 0 , 0 NEW_LINE while i < m and j < n : NEW_LINE INDENT if arr1 [ i ] < arr2 [ j ] : NEW_LINE INDENT i += 1 NEW_LINE DEDENT elif arr2 [ j ] < arr1 [ i ] : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( arr2 [ j ] ) NEW_LINE j += 1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT DEDENT arr1 = [ 1 , 2 , 4 , 5 , 6 ] NEW_LINE arr2 = [ 2 , 3 , 5 , 7 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE printIntersection ( arr1 , arr2 , m , n ) NEW_LINE
Count numbers less than N containing digits from the given set : Digit DP | Python3 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 ; 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 numpy as np ; NEW_LINE dp = np . ones ( ( 15 , 2 ) ) * - 1 ; NEW_LINE def convertToString ( num ) : NEW_LINE INDENT return str ( num ) ; NEW_LINE DEDENT def calculate ( pos , tight , D , sz , num ) : NEW_LINE INDENT if ( pos == len ( num ) ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( dp [ pos ] [ tight ] != - 1 ) : NEW_LINE INDENT return dp [ pos ] [ tight ] ; NEW_LINE DEDENT val = 0 ; NEW_LINE if ( tight == 0 ) : NEW_LINE INDENT for i in range ( sz ) : NEW_LINE INDENT if ( D [ i ] < ( ord ( num [ pos ] ) - ord ( '0' ) ) ) : NEW_LINE INDENT val += calculate ( pos + 1 , 1 , D , sz , num ) ; NEW_LINE DEDENT elif ( D [ i ] == ord ( num [ pos ] ) - ord ( '0' ) ) : NEW_LINE INDENT val += calculate ( pos + 1 , tight , D , sz , num ) ; NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( sz ) : NEW_LINE INDENT val += calculate ( pos + 1 , tight , D , sz , num ) ; NEW_LINE DEDENT DEDENT dp [ pos ] [ tight ] = val ; NEW_LINE return dp [ pos ] [ tight ] ; NEW_LINE DEDENT def countNumbers ( D , N , sz ) : NEW_LINE INDENT num = convertToString ( N ) ; NEW_LINE length = len ( num ) ; NEW_LINE ans = calculate ( 0 , 0 , D , sz , num ) ; NEW_LINE for i in range ( 1 , length ) : NEW_LINE INDENT ans += calculate ( i , 1 , D , sz , num ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT sz = 3 ; NEW_LINE D = [ 1 , 4 , 9 ] ; NEW_LINE N = 10 ; NEW_LINE print ( countNumbers ( D , N , sz ) ) ; NEW_LINE DEDENT
Maximum sum of elements divisible by K from the given array | Python3 implementation ; 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
dp = [ [ - 1 for i in range ( 1001 ) ] for j in range ( 1001 ) ] NEW_LINE def find_max ( i , sum , v , k ) : NEW_LINE INDENT if ( i == len ( v ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ sum ] NEW_LINE DEDENT ans = 0 NEW_LINE if ( ( sum + find_max ( i + 1 , sum , v , k ) ) % k == 0 ) : NEW_LINE INDENT ans = find_max ( i + 1 , sum , v , k ) NEW_LINE DEDENT if ( ( sum + v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) % k == 0 ) : NEW_LINE INDENT ans = max ( ans , v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) NEW_LINE DEDENT dp [ i ] [ sum ] = ans NEW_LINE return dp [ i ] [ sum ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 43 , 1 , 17 , 26 , 15 ] NEW_LINE k = 16 NEW_LINE print ( find_max ( 0 , 0 , arr , k ) ) NEW_LINE DEDENT
Find 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 presen 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
def printUnion ( arr1 , arr2 , m , n ) : NEW_LINE INDENT if ( m > n ) : NEW_LINE INDENT tempp = arr1 NEW_LINE arr1 = arr2 NEW_LINE arr2 = tempp NEW_LINE temp = m NEW_LINE m = n NEW_LINE n = temp NEW_LINE DEDENT arr1 . sort ( ) NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT print ( arr1 [ i ] , end = " ▁ " ) NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) == - 1 ) : NEW_LINE INDENT print ( arr2 [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT def printIntersection ( arr1 , arr2 , m , n ) : NEW_LINE INDENT if ( m > n ) : NEW_LINE INDENT tempp = arr1 NEW_LINE arr1 = arr2 NEW_LINE arr2 = tempp NEW_LINE temp = m NEW_LINE m = n NEW_LINE n = temp NEW_LINE DEDENT arr1 . sort ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) != - 1 ) : NEW_LINE INDENT print ( arr2 [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT def binarySearch ( arr , l , r , x ) : NEW_LINE INDENT if ( r >= l ) : NEW_LINE INDENT mid = int ( l + ( r - l ) / 2 ) NEW_LINE if ( arr [ mid ] == x ) : NEW_LINE INDENT return mid NEW_LINE DEDENT if ( arr [ mid ] > x ) : NEW_LINE INDENT return binarySearch ( arr , l , mid - 1 , x ) NEW_LINE DEDENT return binarySearch ( arr , mid + 1 , r , x ) NEW_LINE DEDENT return - 1 NEW_LINE DEDENT arr1 = [ 7 , 1 , 5 , 2 , 3 , 6 ] NEW_LINE arr2 = [ 3 , 8 , 6 , 20 , 7 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE print ( " Union ▁ of ▁ two ▁ arrays ▁ is ▁ " ) NEW_LINE printUnion ( arr1 , arr2 , m , n ) NEW_LINE print ( " Intersection of two arrays is   " ) NEW_LINE printIntersection ( arr1 , arr2 , m , n ) NEW_LINE