codeparrot/xlcost-text-to-code huggingface dataset:
Count of Palindromic Strings possible by swapping of a pair of Characters | Function to return the count of possible palindromic strings ; Stores the frequencies of each character ; Stores the length of the string ; Increase the number of swaps , the current character make with its previous occurrences ; Increase frequency ; Driver Code
def findNewString ( s ) : 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 ans += freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] NEW_LINE freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT s = " aaabaaa " NEW_LINE print ( findNewString ( s ) ) NEW_LINE
Maximum possible difference of two subsets of an array | function for maximum subset diff ; construct hash for positive elements ; calculate subset sum for positive elements ; construct hash for negative elements ; calculate subset sum for negative elements ; Driver Code
def maxDiff ( arr , n ) : NEW_LINE INDENT hashPositive = dict ( ) NEW_LINE hashNegative = dict ( ) NEW_LINE SubsetSum_1 , SubsetSum_2 = 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT hashPositive [ arr [ i ] ] = hashPositive . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 and arr [ i ] in hashPositive . keys ( ) and hashPositive [ arr [ i ] ] == 1 ) : NEW_LINE INDENT SubsetSum_1 += arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT hashNegative [ abs ( arr [ i ] ) ] = hashNegative . get ( abs ( arr [ i ] ) , 0 ) + 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < 0 and abs ( arr [ i ] ) in hashNegative . keys ( ) and hashNegative [ abs ( arr [ i ] ) ] == 1 ) : NEW_LINE INDENT SubsetSum_2 += arr [ i ] NEW_LINE DEDENT DEDENT return abs ( SubsetSum_1 - SubsetSum_2 ) NEW_LINE DEDENT arr = [ 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ Difference ▁ = " , maxDiff ( arr , n ) ) NEW_LINE
Change the given string according to the given conditions | Function to proofread the spells ; Loop to iterate over the characters of the string ; Push the current character c in the stack ; Check for Rule 1 ; Check for Rule 2 ; To store the resultant string ; Loop to iterate over the characters of stack ; Return the resultant string ; Given string str ; Function Call
def proofreadSpell ( str ) : NEW_LINE INDENT result = [ ] NEW_LINE for c in str : NEW_LINE INDENT result . append ( c ) NEW_LINE n = len ( result ) NEW_LINE if ( n >= 3 ) : NEW_LINE INDENT if ( result [ n - 1 ] == result [ n - 2 ] and result [ n - 1 ] == result [ n - 3 ] ) : NEW_LINE INDENT result . pop ( ) NEW_LINE DEDENT DEDENT n = len ( result ) NEW_LINE if ( n >= 4 ) : NEW_LINE INDENT if ( result [ n - 1 ] == result [ n - 2 ] and result [ n - 3 ] == result [ n - 4 ] ) : NEW_LINE INDENT result . pop ( ) NEW_LINE DEDENT DEDENT DEDENT resultStr = " " NEW_LINE for c in result : NEW_LINE INDENT resultStr += c NEW_LINE DEDENT return resultStr NEW_LINE DEDENT str = " hello " NEW_LINE print ( proofreadSpell ( str ) ) NEW_LINE
Smallest subarray with k distinct numbers | Prints the minimum range that contains exactly k distinct numbers . ; Consider every element as starting point . ; Find the smallest window starting with arr [ i ] and containing exactly k distinct elements . ; There are less than k distinct elements now , so no need to continue . ; If there was no window with k distinct elements ( k is greater than total distinct elements ) ; Driver code
def minRange ( arr , n , k ) : NEW_LINE INDENT l = 0 NEW_LINE r = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = [ ] NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT s . append ( arr [ j ] ) NEW_LINE if ( len ( s ) == k ) : NEW_LINE INDENT if ( ( j - i ) < ( r - l ) ) : NEW_LINE INDENT r = j NEW_LINE l = i NEW_LINE DEDENT break NEW_LINE DEDENT DEDENT if ( j == n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( l == 0 and r == n ) : NEW_LINE INDENT print ( " Invalid ▁ k " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( l , r ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE minRange ( arr , n , k ) NEW_LINE DEDENT
Smallest subarray with k distinct numbers | Python3 program to find the minimum range that contains exactly k distinct numbers . ; Prints the minimum range that contains exactly k distinct numbers . ; Initially left and right side is - 1 and - 1 , number of distinct elements are zero and range is n . ; Initialize right side ; increment right side . ; if number of distinct elements less than k . ; if distinct elements are equal to k and length is less than previous length . ; if number of distinct elements less than k , then break . ; if distinct elements equals to k then try to increment left side . ; increment left side . ; it is same as explained in above loop . ; Driver code for above function .
from collections import defaultdict NEW_LINE def minRange ( arr , n , k ) : NEW_LINE INDENT l , r = 0 , n NEW_LINE i = 0 NEW_LINE j = - 1 NEW_LINE hm = defaultdict ( lambda : 0 ) NEW_LINE while i < n : NEW_LINE INDENT while j < n : NEW_LINE INDENT j += 1 NEW_LINE if len ( hm ) < k and j < n : NEW_LINE INDENT hm [ arr [ j ] ] += 1 NEW_LINE DEDENT if len ( hm ) == k and ( ( r - l ) >= ( j - i ) ) : NEW_LINE INDENT l , r = i , j NEW_LINE break NEW_LINE DEDENT DEDENT if len ( hm ) < k : NEW_LINE INDENT break NEW_LINE DEDENT while len ( hm ) == k : NEW_LINE INDENT if hm [ arr [ i ] ] == 1 : NEW_LINE INDENT del ( hm [ arr [ i ] ] ) NEW_LINE DEDENT else : NEW_LINE INDENT hm [ arr [ i ] ] -= 1 NEW_LINE DEDENT i += 1 NEW_LINE if len ( hm ) == k and ( r - l ) >= ( j - i ) : NEW_LINE INDENT l , r = i , j NEW_LINE DEDENT DEDENT if hm [ arr [ i ] ] == 1 : NEW_LINE INDENT del ( hm [ arr [ i ] ] ) NEW_LINE DEDENT else : NEW_LINE INDENT hm [ arr [ i ] ] -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if l == 0 and r == n : NEW_LINE INDENT print ( " Invalid ▁ k " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( l , r ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 2 , 3 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE minRange ( arr , n , k ) NEW_LINE DEDENT
Sum of f ( a [ i ] , a [ j ] ) over all pairs in an array of n integers | Function to calculate the sum ; map to keep a count of occurrences ; Traverse in the list from start to end number of times a [ i ] can be in a pair and to get the difference we subtract pre_sum . ; if the ( a [ i ] - 1 ) is present then subtract that value as f ( a [ i ] , a [ i ] - 1 ) = 0 ; if the ( a [ i ] + 1 ) is present then add that value as f ( a [ i ] , a [ i ] - 1 ) = 0 here we add as a [ i ] - ( a [ i ] - 1 ) < 0 which would have been added as negative sum , so we add to remove this pair from the sum value ; keeping a counter for every element ; Driver Code
def sum ( a , n ) : NEW_LINE INDENT cnt = dict ( ) NEW_LINE ans = 0 NEW_LINE pre_sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += ( i * a [ i ] ) - pre_sum NEW_LINE pre_sum += a [ i ] NEW_LINE if ( a [ i ] - 1 ) in cnt : NEW_LINE INDENT ans -= cnt [ a [ i ] - 1 ] NEW_LINE DEDENT if ( a [ i ] + 1 ) in cnt : NEW_LINE INDENT ans += cnt [ a [ i ] + 1 ] NEW_LINE DEDENT if a [ i ] not in cnt : NEW_LINE INDENT cnt [ a [ i ] ] = 0 NEW_LINE DEDENT cnt [ a [ i ] ] += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 2 , 3 , 1 , 3 ] NEW_LINE n = len ( a ) NEW_LINE print ( sum ( a , n ) ) NEW_LINE DEDENT
Minimum distance between the maximum and minimum element of a given Array | Python3 Program to implement the above approach ; Function to find the minimum distance between the minimum and the maximum element ; Stores the minimum and maximum array element ; Stores the most recently traversed indices of the minimum and the maximum element ; Stores the minimum distance between the minimum and the maximium ; Find the maximum and the minimum element from the given array ; Find the minimum distance ; Check if current element is equal to minimum ; Check if current element is equal to maximum ; If both the minimum and the maximum element has occurred at least once ; Driver Code
import sys NEW_LINE def minDistance ( a , n ) : NEW_LINE INDENT maximum = - 1 NEW_LINE minimum = sys . maxsize NEW_LINE min_index = - 1 NEW_LINE max_index = - 1 NEW_LINE min_dist = n + 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] > maximum ) : NEW_LINE INDENT maximum = a [ i ] NEW_LINE DEDENT if ( a [ i ] < minimum ) : NEW_LINE INDENT minimum = a [ i ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] == minimum ) : NEW_LINE INDENT min_index = i NEW_LINE DEDENT if ( a [ i ] == maximum ) : NEW_LINE INDENT max_index = i NEW_LINE DEDENT if ( min_index != - 1 and max_index != - 1 ) : NEW_LINE INDENT min_dist = ( min ( min_dist , abs ( min_index - max_index ) ) ) NEW_LINE DEDENT DEDENT return min_dist NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 8 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( minDistance ( a , n ) ) NEW_LINE DEDENT
Kth diagonal from the top left of a given matrix | Function returns required diagonal ; Initialize values to prupper diagonals ; Initialize values to print lower diagonals ; Traverse the diagonals ; Print its contents ; Driver code
def printDiagonal ( K , N , M ) : NEW_LINE INDENT startrow , startcol = 0 , 0 NEW_LINE if K - 1 < N : NEW_LINE INDENT startrow = K - 1 NEW_LINE startcol = 0 NEW_LINE DEDENT else : NEW_LINE INDENT startrow = N - 1 NEW_LINE startcol = K - N NEW_LINE DEDENT while startrow >= 0 and startcol < N : NEW_LINE INDENT print ( M [ startrow ] [ startcol ] , end = " ▁ " ) NEW_LINE startrow -= 1 NEW_LINE startcol += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , K = 3 , 4 NEW_LINE M = [ [ 4 , 7 , 8 ] , [ 9 , 2 , 3 ] , [ 0 , 4 , 1 ] ] NEW_LINE printDiagonal ( K , N , M ) NEW_LINE DEDENT
Longest substring that starts with X and ends with Y | Function returns length of longest substring starting with X and ending with Y ; Length of string ; Find the length of the string starting with X from the beginning ; Find the length of the string ending with Y from the end ; Longest substring ; Print the length ; Driver Code ; Given string str ; Starting and Ending characters ; Function Call
def longestSubstring ( str , X , Y ) : NEW_LINE INDENT N = len ( str ) NEW_LINE start = 0 NEW_LINE end = N - 1 NEW_LINE xPos = 0 NEW_LINE yPos = 0 NEW_LINE while ( True ) : NEW_LINE INDENT if ( str [ start ] == X ) : NEW_LINE INDENT xPos = start NEW_LINE break NEW_LINE DEDENT start += 1 NEW_LINE DEDENT while ( True ) : NEW_LINE INDENT if ( str [ end ] == Y ) : NEW_LINE INDENT yPos = end NEW_LINE break NEW_LINE DEDENT end -= 1 NEW_LINE DEDENT length = ( yPos - xPos ) + 1 NEW_LINE print ( length ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " HASFJGHOGAKZZFEGA " NEW_LINE X = ' A ' NEW_LINE Y = ' Z ' NEW_LINE longestSubstring ( str , X , Y ) NEW_LINE DEDENT
Find parent of given node in a Binary Tree with given postorder traversal | Python implementation to find the parent of the given node ; Function to find the parent of the given node ; Check whether the given node is a root node . if it is then return - 1 because root node has no parent ; Loop till we found the given node ; Find the middle node of the tree because at every level tree parent is divided into two halves ; if the node is found return the parent always the child nodes of every node is node / 2 or ( node - 1 ) ; if the node to be found is greater than the mid search for left subtree else search in right subtree ; Driver code
import math NEW_LINE def findParent ( height , node ) : NEW_LINE INDENT start = 1 NEW_LINE end = pow ( 2 , height ) - 1 NEW_LINE if ( end == node ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT while ( node >= 1 ) : NEW_LINE INDENT end = end - 1 NEW_LINE mid = start + ( end - start ) // 2 NEW_LINE if ( mid == node or end == node ) : NEW_LINE INDENT return ( end + 1 ) NEW_LINE DEDENT elif ( node < mid ) : NEW_LINE INDENT end = mid NEW_LINE DEDENT else : NEW_LINE INDENT start = mid NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT height = 4 NEW_LINE node = 6 NEW_LINE k = findParent ( height , node ) NEW_LINE print ( k ) NEW_LINE DEDENT
Replace every element with the smallest of the others | Python3 code for the above approach . ; There should be atleast two elements ; If current element is smaller than firstSmallest then update both firstSmallest and secondSmallest ; If arr [ i ] is in between firstSmallest and secondSmallest then update secondSmallest ; Replace every element by smallest of all other elements ; Print the modified array . ; Driver code
def ReplaceElements ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " Invalid ▁ Input " ) NEW_LINE return NEW_LINE DEDENT firstSmallest = 10 ** 18 NEW_LINE secondSmallest = 10 ** 18 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < firstSmallest ) : NEW_LINE INDENT secondSmallest = firstSmallest NEW_LINE firstSmallest = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] < secondSmallest and arr [ i ] != firstSmallest ) : NEW_LINE INDENT secondSmallest = arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != firstSmallest ) : NEW_LINE INDENT arr [ i ] = firstSmallest NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = secondSmallest NEW_LINE DEDENT DEDENT for i in arr : NEW_LINE INDENT print ( i , end = " , ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 2 , 1 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE ReplaceElements ( arr , n ) NEW_LINE DEDENT
Count subarrays with equal number of 1 ' s ▁ and ▁ 0' s | function to count subarrays with equal number of 1 ' s ▁ and ▁ 0' s ; ' um ' implemented as hash table to store frequency of values obtained through cumulative sum ; Traverse original array and compute cumulative sum and increase count by 1 for this sum in ' um ' . Adds ' - 1' when arr [ i ] == 0 ; traverse the hash table um ; If there are more than one prefix subarrays with a particular sum ; add the subarrays starting from 1 st element and have equal number of 1 ' s ▁ and ▁ 0' s ; required count of subarrays ; Driver code to test above
def countSubarrWithEqualZeroAndOne ( arr , n ) : NEW_LINE INDENT um = dict ( ) NEW_LINE curr_sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT curr_sum += ( - 1 if ( arr [ i ] == 0 ) else arr [ i ] ) NEW_LINE if um . get ( curr_sum ) : NEW_LINE INDENT um [ curr_sum ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT um [ curr_sum ] = 1 NEW_LINE DEDENT DEDENT count = 0 NEW_LINE for itr in um : NEW_LINE INDENT if um [ itr ] > 1 : NEW_LINE INDENT count += ( ( um [ itr ] * int ( um [ itr ] - 1 ) ) / 2 ) NEW_LINE DEDENT DEDENT if um . get ( 0 ) : NEW_LINE INDENT count += um [ 0 ] NEW_LINE DEDENT return int ( count ) NEW_LINE DEDENT arr = [ 1 , 0 , 0 , 1 , 0 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Count ▁ = " , countSubarrWithEqualZeroAndOne ( arr , n ) ) NEW_LINE
Longest subarray having count of 1 s one more than count of 0 s | function to find the length of longest subarray having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; unordered_map ' um ' implemented as hash table ; traverse the given array ; consider '0' as '-1   ; when subarray starts form index '0   ; make an entry for ' sum ' if it is not present in 'um   ; check if ' sum - 1' is present in ' um ' or not ; update maxLength ; required maximum length ; Driver code
def lenOfLongSubarr ( arr , n ) : NEW_LINE INDENT um = { i : 0 for i in range ( 10 ) } NEW_LINE sum = 0 NEW_LINE maxLen = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] == 0 : NEW_LINE INDENT sum += - 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum += 1 NEW_LINE DEDENT if ( sum == 1 ) : NEW_LINE INDENT maxLen = i + 1 NEW_LINE DEDENT elif ( sum not in um ) : NEW_LINE INDENT um [ sum ] = i NEW_LINE DEDENT if ( ( sum - 1 ) in um ) : NEW_LINE INDENT if ( maxLen < ( i - um [ sum - 1 ] ) ) : NEW_LINE INDENT maxLen = i - um [ sum - 1 ] NEW_LINE DEDENT DEDENT DEDENT return maxLen NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 1 , 1 , 0 , 0 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Length ▁ = " , lenOfLongSubarr ( arr , n ) ) NEW_LINE DEDENT
Print all triplets in sorted array that form AP | Function to print all triplets in given sorted array that forms AP ; Use hash to find if there is a previous element with difference equal to arr [ j ] - arr [ i ] ; Driver code
def printAllAPTriplets ( arr , n ) : NEW_LINE INDENT s = [ ] ; NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT diff = arr [ j ] - arr [ i ] ; NEW_LINE if ( ( arr [ i ] - diff ) in arr ) : NEW_LINE INDENT print ( " { } ▁ { } ▁ { } " . format ( ( arr [ i ] - diff ) , arr [ i ] , arr [ j ] ) , end =   " " ) ; NEW_LINE DEDENT DEDENT DEDENT s . append ( arr [ i ] ) ; NEW_LINE DEDENT arr = [ 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE printAllAPTriplets ( arr , n ) ; NEW_LINE
Print all triplets in sorted array that form AP | Function to print all triplets in given sorted array that forms AP ; Search other two elements of AP with arr [ i ] as middle . ; if a triplet is found ; Since elements are distinct , arr [ k ] and arr [ j ] cannot form any more triplets with arr [ i ] ; If middle element is more move to higher side , else move lower side . ; Driver code
def printAllAPTriplets ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT j = i - 1 NEW_LINE k = i + 1 NEW_LINE while ( j >= 0 and k < n ) : NEW_LINE INDENT if ( arr [ j ] + arr [ k ] == 2 * arr [ i ] ) : NEW_LINE INDENT print ( arr [ j ] , " " , arr [ i ] , " " , arr [ k ] ) NEW_LINE k += 1 NEW_LINE j -= 1 NEW_LINE DEDENT elif ( arr [ j ] + arr [ k ] < 2 * arr [ i ] ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT else : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 ] NEW_LINE n = len ( arr ) NEW_LINE printAllAPTriplets ( arr , n ) NEW_LINE
Check if there is a root to leaf path with given sequence | Class of Node ; Util function ; If root is NULL or reached end of the array ; If current node is leaf ; If current node is equal to arr [ index ] this means that till this level path has been matched and remaining path can be either in left subtree or right subtree . ; Function to check given sequence of root to leaf path exist in tree or not . index represents current element in sequence of rooth to leaf path ; Driver Code ; arr [ ] : sequence of root to leaf path
class Node : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . val = val NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def existPathUtil ( root , arr , n , index ) : NEW_LINE INDENT if not root or index == n : NEW_LINE INDENT return False NEW_LINE DEDENT if not root . left and not root . right : NEW_LINE INDENT if root . val == arr [ index ] and index == n - 1 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT return ( ( index < n ) and ( root . val == arr [ index ] ) and ( existPathUtil ( root . left , arr , n , index + 1 ) or existPathUtil ( root . right , arr , n , index + 1 ) ) ) NEW_LINE DEDENT def existPath ( root , arr , n , index ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return ( n == 0 ) NEW_LINE DEDENT return existPathUtil ( root , arr , n , 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 8 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE root = Node ( 5 ) NEW_LINE root . left = Node ( 3 ) NEW_LINE root . right = Node ( 8 ) NEW_LINE root . left . left = Node ( 2 ) NEW_LINE root . left . right = Node ( 4 ) NEW_LINE root . left . left . left = Node ( 1 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . left . right = Node ( 7 ) NEW_LINE if existPath ( root , arr , n , 0 ) : NEW_LINE INDENT print ( " Path ▁ Exists " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Path ▁ does ▁ not ▁ Exist " ) NEW_LINE DEDENT DEDENT
All unique triplets that sum up to a given value | Function to all find unique triplets without using extra space ; Sort the input array ; For handling the cases when no such triplets exits . ; Iterate over the array from start to n - 2. ; Index of the first element in remaining range . ; Index of the last element ; Setting our new target ; Checking if current element is same as previous ; Checking if current element is same as previous ; If we found the triplets then print it and set the flag ; If target is greater then increment the start index ; If target is smaller than decrement the end index ; If no such triplets found ; Driver code ; Function call
def findTriplets ( a , n , sum ) : NEW_LINE INDENT a . sort ( ) NEW_LINE flag = False NEW_LINE for i in range ( n - 2 ) : NEW_LINE INDENT if ( i == 0 or a [ i ] > a [ i - 1 ] ) : NEW_LINE INDENT start = i + 1 NEW_LINE end = n - 1 NEW_LINE target = sum - a [ i ] NEW_LINE while ( start < end ) : NEW_LINE INDENT if ( start > i + 1 and a [ start ] == a [ start - 1 ] ) : NEW_LINE INDENT start += 1 NEW_LINE continue NEW_LINE DEDENT if ( end < n - 1 and a [ end ] == a [ end + 1 ] ) : NEW_LINE INDENT end -= 1 NEW_LINE continue NEW_LINE DEDENT if ( target == a [ start ] + a [ end ] ) : NEW_LINE INDENT print ( " [ " , a [ i ] , " , " , a [ start ] , " , " , a [ end ] , " ] " , end = " ▁ " ) NEW_LINE flag = True NEW_LINE start += 1 NEW_LINE end -= 1 NEW_LINE DEDENT elif ( target > ( a [ start ] + a [ end ] ) ) : NEW_LINE INDENT start += 1 NEW_LINE DEDENT else : NEW_LINE INDENT end -= 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( flag == False ) : NEW_LINE INDENT print ( " No ▁ Such ▁ Triplets ▁ Exist " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 12 , 3 , 6 , 1 , 6 , 9 ] NEW_LINE n = len ( a ) NEW_LINE sum = 24 NEW_LINE findTriplets ( a , n , sum ) NEW_LINE DEDENT
Find the position of the given row in a 2 | Python implementation of the approach ; Function to find a row in the given matrix using linear search ; Assume that the current row matched with the given array ; If any element of the current row doesn 't  match with the corresponding element  of the given array ; Set matched to false and break ; ; If matched then return the row number ; No row matched with the given array ; Driver code
m , n = 6 , 4 ; NEW_LINE def linearCheck ( ar , arr ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT matched = True ; NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( ar [ i ] [ j ] != arr [ j ] ) : NEW_LINE INDENT matched = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( matched ) : NEW_LINE INDENT return i + 1 ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 0 , 0 , 1 , 0 ] , [ 10 , 9 , 22 , 23 ] , [ 40 , 40 , 40 , 40 ] , [ 43 , 44 , 55 , 68 ] , [ 81 , 73 , 100 , 132 ] , [ 100 , 75 , 125 , 133 ] ] ; NEW_LINE row = [ 10 , 9 , 22 , 23 ] ; NEW_LINE print ( linearCheck ( mat , row ) ) ; NEW_LINE DEDENT
Count number of triplets with product equal to given number | Method to count such triplets ; Consider all triplets and count if their product is equal to m ; Driver code
def countTriplets ( arr , n , m ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n - 2 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 1 ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] * arr [ j ] * arr [ k ] == m ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 4 , 6 , 2 , 3 , 8 ] NEW_LINE m = 24 NEW_LINE print ( countTriplets ( arr , len ( arr ) , m ) ) NEW_LINE DEDENT