codeparrot/xlcost-text-to-code huggingface dataset:
Sum of array elements which are prime factors of a given number | Function to check if a number is prime or not ; Corner cases ; Check if n is a multiple of 2 or 3 ; Above condition allows to check only for every 6 th number , starting from 5 ; If n is a multiple of i and i + 2 ; Function to find the sum of array elements which are prime factors of K ; Stores the required sum ; Traverse the given array ; If current element is a prime factor of k , add it to the sum ; Print the result ; Given arr [ ] ; Store the size of the array
def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def primeFactorSum ( arr , n , k ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( k % arr [ i ] == 0 and isPrime ( arr [ i ] ) ) : NEW_LINE INDENT sum = sum + arr [ i ] NEW_LINE DEDENT DEDENT print ( sum ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 5 , 6 , 7 , 15 ] NEW_LINE N = len ( arr ) NEW_LINE K = 35 NEW_LINE primeFactorSum ( arr , N , K ) NEW_LINE
Minimum number of array elements from either ends required to be subtracted from X to reduce X to 0 | Python3 Program to implement the above approach ; Function to count the minimum number of operations required to reduce x to 0 ; If sum of the array is less than x ; Stores the count of operations ; Two pointers to traverse the array ; Reduce x by the sum of the entire array ; Iterate until l reaches the front of the array ; If sum of elements from the front exceeds x ; Shift towards left ; If sum exceeds 0 ; Reduce x by elements from the right ; If x is reduced to 0 ; Update the minimum count of operations required ; Driver Code
import math NEW_LINE def minOperations ( nums , x ) : NEW_LINE INDENT if sum ( nums ) < x : NEW_LINE INDENT return - 1 NEW_LINE DEDENT ans = math . inf NEW_LINE l , r = len ( nums ) - 1 , len ( nums ) NEW_LINE x -= sum ( nums ) NEW_LINE while l >= 0 : NEW_LINE INDENT if x <= 0 : NEW_LINE INDENT x += nums [ l ] NEW_LINE l -= 1 NEW_LINE DEDENT if x > 0 : NEW_LINE INDENT r -= 1 NEW_LINE x -= nums [ r ] NEW_LINE DEDENT if x == 0 : NEW_LINE INDENT ans = min ( ans , ( l + 1 ) + ( len ( nums ) - r ) ) NEW_LINE DEDENT DEDENT return ans if ans < math . inf else - 1 NEW_LINE DEDENT nums = [ 1 , 1 , 4 , 2 , 3 ] NEW_LINE x = 5 NEW_LINE print ( minOperations ( nums , x ) ) NEW_LINE
Number of subarrays having even product | Function to count subarrays with even product ; Total number of subarrays ; Counter variables ; Traverse the array ; If current element is odd ; Update count of subarrays with odd product up to index i ; Print count of subarrays with even product ; Driver code ; Input ; Length of an array ; Function call to count even product subarrays
def evenproduct ( arr , length ) : NEW_LINE INDENT total_subarray = length * ( length + 1 ) // 2 NEW_LINE total_odd = 0 NEW_LINE count_odd = 0 NEW_LINE for i in range ( length ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT count_odd = 0 NEW_LINE DEDENT else : NEW_LINE INDENT count_odd += 1 NEW_LINE total_odd += count_odd NEW_LINE DEDENT DEDENT print ( total_subarray - total_odd ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 5 , 4 , 9 ] NEW_LINE length = len ( arr ) NEW_LINE evenproduct ( arr , length ) NEW_LINE DEDENT
Check if all rows of a Binary Matrix have all ones placed adjacently or not | Function to check if all 1 s are placed adjacently in an array or not ; Base Case ; Stores the sum of XOR of all pair of adjacent elements ; Calculate sum of XOR of all pair of adjacent elements ; Check for corner cases ; Return true ; Function to check if all the rows have all 1 s grouped together or not ; Traverse each row ; Check if all 1 s are placed together in the ith row or not ; Function to check if all 1 s in a row are grouped together in a matrix or not ; Print the result ; Given matrix ; Function Call
def checkGroup ( arr ) : NEW_LINE INDENT if len ( arr ) <= 2 : NEW_LINE INDENT return True NEW_LINE DEDENT corner = arr [ 0 ] + arr [ - 1 ] NEW_LINE xorSum = 0 NEW_LINE for i in range ( len ( arr ) - 1 ) : NEW_LINE INDENT xorSum += ( arr [ i ] ^ arr [ i + 1 ] ) NEW_LINE DEDENT if not corner : NEW_LINE INDENT if xorSum > 2 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT elif corner == 1 : NEW_LINE INDENT if xorSum > 1 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if xorSum > 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isInGroupUtil ( mat ) : NEW_LINE INDENT for i in mat : NEW_LINE INDENT if not checkGroup ( i ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isInGroup ( mat ) : NEW_LINE INDENT ans = isInGroupUtil ( mat ) NEW_LINE if ans : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT mat = [ [ 0 , 1 , 1 , 0 ] , [ 1 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 1 ] , [ 1 , 1 , 1 , 0 ] ] NEW_LINE isInGroup ( mat ) NEW_LINE
Print all numbers up to N having product of digits equal to K | Function to find the product of digits of a number ; Stores the product of digits of a number ; Return the product ; Function to print all numbers upto N having product of digits equal to K ; Stores whether any number satisfying the given conditions exists or not ; Iterate over the range [ 1 , N ] ; If product of digits of arr [ i ] is equal to K or not ; Print that number ; If no numbers are found ; Driver Code ; Given value of N & K ; Function call to print all numbers from [ 1 , N ] with product of digits K
def productOfDigits ( N ) : NEW_LINE INDENT product = 1 ; NEW_LINE while ( N != 0 ) : NEW_LINE INDENT product = product * ( N % 10 ) ; NEW_LINE N = N // 10 ; NEW_LINE DEDENT return product ; NEW_LINE DEDENT def productOfDigitsK ( N , K ) : NEW_LINE INDENT flag = 0 ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( K == productOfDigits ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE flag = 1 ; NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 500 ; K = 10 ; NEW_LINE productOfDigitsK ( N , K ) ; NEW_LINE DEDENT
Minimize difference between maximum and minimum array elements by removing a K | Function to minimize difference between maximum and minimum array elements by removing a K - length subarray ; Size of array ; Stores the maximum and minimum in the suffix subarray [ i . . N - 1 ] ; Traverse the array ; Stores the maximum and minimum in the prefix subarray [ 0 . . i - 1 ] ; Store the minimum difference ; Traverse the array ; If the suffix doesn 't exceed  the end of the array ; Store the maximum element in array after removing subarray of size K ; Stores the maximum element in array after removing subarray of size K ; Update minimum difference ; Updating the maxPrefix and minPrefix with current element ; Print the minimum difference ; Driver Code
def minimiseDifference ( arr , K ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE maxSuffix = [ 0 for i in range ( N + 1 ) ] NEW_LINE minSuffix = [ 0 for i in range ( N + 1 ) ] NEW_LINE maxSuffix [ N ] = - 1e9 NEW_LINE minSuffix [ N ] = 1e9 NEW_LINE maxSuffix [ N - 1 ] = arr [ N - 1 ] NEW_LINE minSuffix [ N - 1 ] = arr [ N - 1 ] NEW_LINE i = N - 2 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT maxSuffix [ i ] = max ( maxSuffix [ i + 1 ] , arr [ i ] ) NEW_LINE minSuffix [ i ] = min ( minSuffix [ i + 1 ] , arr [ i ] ) NEW_LINE i -= 1 NEW_LINE DEDENT maxPrefix = arr [ 0 ] NEW_LINE minPrefix = arr [ 0 ] NEW_LINE minDiff = maxSuffix [ K ] - minSuffix [ K ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( i + K <= N ) : NEW_LINE INDENT maximum = max ( maxSuffix [ i + K ] , maxPrefix ) NEW_LINE minimum = min ( minSuffix [ i + K ] , minPrefix ) NEW_LINE minDiff = min ( minDiff , maximum - minimum ) NEW_LINE DEDENT maxPrefix = max ( maxPrefix , arr [ i ] ) NEW_LINE minPrefix = min ( minPrefix , arr [ i ] ) NEW_LINE DEDENT print ( minDiff ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 5 , 8 , 9 , 1 , 2 ] NEW_LINE K = 2 NEW_LINE minimiseDifference ( arr , K ) NEW_LINE DEDENT
Minimize segments required to be removed such that at least one segment intersects with all remaining segments | Pyhton3 program for the above approach ; Function to find the minimum number of segments required to be deleted ; Stores the start and end points ; Traverse segments and fill the startPoints and endPoints ; Sort the startPoints ; Sort the startPoints ; Store the minimum number of deletions required and initialize with ( N - 1 ) ; Traverse the array segments [ ] ; Store the starting point ; Store the ending point ; Store the number of segments satisfying the first condition of non - intersection ; Store the number of segments satisfying the second condition of non - intersection ; Update answer ; Print the answer ; Driver Code ; Function Call
from bisect import bisect_left , bisect_right NEW_LINE def minSegments ( segments , n ) : NEW_LINE INDENT startPoints = [ 0 for i in range ( n ) ] NEW_LINE endPoints = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT startPoints [ i ] = segments [ i ] [ 0 ] NEW_LINE endPoints [ i ] = segments [ i ] [ 1 ] NEW_LINE DEDENT startPoints . sort ( reverse = False ) NEW_LINE endPoints . sort ( reverse = False ) NEW_LINE ans = n - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT f = segments [ i ] [ 0 ] NEW_LINE s = segments [ i ] [ 1 ] NEW_LINE leftDelete = bisect_left ( endPoints , f ) NEW_LINE rightDelete = max ( 0 , n - bisect_right ( startPoints , s ) ) NEW_LINE ans = min ( ans , leftDelete + rightDelete ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 2 ] , [ 5 , 6 ] , [ 6 , 7 ] , [ 7 , 10 ] , [ 8 , 9 ] ] NEW_LINE N = len ( arr ) NEW_LINE minSegments ( arr , N ) NEW_LINE DEDENT
Longest substring where all the characters appear at least K times | Set 3 | Function to find the length of the longest substring ; Store the required answer ; Create a frequency map of the characters of the string ; Store the length of the string ; Traverse the string , s ; Increment the frequency of the current character by 1 ; Stores count of unique characters ; Find the number of unique characters in string ; Iterate in range [ 1 , unique ] ; Initialize frequency of all characters as 0 ; Stores the start and the end of the window ; Stores the current number of unique characters and characters occuring atleast K times ; New unique character ; New character which occurs atleast k times ; Expand window by incrementing end by 1 ; Check if this character is present atleast k times ; Check if this character is unique ; Shrink the window by incrementing start by 1 ; If there are curr_unique characters and each character is atleast k times ; Update the overall maximum length ; Print the answer ;
def longestSubstring ( s , k ) : NEW_LINE INDENT ans = 0 NEW_LINE freq = [ 0 ] * 26 NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT unique = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] != 0 ) : NEW_LINE INDENT unique += 1 NEW_LINE DEDENT DEDENT for curr_unique in range ( 1 , unique + 1 ) : NEW_LINE INDENT Freq = [ 0 ] * 26 NEW_LINE start , end = 0 , 0 NEW_LINE cnt , count_k = 0 , 0 NEW_LINE while ( end < n ) : NEW_LINE INDENT if ( cnt <= curr_unique ) : NEW_LINE INDENT ind = ord ( s [ end ] ) - ord ( ' a ' ) NEW_LINE if ( Freq [ ind ] == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT Freq [ ind ] += 1 NEW_LINE if ( Freq [ ind ] == k ) : NEW_LINE INDENT count_k += 1 NEW_LINE DEDENT end += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ind = ord ( s [ start ] ) - ord ( ' a ' ) NEW_LINE if ( Freq [ ind ] == k ) : NEW_LINE INDENT count_k -= 1 NEW_LINE DEDENT Freq [ ind ] -= 1 NEW_LINE if ( Freq [ ind ] == 0 ) : NEW_LINE INDENT cnt -= 1 NEW_LINE DEDENT start += 1 NEW_LINE DEDENT if ( ( cnt == curr_unique ) and ( count_k == curr_unique ) ) : NEW_LINE INDENT ans = max ( ans , end - start ) NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT / * Driver Code * / NEW_LINE S = " aabbba " NEW_LINE K = 3 NEW_LINE longestSubstring ( S , K ) NEW_LINE
Print matrix elements using DFS traversal | Direction vectors ; Function to check if current position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited or not ; Utility function to prmatrix elements using DFS Traversal ; Mark the current cell visited ; Print element at the cell ; Traverse all four adjacent cells of the current element ; Check if x and y is valid index or not ; Function to print matrix elementsdef ; Function call to prmatrix elements by DFS traversal ; Driver Code ; Given matrix ; Row of the matrix ; Column of the matrix
dRow = [ - 1 , 0 , 1 , 0 ] NEW_LINE dCol = [ 0 , 1 , 0 , - 1 ] NEW_LINE def isValid ( row , col , COL , ROW ) : NEW_LINE INDENT global vis NEW_LINE if ( row < 0 or col < 0 or col > COL - 1 or row > ROW - 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( vis [ row ] [ col ] == True ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def DFSUtil ( row , col , grid , M , N ) : NEW_LINE INDENT global vis NEW_LINE vis [ row ] [ col ] = True NEW_LINE print ( grid [ row ] [ col ] , end = " ▁ " ) NEW_LINE for i in range ( 4 ) : NEW_LINE INDENT x = row + dRow [ i ] NEW_LINE y = col + dCol [ i ] NEW_LINE if ( isValid ( x , y , M , N ) ) : NEW_LINE INDENT DFSUtil ( x , y , grid , M , N ) NEW_LINE DEDENT DEDENT DEDENT def DFS ( row , col , grid , M , N ) : NEW_LINE INDENT global vis NEW_LINE DFSUtil ( 0 , 0 , grid , M , N ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT grid = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] NEW_LINE M = len ( grid ) NEW_LINE N = len ( grid [ 0 ] ) NEW_LINE vis = [ [ False for i in range ( M ) ] for i in range ( N ) ] NEW_LINE DFS ( 0 , 0 , grid , M , N ) NEW_LINE DEDENT
Print matrix elements using DFS traversal | Direction vectors ; Function to check if curruent position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited ; Function to print the matrix elements ; Stores if a position in the matrix been visited or not ; Initialize stack to implement DFS ; Push the first position of grid [ ] [ ] in the stack ; Mark the cell ( 0 , 0 ) visited ; Stores top element of stack ; Delete the top ( ) element of stack ; Print element at the cell ; Traverse in all four adjacent sides of current positions ; Check if x and y is valid position and then push the position of current cell in the stack ; Push the current cell ; Mark current cell visited ; Given matrix ; Row of the matrix ; Column of the matrix
dRow = [ - 1 , 0 , 1 , 0 ] NEW_LINE dCol = [ 0 , 1 , 0 , - 1 ] NEW_LINE vis = [ ] NEW_LINE def isValid ( row , col , COL , ROW ) : NEW_LINE INDENT global vis NEW_LINE if ( row < 0 or col < 0 or col > COL - 1 or row > ROW - 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( vis [ row ] [ col ] == True ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def DFS_iterative ( grid , M , N ) : NEW_LINE INDENT global vis NEW_LINE vis = [ ] NEW_LINE for i in range ( M + 5 ) : NEW_LINE INDENT vis . append ( [ ] ) NEW_LINE for j in range ( N + 5 ) : NEW_LINE INDENT vis [ i ] . append ( False ) NEW_LINE DEDENT DEDENT st = [ ] NEW_LINE st . append ( [ 0 , 0 ] ) NEW_LINE vis [ 0 ] [ 0 ] = True NEW_LINE while ( len ( st ) > 0 ) : NEW_LINE INDENT p = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE row = p [ 0 ] NEW_LINE col = p [ 1 ] NEW_LINE print ( grid [ row ] [ col ] , " " , end = " " ) NEW_LINE for i in range ( 4 ) : NEW_LINE INDENT x = row + dRow [ i ] NEW_LINE y = col + dCol [ i ] NEW_LINE if ( isValid ( x , y , M , N ) ) : NEW_LINE INDENT st . append ( [ x , y ] ) NEW_LINE vis [ x ] [ y ] = True NEW_LINE DEDENT DEDENT DEDENT DEDENT grid = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] NEW_LINE M = len ( grid ) NEW_LINE N = len ( grid [ 0 ] ) NEW_LINE DFS_iterative ( grid , M , N ) NEW_LINE
Segregate 1 s and 0 s in separate halves of a Binary String | Function to count the minimum number of operations required to segregate all 1 s and 0 s in a binary string ; Stores the count of unequal pair of adjacent characters ; If an unequal pair of adjacent characters occurs ; For odd count ; For even count ; Given string ; Length of the string ; Prints the minimum count of operations required
def minOps ( s , N ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( s [ i ] != s [ i - 1 ] ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT if ( ans % 2 == 1 ) : NEW_LINE INDENT print ( ( ans - 1 ) // 2 ) NEW_LINE return NEW_LINE DEDENT print ( ans // 2 ) NEW_LINE DEDENT str = "01011100" NEW_LINE N = len ( str ) NEW_LINE minOps ( str , N ) NEW_LINE
Find index of the element differing in parity with all other array elements | Function to print the array element which differs in parity with the remaining array elements ; Multimaps to store even and odd numbers along with their indices ; Traverse the array ; If array element is even ; Otherwise ; If only one even element is present in the array ; If only one odd element is present in the array ;  ; Given array ; Size of the array
def OddOneOut ( arr , N ) : NEW_LINE INDENT e , o = { } , { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT e [ arr [ i ] ] = i NEW_LINE DEDENT else : NEW_LINE INDENT o [ arr [ i ] ] = i NEW_LINE DEDENT DEDENT if ( len ( e ) == 1 ) : NEW_LINE INDENT print ( list ( e . values ( ) ) [ 0 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( list ( o . values ( ) ) [ 0 ] ) NEW_LINE DEDENT DEDENT / * Driver Code * / NEW_LINE arr = [ 2 , 4 , 7 , 8 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE OddOneOut ( arr , N ) NEW_LINE
Count indices where the maximum in the prefix array is less than that in the suffix array | Function to print the count of indices in which the maximum in prefix arrays is less than that in the suffix array ; If size of array is 1 ; pre [ ] : Prefix array suf [ ] : Suffix array ; Stores the required count ; Find the maximum in prefix array ; Find the maximum in suffix array ; Traverse the array ; If maximum in prefix array is less than maximum in the suffix array ; Print the answer ; Driver Code ; Function Call
def count ( a , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT pre = [ 0 ] * ( n - 1 ) NEW_LINE suf = [ 0 ] * ( n - 1 ) NEW_LINE max = a [ 0 ] NEW_LINE ans = 0 NEW_LINE pre [ 0 ] = a [ 0 ] NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( a [ i ] > max ) : NEW_LINE INDENT max = a [ i ] NEW_LINE DEDENT pre [ i ] = max NEW_LINE DEDENT max = a [ n - 1 ] NEW_LINE suf [ n - 2 ] = a [ n - 1 ] NEW_LINE for i in range ( n - 2 , 0 , - 1 ) : NEW_LINE INDENT if ( a [ i ] > max ) : NEW_LINE INDENT max = a [ i ] NEW_LINE DEDENT suf [ i - 1 ] = max NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( pre [ i ] < suf [ i ] ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 8 , 1 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE count ( arr , N ) NEW_LINE
Length of the longest subsequence such that XOR of adjacent elements is equal to K | Function to find maximum length of subsequence ; Stores maximum length of subsequence ; HashMap to store the longest length of subsequence ending at an integer , say X ; Stores the maximum length of subsequence ending at index i ; Base case ; Iterate over the range [ 1 , N - 1 ] ; If dpj is not NULL ; Update dp [ i ] ; Update ans ; Update the maximum length of subsequence ending at element is a [ i ] in HashMap ; Return the ans if ans >= 2. Otherwise , return 0 ; Driver Code ; Input ; Print the length of the longest subsequence
def xorSubsequence ( a , n , k ) : NEW_LINE INDENT ans = 0 NEW_LINE map = { } NEW_LINE dp = [ 0 ] * n NEW_LINE map [ a [ 0 ] ] = 1 NEW_LINE dp [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] ^ k in map ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , map [ a [ i ] ^ k ] + 1 ) NEW_LINE DEDENT ans = max ( ans , dp [ i ] ) NEW_LINE if a [ i ] in map : NEW_LINE INDENT map [ a [ i ] ] = max ( map [ a [ i ] ] , dp [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT map [ a [ i ] ] = max ( 1 , dp [ i ] ) NEW_LINE DEDENT DEDENT if ans >= 2 : NEW_LINE return ans NEW_LINE return 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 4 , 3 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE K = 1 NEW_LINE print ( xorSubsequence ( arr , N , K ) ) NEW_LINE DEDENT
Sum of subtree depths for every node of a given Binary Tree | Binary tree node ; Constructor to set the data of the newly created tree node ; Function to allocate a new node with the given data and null in its left and right pointers ; DFS function to calculate the sum of depths of all subtrees depth sum ; Store total number of node in its subtree and total sum of depth in its subtree ; Check if left is not null ; Call recursively the DFS function for left child ; Increment the sum of depths by ptemp . first + p . temp . first ; Increment p . first by count of noded in left subtree ; Check if right is not null ; Call recursively the DFS function for right child ; Increment the sum of depths by ptemp . first + p . temp . first ; Increment p . first by count of nodes in right subtree ; Increment the result by total sum of depth in current subtree ; Return p ; Given Tree ; Print the result
class TreeNode : 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 ans = 0 NEW_LINE def newNode ( data ) : NEW_LINE INDENT Node = TreeNode ( data ) NEW_LINE return ( Node ) NEW_LINE DEDENT def sumofsubtree ( root ) : NEW_LINE INDENT global ans NEW_LINE p = [ 1 , 0 ] NEW_LINE if ( root . left != None ) : NEW_LINE INDENT ptemp = sumofsubtree ( root . left ) NEW_LINE p [ 1 ] += ptemp [ 0 ] + ptemp [ 1 ] NEW_LINE p [ 0 ] += ptemp [ 0 ] NEW_LINE DEDENT if ( root . right != None ) : NEW_LINE INDENT ptemp = sumofsubtree ( root . right ) NEW_LINE p [ 1 ] += ptemp [ 0 ] + ptemp [ 1 ] NEW_LINE p [ 0 ] += ptemp [ 0 ] NEW_LINE DEDENT ans += p [ 1 ] NEW_LINE return p NEW_LINE DEDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . left = newNode ( 6 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE root . left . left . left = newNode ( 8 ) NEW_LINE root . left . left . right = newNode ( 9 ) NEW_LINE sumofsubtree ( root ) NEW_LINE print ( ans ) NEW_LINE