codeparrot/xlcost-text-to-code huggingface dataset:
Length of the longest subsequence such that XOR of adjacent elements is equal to K | ''Function to find maximum length of subsequence having XOR of adjacent elements equal to K ; '' Store maximum length of subsequence ; '' Stores the dp-states ; '' Base case ; '' Iterate over the range [1, N-1] ; '' Iterate over the range [0, i - 1] ; '' If arr[i]^arr[j] == K ; '' Update the dp[i] ; '' Update the maximum subsequence length ; '' If length of longest subsequence  is less than 2 then return 0 ; ''Driver Code ; '' Input ; '' Prthe length of longest subsequence
def xorSubsequence ( a , n , k ) : NEW_LINE INDENT ans = 0 ; NEW_LINE dp = [ 0 ] * n ; NEW_LINE dp [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( ( a [ i ] ^ a [ j ] ) == k ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , dp [ j ] + 1 ) ; NEW_LINE DEDENT DEDENT ans = max ( ans , dp [ i ] ) ; NEW_LINE dp [ i ] = max ( 1 , dp [ i ] ) ; NEW_LINE DEDENT return ans if ans >= 2 else 0 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 4 , 3 , 5 ] ; NEW_LINE K = 1 ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( xorSubsequence ( arr , N , K ) ) ; NEW_LINE DEDENT
Check if a Binary Tree contains duplicate subtrees of size 2 or more | Python3 program to find if there is a duplicate sub - tree of size 2 or more Separator node ; Structure for a binary tree node ; This function returns empty if tree contains a duplicate subtree of size 2 or more . ; If current node is None , return marker ; If left subtree has a duplicate subtree . ; Do same for right subtree ; Serialize current subtree ; If current subtree already exists in hash table . [ Note that size of a serialized tree with single node is 3 as it has two marker nodes . ; Driver code
MARKER = ' $ ' NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . key = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT subtrees = { } NEW_LINE def dupSubUtil ( root ) : NEW_LINE INDENT global subtrees NEW_LINE s = " " NEW_LINE if ( root == None ) : NEW_LINE INDENT return s + MARKER NEW_LINE DEDENT lStr = dupSubUtil ( root . left ) NEW_LINE if ( s in lStr ) : NEW_LINE return s NEW_LINE rStr = dupSubUtil ( root . right ) NEW_LINE if ( s in rStr ) : NEW_LINE return s NEW_LINE s = s + root . key + lStr + rStr NEW_LINE if ( len ( s ) > 3 and s in subtrees ) : NEW_LINE return " " NEW_LINE subtrees [ s ] = 1 NEW_LINE return s NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( ' A ' ) NEW_LINE root . left = Node ( ' B ' ) NEW_LINE root . right = Node ( ' C ' ) NEW_LINE root . left . left = Node ( ' D ' ) NEW_LINE root . left . right = Node ( ' E ' ) NEW_LINE root . right . right = Node ( ' B ' ) NEW_LINE root . right . right . right = Node ( ' E ' ) NEW_LINE root . right . right . left = Node ( ' D ' ) NEW_LINE str = dupSubUtil ( root ) NEW_LINE if " " in str : NEW_LINE INDENT print ( " ▁ Yes ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ No ▁ " ) NEW_LINE DEDENT DEDENT
Count pairs of similar rectangles possible from a given array | ''Python 3 Program for the hashmap Approach ; ''Get the count of all pairs of similar rectangles ; '' Initialize the result value and  map to store the ratio to the rectangles ; '' Calculate the rectangular ratio and save them ; '' Calculate pairs of similar rectangles from its common ratio ; ''Driver code
from collections import defaultdict NEW_LINE def getCount ( rows , columns , sides ) : NEW_LINE INDENT ans = 0 NEW_LINE umap = defaultdict ( int ) NEW_LINE for i in range ( rows ) : NEW_LINE INDENT ratio = sides [ i ] [ 0 ] / sides [ i ] [ 1 ] NEW_LINE umap [ ratio ] += 1 NEW_LINE DEDENT for x in umap : NEW_LINE INDENT value = umap [ x ] NEW_LINE if ( value > 1 ) : NEW_LINE INDENT ans += ( value * ( value - 1 ) ) / 2 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT sides = [ [ 4 , 8 ] , [ 10 , 20 ] , [ 15 , 30 ] , [ 3 , 6 ] ] NEW_LINE rows = 4 NEW_LINE columns = 2 NEW_LINE print ( int ( getCount ( rows , columns , sides ) ) ) NEW_LINE DEDENT
Non | Python3 program to find first non - repeating element . ; Driver code
def firstNonRepeating ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT j = 0 NEW_LINE while ( j < n ) : NEW_LINE INDENT if ( i != j and arr [ i ] == arr [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( j == n ) : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 9 , 4 , 9 , 6 , 7 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( firstNonRepeating ( arr , n ) ) NEW_LINE
Non | Efficient Python3 program to find first non - repeating element . ; Insert all array elements in hash table ; Traverse array again and return first element with count 1. ; Driver Code
from collections import defaultdict NEW_LINE def firstNonRepeating ( arr , n ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if mp [ arr [ i ] ] == 1 : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 9 , 4 , 9 , 6 , 7 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( firstNonRepeating ( arr , n ) ) NEW_LINE
Non | Efficient Python program to print all non - repeating elements . ; Insert all array elements in hash table ; Traverse through map only and ; Driver code
def firstNonRepeating ( arr , n ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] not in mp : NEW_LINE INDENT mp [ arr [ i ] ] = 0 NEW_LINE DEDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT for x in mp : NEW_LINE INDENT if ( mp [ x ] == 1 ) : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 9 , 4 , 9 , 6 , 7 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE firstNonRepeating ( arr , n ) NEW_LINE
Minimum substring removals required to make all remaining characters of a string same | ''Python3 program to implement the above approach ; ''Function to count minimum operations required to make all characters equal by repeatedly removing substring ; '' Remove consecutive duplicate  characters from str ; '' Stores length of the string ; '' Stores frequency of each distinct  characters of the string str ; '' Iterate over all the characters  of the string str ; '' Update frequency of str[i] ; '' Decrementing the frequency  of the string str[0] ; '' Decrementing the frequency  of the ord(string ord(str[N - 1] ; '' Stores the required count ; '' Iterate over all characters  of the string str ; '' Update ans ; ''Driver Code ; '' Given string
import re , sys NEW_LINE def minOperationNeeded ( s ) : NEW_LINE INDENT d = { } NEW_LINE str = re . sub ( r " ( . ) \1 ▁ + ▁ " , ' ' , s ) NEW_LINE N = len ( str ) NEW_LINE res = [ 0 for i in range ( 256 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT res [ ord ( str [ i ] ) ] += 1 NEW_LINE DEDENT res [ ord ( str [ 0 ] ) ] -= 1 NEW_LINE res [ ord ( str [ N - 1 ] ) ] -= 1 NEW_LINE ans = sys . maxsize NEW_LINE for i in range ( N ) : NEW_LINE INDENT ans = min ( ans , res [ ord ( str [ i ] ) ] ) NEW_LINE DEDENT print ( ( ans + 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " ABCDABCDABCDA " NEW_LINE minOperationNeeded ( str ) NEW_LINE DEDENT
Print all root to leaf paths of an N | ''Structure of an N ary tree node ; ''Function to print the root to leaf path of the given N-ary Tree ; '' Print elements in the vector ; ''Utility function to print all root to leaf paths of an Nary Tree ; '' If root is null ; ' ' ▁ Insert ▁ current ▁ node ' s data into the vector ; '' If current node is a leaf node ; '' Print the path ; '' Pop the leaf node  and return ; '' Recur for all children of  the current node ; '' Recursive Function Call ; ''Function to print root to leaf path ; '' If root is null, return ; '' Utility function call ; ''Driver Code ; '' Given N-Ary tree ; '' Function Call
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . child = [ ] NEW_LINE DEDENT DEDENT def printPath ( vec ) : NEW_LINE INDENT for ele in vec : NEW_LINE INDENT print ( ele , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def printAllRootToLeafPaths ( root ) : NEW_LINE INDENT global vec NEW_LINE if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT vec . append ( root . data ) NEW_LINE if ( len ( root . child ) == 0 ) : NEW_LINE INDENT printPath ( vec ) NEW_LINE vec . pop ( ) NEW_LINE return NEW_LINE DEDENT for i in range ( len ( root . child ) ) : NEW_LINE INDENT printAllRootToLeafPaths ( root . child [ i ] ) NEW_LINE DEDENT vec . pop ( ) NEW_LINE DEDENT def printRootToLeafPaths ( root ) : NEW_LINE INDENT global vec NEW_LINE if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT printAllRootToLeafPaths ( root ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT vec = [ ] NEW_LINE root = Node ( 1 ) NEW_LINE root . child . append ( Node ( 2 ) ) NEW_LINE root . child . append ( Node ( 3 ) ) NEW_LINE root . child [ 0 ] . child . append ( Node ( 4 ) ) NEW_LINE root . child [ 1 ] . child . append ( Node ( 5 ) ) NEW_LINE root . child [ 1 ] . child . append ( Node ( 6 ) ) NEW_LINE root . child [ 1 ] . child [ 1 ] . child . append ( Node ( 7 ) ) NEW_LINE root . child [ 1 ] . child [ 1 ] . child . append ( Node ( 8 ) ) NEW_LINE printRootToLeafPaths ( root ) NEW_LINE DEDENT
Pairs of Positive Negative values in an array | Print pair with negative and positive value ; For each element of array . ; Try to find the negative value of arr [ i ] from i + 1 to n ; If absolute values are equal print pair . ; If size of vector is 0 , therefore there is no element with positive negative value , print "0" ; Sort the vector ; Print the pair with negative positive value . ; Driver Code
def printPairs ( arr , n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( abs ( arr [ i ] ) == abs ( arr [ j ] ) ) : NEW_LINE INDENT v . append ( abs ( arr [ i ] ) ) NEW_LINE DEDENT DEDENT DEDENT if ( len ( v ) == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT v . sort ( ) NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT print ( - v [ i ] , " " , v [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 ] NEW_LINE n = len ( arr ) NEW_LINE printPairs ( arr , n ) NEW_LINE DEDENT
Check if an array can be divided into pairs whose sum is divisible by k | Python3 program to check if arr [ 0. . n - 1 ] can be divided in pairs such that every pair is divisible by k . ; Returns true if arr [ 0. . n - 1 ] can be divided into pairs with sum divisible by k . ; An odd length array cannot be divided into pairs ; Create a frequency array to count occurrences of all remainders when divided by k . ; Count occurrences of all remainders ; Traverse input array and use freq [ ] to decide if given array can be divided in pairs ; Remainder of current element ; If remainder with current element divides k into two halves . ; Then there must be even occurrences of such remainder ; If remainder is 0 , then there must be two elements with 0 remainde ; Then there must be even occurrences of such remainder ; Else number of occurrences of remainder must be equal to number of occurrences of k - remainder ; Driver code ; Function call
from collections import defaultdict NEW_LINE def canPairs ( arr , n , k ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT freq = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT freq [ ( ( arr [ i ] % k ) + k ) % k ] += 1 NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT rem = ( ( arr [ i ] % k ) + k ) % k NEW_LINE if ( 2 * rem == k ) : NEW_LINE INDENT if ( freq [ rem ] % 2 != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT elif ( rem == 0 ) : NEW_LINE INDENT if ( freq [ rem ] & 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT elif ( freq [ rem ] != freq [ k - rem ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT arr = [ 92 , 75 , 65 , 48 , 45 , 35 ] NEW_LINE k = 10 NEW_LINE n = len ( arr ) NEW_LINE if ( canPairs ( arr , n , k ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT
Subarray with no pair sum divisible by K | function to find the subarray with no pair sum divisible by k ; hash table to store the remainders obtained on dividing by K ; s : starting index of the current subarray , e : ending index of the current subarray , maxs : starting index of the maximum size subarray so far , maxe : ending index of the maximum size subarray so far ; insert the first element in the set ; Removing starting elements of current subarray while there is an element in set which makes a pair with mod [ i ] such that the pair sum is divisible . ; include the current element in the current subarray the ending index of the current subarray increments by one ; compare the size of the current subarray with the maximum size so far ; Driver Code
def subarrayDivisibleByK ( arr , n , k ) : NEW_LINE INDENT mp = [ 0 ] * 1000 NEW_LINE s = 0 ; e = 0 ; maxs = 0 ; maxe = 0 ; NEW_LINE mp [ arr [ 0 ] % k ] = mp [ arr [ 0 ] % k ] + 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT mod = arr [ i ] % k NEW_LINE while ( mp [ k - mod ] != 0 or ( mod == 0 and mp [ mod ] != 0 ) ) : NEW_LINE INDENT mp [ arr [ s ] % k ] = mp [ arr [ s ] % k ] - 1 NEW_LINE s = s + 1 NEW_LINE DEDENT mp [ mod ] = mp [ mod ] + 1 NEW_LINE e = e + 1 NEW_LINE if ( ( e - s ) > ( maxe - maxs ) ) : NEW_LINE INDENT maxe = e NEW_LINE maxs = s NEW_LINE DEDENT DEDENT print ( " The ▁ maximum ▁ size ▁ is ▁ { } ▁ and ▁ the ▁ " . format ( ( maxe - maxs + 1 ) ) ) for i in range ( maxs , maxe + 1 ) : NEW_LINE INDENT print ( " { } ▁ " . format ( arr [ i ] ) , end = " " ) NEW_LINE DEDENT DEDENT k = 3 NEW_LINE arr = [ 5 , 10 , 15 , 20 , 25 ] NEW_LINE n = len ( arr ) NEW_LINE subarrayDivisibleByK ( arr , n , k ) NEW_LINE
Find three element from different three arrays such that a + b + c = sum | Function to check if there is an element from each array such that sum of the three elements is equal to given sum . ; Driver Code
def findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) : NEW_LINE INDENT for i in range ( 0 , n1 ) : NEW_LINE INDENT for j in range ( 0 , n2 ) : NEW_LINE INDENT for k in range ( 0 , n3 ) : NEW_LINE INDENT if ( a1 [ i ] + a2 [ j ] + a3 [ k ] == sum ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT DEDENT return False NEW_LINE DEDENT a1 = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE a2 = [ 2 , 3 , 6 , 1 , 2 ] NEW_LINE a3 = [ 3 , 2 , 4 , 5 , 6 ] NEW_LINE sum = 9 NEW_LINE n1 = len ( a1 ) NEW_LINE n2 = len ( a2 ) NEW_LINE n3 = len ( a3 ) NEW_LINE print ( " Yes " ) if findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) else print ( " No " ) NEW_LINE
Find three element from different three arrays such that a + b + c = sum | Function to check if there is an element from each array such that sum of the three elements is equal to given sum . ; Store elements of first array in hash ; sum last two arrays element one by one ; Consider current pair and find if there is an element in a1 [ ] such that these three form a required triplet ; Driver code
def findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( n1 ) : NEW_LINE INDENT s . add ( a1 [ i ] ) NEW_LINE DEDENT for i in range ( n2 ) : NEW_LINE INDENT for j in range ( n3 ) : NEW_LINE INDENT if sum - a2 [ i ] - a3 [ j ] in s : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT a1 = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE a2 = [ 2 , 3 , 6 , 1 , 2 ] NEW_LINE a3 = [ 3 , 24 , 5 , 6 ] NEW_LINE n1 = len ( a1 ) NEW_LINE n2 = len ( a2 ) NEW_LINE n3 = len ( a3 ) NEW_LINE sum = 9 NEW_LINE if findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Count quadruplets with sum K from given array | ''Function to return the number of quadruplets having given sum ; '' Initialize answer ; '' All possible first elements ; '' All possible second element ; '' Use map to find the  fourth element ; '' All possible third elements ; '' Calculate number of valid  4th elements ; '' Update the twice_count ; '' Unordered pairs ; '' Return answer ; ''Driver Code ; '' Given array arr[] ; '' Given sum S ; '' Function Call
def countSum ( a , n , sum ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n - 3 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 2 , 1 ) : NEW_LINE INDENT req = sum - a [ i ] - a [ j ] NEW_LINE m = { } NEW_LINE for k in range ( j + 1 , n , 1 ) : NEW_LINE INDENT m [ a [ k ] ] = m . get ( a [ k ] , 0 ) + 1 NEW_LINE DEDENT twice_count = 0 NEW_LINE for k in range ( j + 1 , n , 1 ) : NEW_LINE INDENT twice_count += m . get ( req - a [ k ] , 0 ) NEW_LINE if ( req - a [ k ] == a [ k ] ) : NEW_LINE INDENT twice_count -= 1 NEW_LINE DEDENT DEDENT count += twice_count // 2 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 5 , 3 , 1 , 2 , 4 ] NEW_LINE S = 13 NEW_LINE N = len ( arr ) NEW_LINE print ( countSum ( arr , N , S ) ) NEW_LINE DEDENT
Find four elements a , b , c and d in an array such that a + b = c + d | function to find a , b , c , d such that ( a + b ) = ( c + d ) ; Create an empty hashmap to store mapping from sum to pair indexes ; Traverse through all possible pairs of arr [ ] ; Sum already present in hash ; driver program
def find_pair_of_sum ( arr : list , n : int ) : NEW_LINE INDENT map = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT sum = arr [ i ] + arr [ j ] NEW_LINE if sum in map : NEW_LINE INDENT print ( f " { map [ sum ] } ▁ and ▁ ( { arr [ i ] } , ▁ { arr [ j ] } ) " ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT map [ sum ] = ( arr [ i ] , arr [ j ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 4 , 7 , 1 , 2 , 9 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE find_pair_of_sum ( arr , n ) NEW_LINE DEDENT
Check if all subarrays contains at least one unique element | ''Function to check if all subarrays have at least one unique element ; '' Generate all subarray ; ' ' ▁ Store ▁ frequency ▁ of ▁ ▁ subarray ' s elements ; '' Traverse the array over  the range [i, N] ; '' Update frequency of  current subarray in map ; '' Increment count ; '' Decrement count ; '' If all subarrays have at  least 1 unique element ; ''Driver Code ; '' Given array arr[] ; '' Function Call
def check ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT hm = { } NEW_LINE count = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT hm [ arr [ j ] ] = hm . get ( arr [ j ] , 0 ) + 1 NEW_LINE if ( hm [ arr [ j ] ] == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( hm [ arr [ j ] ] == 2 ) : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT if ( count == 0 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT DEDENT return " Yes " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( check ( arr , N ) ) NEW_LINE DEDENT
Count all disjoint pairs having absolute difference at least K from a given array | ''Function to count distinct pairs with absolute difference atleast K ; '' Track the element that  have been paired ; '' Stores count of distinct pairs ; '' Pick all elements one by one ; '' If already visited ; '' If already visited ; '' If difference is at least K ; '' Mark element as visited and  increment the count ; '' Print the final count ; ''Driver Code ; '' Given arr[] ; '' Size of array ; '' Given difference K ; '' Function Call
def countPairsWithDiffK ( arr , N , K ) : NEW_LINE INDENT vis = [ 0 ] * N NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( vis [ i ] == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( vis [ j ] == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( abs ( arr [ i ] - arr [ j ] ) >= K ) : NEW_LINE INDENT count += 1 NEW_LINE vis [ i ] = 1 NEW_LINE vis [ j ] = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 3 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE countPairsWithDiffK ( arr , N , K ) NEW_LINE DEDENT
Check if two trees are Mirror | A binary tree node ; Given two trees , return true if they are mirror of each other ; Base case : Both empty ; If only one is empty ; Both non - empty , compare them recursively . Note that in recursive calls , we pass left of one tree and right of other tree ; Driver code
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 areMirror ( a , b ) : NEW_LINE INDENT if a is None and b is None : NEW_LINE INDENT return True NEW_LINE DEDENT if a is None or b is None : NEW_LINE INDENT return False NEW_LINE DEDENT return ( a . data == b . data and areMirror ( a . left , b . right ) and areMirror ( a . right , b . left ) ) NEW_LINE DEDENT root1 = Node ( 1 ) NEW_LINE root2 = Node ( 1 ) NEW_LINE root1 . left = Node ( 2 ) NEW_LINE root1 . right = Node ( 3 ) NEW_LINE root1 . left . left = Node ( 4 ) NEW_LINE root1 . left . right = Node ( 5 ) NEW_LINE root2 . left = Node ( 3 ) NEW_LINE root2 . right = Node ( 2 ) NEW_LINE root2 . right . left = Node ( 5 ) NEW_LINE root2 . right . right = Node ( 4 ) NEW_LINE if areMirror ( root1 , root2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find the length of largest subarray with 0 sum | Returns the maximum length ; NOTE : Dictonary in python in implemented as Hash Maps Create an empty hash map ( dictionary ) ; Initialize sum of elements ; Initialize result ; Traverse through the given array ; Add the current element to the sum ; NOTE : ' in ' operation in dictionary to search key takes O ( 1 ) . Look if current sum is seen before ; else put this sum in dictionary ; test array
def maxLen ( arr ) : NEW_LINE INDENT hash_map = { } NEW_LINE curr_sum = 0 NEW_LINE max_len = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT curr_sum += arr [ i ] NEW_LINE if arr [ i ] is 0 and max_len is 0 : NEW_LINE INDENT max_len = 1 NEW_LINE DEDENT if curr_sum is 0 : NEW_LINE INDENT max_len = i + 1 NEW_LINE DEDENT if curr_sum in hash_map : NEW_LINE INDENT max_len = max ( max_len , i - hash_map [ curr_sum ] ) NEW_LINE DEDENT else : NEW_LINE INDENT hash_map [ curr_sum ] = i NEW_LINE DEDENT DEDENT return max_len NEW_LINE DEDENT arr = [ 15 , - 2 , 2 , - 8 , 1 , 7 , 10 , 13 ] NEW_LINE print " Length ▁ of ▁ the ▁ longest ▁ 0 ▁ sum ▁ subarray ▁ is ▁ % ▁ d " % maxLen ( arr ) NEW_LINE
Print alternate elements of an array | ''Function to print Alternate elements of the given array ; '' Print elements  at odd positions ; '' Print elements of array ; ''Driver Code
def printAlter ( arr , N ) : NEW_LINE INDENT for currIndex in range ( 0 , N , 2 ) : NEW_LINE INDENT print ( arr [ currIndex ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE printAlter ( arr , N ) NEW_LINE DEDENT