codeparrot/xlcost-text-to-code huggingface dataset:
Count number of triplets with product equal to given number | Function to find the triplet ; Consider all pairs and check for a third number so their product is equal to product ; Check if current pair divides product or not If yes , then search for ( product / li [ i ] * li [ j ] ) ; Check if the third number is present in the map and it is not equal to any other two elements and also check if this triplet is not counted already using their indexes ; Driver code
def countTriplets ( li , product ) : NEW_LINE INDENT flag = 0 NEW_LINE count = 0 NEW_LINE for i in range ( len ( li ) ) : NEW_LINE INDENT if li [ i ] != 0 and product % li [ i ] == 0 : NEW_LINE INDENT for j in range ( i + 1 , len ( li ) ) : NEW_LINE INDENT if li [ j ] != 0 and product % ( li [ j ] * li [ i ] ) == 0 : NEW_LINE INDENT if product // ( li [ j ] * li [ i ] ) in li : NEW_LINE INDENT n = li . index ( product // ( li [ j ] * li [ i ] ) ) NEW_LINE if n > i and n > j : NEW_LINE INDENT flag = 1 NEW_LINE count += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT print ( count ) NEW_LINE DEDENT li = [ 1 , 4 , 6 , 2 , 3 , 8 ] NEW_LINE product = 24 NEW_LINE countTriplets ( li , product ) NEW_LINE
Count of index pairs with equal elements in an array | Return the number of pairs with equal values . ; for each index i and j ; finding the index with same value but different index . ; Driven Code
def countPairs ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] == arr [ j ] ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE
Queries to answer the number of ones and zero to the left of given index | Function to pre - calculate the left [ ] array ; Iterate in the binary array ; Initialize the number of 1 and 0 ; Increase the count ; Driver code ; Queries ; Solve queries
def preCalculate ( binary , n , left ) : NEW_LINE INDENT count1 , count0 = 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT left [ i ] [ 0 ] = count1 NEW_LINE left [ i ] [ 1 ] = count0 NEW_LINE if ( binary [ i ] ) : NEW_LINE INDENT count1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count0 += 1 NEW_LINE DEDENT DEDENT DEDENT binary = [ 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 ] NEW_LINE n = len ( binary ) NEW_LINE left = [ [ 0 for i in range ( 2 ) ] for i in range ( n ) ] NEW_LINE preCalculate ( binary , n , left ) NEW_LINE queries = [ 0 , 1 , 2 , 4 ] NEW_LINE q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( left [ queries [ i ] ] [ 0 ] , " ones " , left [ queries [ i ] ] [ 1 ] , " zeros " ) NEW_LINE DEDENT
Count of index pairs with equal elements in an array | Python3 program to count of index pairs with equal elements in an array . ; Return the number of pairs with equal values . ; Finding frequency of each number . ; Calculating pairs of each value . ; Driver Code
import math as mt NEW_LINE def countPairs ( arr , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in mp . keys ( ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for it in mp : NEW_LINE INDENT count = mp [ it ] NEW_LINE ans += ( count * ( count - 1 ) ) // 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE
Partition the array into three equal sum segments | This function returns true if the array can be divided into three equal sum segments ; Prefix Sum Array ; Suffix Sum Array ; Stores the total sum of the array ; We can also take pre [ pos2 - 1 ] - pre [ pos1 ] == total_sum / 3 here . ; Driver Code
def equiSumUtil ( arr , pos1 , pos2 ) : NEW_LINE INDENT n = len ( arr ) ; NEW_LINE pre = [ 0 ] * n ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE pre [ i ] = sum ; NEW_LINE DEDENT suf = [ 0 ] * n ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE suf [ i ] = sum ; NEW_LINE DEDENT total_sum = sum ; NEW_LINE i = 0 ; NEW_LINE j = n - 1 ; NEW_LINE while ( i < j - 1 ) : NEW_LINE INDENT if ( pre [ i ] == total_sum // 3 ) : NEW_LINE INDENT pos1 = i ; NEW_LINE DEDENT if ( suf [ j ] == total_sum // 3 ) : NEW_LINE INDENT pos2 = j ; NEW_LINE DEDENT if ( pos1 != - 1 and pos2 != - 1 ) : NEW_LINE INDENT if ( suf [ pos1 + 1 ] - suf [ pos2 ] == total_sum // 3 ) : NEW_LINE INDENT return [ True , pos1 , pos2 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT return [ False , pos1 , pos2 ] ; NEW_LINE DEDENT DEDENT if ( pre [ i ] < suf [ j ] ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT j -= 1 ; NEW_LINE DEDENT DEDENT return [ False , pos1 , pos2 ] ; NEW_LINE DEDENT def equiSum ( arr ) : NEW_LINE INDENT pos1 = - 1 ; NEW_LINE pos2 = - 1 ; NEW_LINE ans = equiSumUtil ( arr , pos1 , pos2 ) ; NEW_LINE pos1 = ans [ 1 ] ; NEW_LINE pos2 = ans [ 2 ] ; NEW_LINE if ( ans [ 0 ] ) : NEW_LINE INDENT print ( " First ▁ Segment ▁ : ▁ " , end = " " ) ; NEW_LINE for i in range ( pos1 + 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE print ( " Second ▁ Segment ▁ : ▁ " , end = " " ) ; NEW_LINE for i in range ( pos1 + 1 , pos2 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE print ( " Third ▁ Segment ▁ : ▁ " , end = " " ) ; NEW_LINE for i in range ( pos2 , len ( arr ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT println ( " Array ▁ cannot ▁ be ▁ divided ▁ into " , " three ▁ equal ▁ sum ▁ segments " ) ; NEW_LINE DEDENT DEDENT arr = [ 1 , 3 , 6 , 2 , 7 , 1 , 2 , 8 ] ; NEW_LINE equiSum ( arr ) ; NEW_LINE
Print cousins of a given node in Binary Tree | A utility function to create a new Binary Tree Node ; It returns level of the node if it is present in tree , otherwise returns 0. ; base cases ; If node is present in left subtree ; If node is not present in left subtree ; Prnodes at a given level such that sibling of node is not printed if it exists ; Base cases ; If current node is parent of a node with given level ; Recur for left and right subtrees ; This function prints cousins of a given node ; Get level of given node ; Prnodes of given level . ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , item ) : NEW_LINE INDENT self . data = item NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def getLevel ( root , node , level ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( root == node ) : NEW_LINE INDENT return level NEW_LINE DEDENT downlevel = getLevel ( root . left , node , level + 1 ) NEW_LINE if ( downlevel != 0 ) : NEW_LINE INDENT return downlevel NEW_LINE DEDENT return getLevel ( root . right , node , level + 1 ) NEW_LINE DEDENT def printGivenLevel ( root , node , level ) : NEW_LINE INDENT if ( root == None or level < 2 ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( level == 2 ) : NEW_LINE INDENT if ( root . left == node or root . right == node ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . left ) : NEW_LINE INDENT print ( root . left . data , end = " ▁ " ) NEW_LINE DEDENT if ( root . right ) : NEW_LINE INDENT print ( root . right . data , end = " ▁ " ) NEW_LINE DEDENT DEDENT elif ( level > 2 ) : NEW_LINE INDENT printGivenLevel ( root . left , node , level - 1 ) NEW_LINE printGivenLevel ( root . right , node , level - 1 ) NEW_LINE DEDENT DEDENT def printCousins ( root , node ) : NEW_LINE INDENT level = getLevel ( root , node , 1 ) NEW_LINE printGivenLevel ( root , node , level ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT 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 . left . right . right = newNode ( 15 ) NEW_LINE root . right . left = newNode ( 6 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE root . right . left . right = newNode ( 8 ) NEW_LINE printCousins ( root , root . left . right ) NEW_LINE DEDENT
Leftmost and rightmost indices of the maximum and the minimum element of an array | Function to return the index of the rightmost minimum element from the array ; First element is the minimum in a sorted array ; While the elements are equal to the minimum update rightMin ; Final check whether there are any elements which are equal to the minimum ; Function to return the index of the leftmost maximum element from the array ; Last element is the maximum in a sorted array ; While the elements are equal to the maximum update leftMax ; Final check whether there are any elements which are equal to the maximum ; Driver code ; First element is the leftmost minimum in a sorted array ; Last element is the rightmost maximum in a sorted array
def getRightMin ( arr , n ) : NEW_LINE INDENT min = arr [ 0 ] NEW_LINE rightMin = 0 NEW_LINE i = 1 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] == min ) : NEW_LINE INDENT rightMin = i NEW_LINE DEDENT i *= 2 NEW_LINE DEDENT i = rightMin + 1 NEW_LINE while ( i < n and arr [ i ] == min ) : NEW_LINE INDENT rightMin = i NEW_LINE i += 1 NEW_LINE DEDENT return rightMin NEW_LINE DEDENT def getLeftMax ( arr , n ) : NEW_LINE INDENT max = arr [ n - 1 ] NEW_LINE leftMax = n - 1 NEW_LINE i = n - 2 NEW_LINE while ( i > 0 ) : NEW_LINE INDENT if ( arr [ i ] == max ) : NEW_LINE INDENT leftMax = i NEW_LINE DEDENT i = int ( i / 2 ) NEW_LINE DEDENT i = leftMax - 1 NEW_LINE while ( i >= 0 and arr [ i ] == max ) : NEW_LINE INDENT leftMax = i NEW_LINE i -= 1 NEW_LINE DEDENT return leftMax NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ left ▁ : " , 0 ) NEW_LINE print ( " Minimum ▁ right ▁ : " , getRightMin ( arr , n ) ) NEW_LINE print ( " Maximum ▁ left ▁ : " , getLeftMax ( arr , n ) ) NEW_LINE print ( " Maximum ▁ right ▁ : " , ( n - 1 ) ) NEW_LINE DEDENT
Elements to be added so that all elements of a range are present in array | Function to count numbers to be added ; Sort the array ; Check if elements are consecutive or not . If not , update count ; Drivers code
def countNum ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i + 1 ] and arr [ i ] != arr [ i + 1 ] - 1 ) : NEW_LINE INDENT count += arr [ i + 1 ] - arr [ i ] - 1 ; NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 3 , 5 , 8 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countNum ( arr , n ) ) NEW_LINE
Find the k smallest numbers after deleting given elements | Python3 program to find the k maximum number from the array after n deletions ; Find k maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else push it ; Driver code
import math as mt NEW_LINE def findElementsAfterDel ( arr , m , dell , n , k ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if dell [ i ] in mp . keys ( ) : NEW_LINE INDENT mp [ dell [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ dell [ i ] ] = 1 NEW_LINE DEDENT DEDENT heap = list ( ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT if ( arr [ i ] in mp . keys ( ) ) : NEW_LINE INDENT mp [ arr [ i ] ] -= 1 NEW_LINE if ( mp [ arr [ i ] ] == 0 ) : NEW_LINE INDENT mp . pop ( arr [ i ] ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT heap . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT heap . sort ( ) NEW_LINE return heap [ : k ] NEW_LINE DEDENT array = [ 5 , 12 , 33 , 4 , 56 , 12 , 20 ] NEW_LINE m = len ( array ) NEW_LINE dell = [ 12 , 56 , 5 ] NEW_LINE n = len ( dell ) NEW_LINE k = 3 NEW_LINE print ( * findElementsAfterDel ( array , m , dell , n , k ) ) NEW_LINE
Elements to be added so that all elements of a range are present in array | Function to count numbers to be added ; Make a hash of elements and store minimum and maximum element ; Traverse all elements from minimum to maximum and count if it is not in the hash ; Driver code
def countNum ( arr , n ) : NEW_LINE INDENT s = dict ( ) NEW_LINE count , maxm , minm = 0 , - 10 ** 9 , 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT s [ arr [ i ] ] = 1 NEW_LINE if ( arr [ i ] < minm ) : NEW_LINE INDENT minm = arr [ i ] NEW_LINE DEDENT if ( arr [ i ] > maxm ) : NEW_LINE INDENT maxm = arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( minm , maxm + 1 ) : NEW_LINE INDENT if i not in s . keys ( ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 3 , 5 , 8 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countNum ( arr , n ) ) NEW_LINE
Find minimum speed to finish all Jobs | Function to check if the person can do all jobs in H hours with speed K ; Function to return the minimum speed of person to complete all jobs ; If H < N it is not possible to complete all jobs as person can not move from one element to another during current hour ; Max element of array ; Use binary search to find smallest K ; Driver Code ; Print required maxLenwer
def isPossible ( A , n , H , K ) : NEW_LINE INDENT time = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT time += ( A [ i ] - 1 ) // K + 1 NEW_LINE DEDENT return time <= H NEW_LINE DEDENT def minJobSpeed ( A , n , H ) : NEW_LINE INDENT if H < n : NEW_LINE INDENT return - 1 NEW_LINE DEDENT Max = max ( A ) NEW_LINE lo , hi = 1 , Max NEW_LINE while lo < hi : NEW_LINE INDENT mi = lo + ( hi - lo ) // 2 NEW_LINE if not isPossible ( A , n , H , mi ) : NEW_LINE INDENT lo = mi + 1 NEW_LINE DEDENT else : NEW_LINE INDENT hi = mi NEW_LINE DEDENT DEDENT return lo NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 6 , 7 , 11 ] NEW_LINE H = 8 NEW_LINE n = len ( A ) NEW_LINE print ( minJobSpeed ( A , n , H ) ) NEW_LINE DEDENT
Number of anomalies in an array | Python3 program to implement the above approach ; Sort the array so that we can apply binary search . ; One by one check every element if it is anomaly or not using binary search . ; If arr [ i ] is not largest element and element just greater than it is within k , then return False . ; If there are more than one occurrences of arr [ i ] , return False . ; If arr [ i ] is not smallest element and just smaller element is not k distance away ; Driver code
def countAnomalies ( a , n , k ) : NEW_LINE INDENT a = sorted ( a ) ; NEW_LINE res = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT u = upper_bound ( a , 0 , n , a [ i ] ) ; NEW_LINE if ( u < n and a [ u ] - a [ i ] <= k ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT s = lower_bound ( a , 0 , n , a [ i ] ) ; NEW_LINE if ( u - s > 1 ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT if ( s > 0 and a [ s - 1 ] - a [ i ] <= k ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT res += 1 ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def lower_bound ( a , low , high , element ) : NEW_LINE INDENT while ( low < high ) : NEW_LINE INDENT middle = int ( low + int ( high - low ) / 2 ) ; NEW_LINE if ( element > a [ middle ] ) : NEW_LINE INDENT low = middle + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT high = middle ; NEW_LINE DEDENT DEDENT return low ; NEW_LINE DEDENT def upper_bound ( a , low , high , element ) : NEW_LINE INDENT while ( low < high ) : NEW_LINE INDENT middle = int ( low + ( high - low ) / 2 ) ; NEW_LINE if ( a [ middle ] > element ) : NEW_LINE INDENT high = middle ; NEW_LINE DEDENT else : NEW_LINE INDENT low = middle + 1 ; NEW_LINE DEDENT DEDENT return low ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 1 , 8 ] NEW_LINE k = 5 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countAnomalies ( arr , n , k ) ) ; NEW_LINE DEDENT
Subarrays with distinct elements | Returns sum of lengths of all subarrays with distinct elements . ; For maintaining distinct elements . ; Initialize ending point and result ; Fix starting point ; Calculating and adding all possible length subarrays in arr [ i . . j ] ; Remove arr [ i ] as we pick new stating point from next ; Driven Code
def sumoflength ( arr , n ) : NEW_LINE INDENT s = [ ] NEW_LINE j = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( j < n and ( arr [ j ] not in s ) ) : NEW_LINE INDENT s . append ( arr [ j ] ) NEW_LINE j += 1 NEW_LINE DEDENT ans += ( ( j - i ) * ( j - i + 1 ) ) // 2 NEW_LINE s . remove ( arr [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sumoflength ( arr , n ) ) NEW_LINE DEDENT
Count subarrays having total distinct elements same as original array | Function to calculate distinct sub - array ; Count distinct elements in whole array ; Reset the container by removing all elements ; Use sliding window concept to find count of subarrays having k distinct elements . ; If window size equals to array distinct element size , then update answer ; Decrease the frequency of previous element for next sliding window ; If frequency is zero then decrease the window size ; Driver code
def countDistictSubarray ( arr , n ) : NEW_LINE INDENT vis = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT vis [ arr [ i ] ] = 1 NEW_LINE DEDENT k = len ( vis ) NEW_LINE vid = dict ( ) NEW_LINE ans = 0 NEW_LINE right = 0 NEW_LINE window = 0 NEW_LINE for left in range ( n ) : NEW_LINE INDENT while ( right < n and window < k ) : NEW_LINE INDENT if arr [ right ] in vid . keys ( ) : NEW_LINE INDENT vid [ arr [ right ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT vid [ arr [ right ] ] = 1 NEW_LINE DEDENT if ( vid [ arr [ right ] ] == 1 ) : NEW_LINE INDENT window += 1 NEW_LINE DEDENT right += 1 NEW_LINE DEDENT if ( window == k ) : NEW_LINE INDENT ans += ( n - right + 1 ) NEW_LINE DEDENT vid [ arr [ left ] ] -= 1 NEW_LINE if ( vid [ arr [ left ] ] == 0 ) : NEW_LINE INDENT window -= 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 1 , 3 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countDistictSubarray ( arr , n ) ) NEW_LINE
Count subarrays with same even and odd elements | function that returns the count of subarrays that contain equal number of odd as well as even numbers ; initialize difference and answer with 0 ; initialize these auxiliary arrays with 0 ; since the difference is initially 0 , we have to initialize hash_positive [ 0 ] with 1 ; for loop to iterate through whole array ( zero - based indexing is used ) ; incrementing or decrementing difference based on arr [ i ] being even or odd , check if arr [ i ] is odd ; adding hash value of ' difference ' to our answer as all the previous occurrences of the same difference value will make even - odd subarray ending at index ' i ' . After that , we will increment hash array for that ' difference ' value for its occurrence at index ' i ' . if difference is negative then use hash_negative ; else use hash_positive ; return total number of even - odd subarrays ; Driver code ; Printing total number of even - odd subarrays
def countSubarrays ( arr , n ) : NEW_LINE INDENT difference = 0 NEW_LINE ans = 0 NEW_LINE hash_positive = [ 0 ] * ( n + 1 ) NEW_LINE hash_negative = [ 0 ] * ( n + 1 ) NEW_LINE hash_positive [ 0 ] = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 == 1 ) : NEW_LINE INDENT difference = difference + 1 NEW_LINE DEDENT else : NEW_LINE INDENT difference = difference - 1 NEW_LINE DEDENT if ( difference < 0 ) : NEW_LINE INDENT ans += hash_negative [ - difference ] NEW_LINE hash_negative [ - difference ] = hash_negative [ - difference ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += hash_positive [ difference ] NEW_LINE hash_positive [ difference ] = hash_positive [ difference ] + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 3 , 4 , 6 , 8 , 1 , 10 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Total ▁ Number ▁ of ▁ Even - Odd ▁ subarrays ▁ are ▁ " + str ( countSubarrays ( arr , n ) ) ) NEW_LINE
Evaluation of Expression Tree | Class to represent the nodes of syntax tree ; This function receives a node of the syntax tree and recursively evaluate it ; empty tree ; leaf node ; evaluate left tree ; evaluate right tree ; check which operation to apply ; Driver function to test above problem
class node : NEW_LINE INDENT def __init__ ( self , value ) : NEW_LINE INDENT self . left = None NEW_LINE self . data = value NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def evaluateExpressionTree ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT if root . left is None and root . right is None : NEW_LINE INDENT return int ( root . data ) NEW_LINE DEDENT left_sum = evaluateExpressionTree ( root . left ) NEW_LINE right_sum = evaluateExpressionTree ( root . right ) NEW_LINE if root . data == ' + ' : NEW_LINE INDENT return left_sum + right_sum NEW_LINE DEDENT elif root . data == ' - ' : NEW_LINE INDENT return left_sum - right_sum NEW_LINE DEDENT elif root . data == ' * ' : NEW_LINE INDENT return left_sum * right_sum NEW_LINE DEDENT else : NEW_LINE INDENT return left_sum / right_sum NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = node ( ' + ' ) NEW_LINE root . left = node ( ' * ' ) NEW_LINE root . left . left = node ( '5' ) NEW_LINE root . left . right = node ( '4' ) NEW_LINE root . right = node ( ' - ' ) NEW_LINE root . right . left = node ( '100' ) NEW_LINE root . right . right = node ( '20' ) NEW_LINE print evaluateExpressionTree ( root ) NEW_LINE root = None NEW_LINE root = node ( ' + ' ) NEW_LINE root . left = node ( ' * ' ) NEW_LINE root . left . left = node ( '5' ) NEW_LINE root . left . right = node ( '4' ) NEW_LINE root . right = node ( ' - ' ) NEW_LINE root . right . left = node ( '100' ) NEW_LINE root . right . right = node ( ' / ' ) NEW_LINE root . right . right . left = node ( '20' ) NEW_LINE root . right . right . right = node ( '2' ) NEW_LINE print evaluateExpressionTree ( root ) NEW_LINE DEDENT
Minimum number of distinct elements after removing m items | Function to find distintc id 's ; Store the occurrence of ids ; Store into the list value as key and vice - versa ; Start removing elements from the beginning ; Remove if current value is less than or equal to mi ; Return the remaining size ; Driver code
def distinctIds ( arr , n , mi ) : NEW_LINE INDENT m = { } NEW_LINE v = [ ] NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in m : NEW_LINE m [ arr [ i ] ] += 1 NEW_LINE else : NEW_LINE m [ arr [ i ] ] = 1 NEW_LINE DEDENT for i in m : NEW_LINE INDENT v . append ( [ m [ i ] , i ] ) NEW_LINE DEDENT v . sort ( ) NEW_LINE size = len ( v ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT if ( v [ i ] [ 0 ] <= mi ) : NEW_LINE mi -= v [ i ] [ 0 ] NEW_LINE count += 1 NEW_LINE else : NEW_LINE return size - count NEW_LINE DEDENT return size - count NEW_LINE DEDENT arr = [ 2 , 3 , 1 , 2 , 3 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE m = 3 NEW_LINE print ( distinctIds ( arr , n , m ) ) NEW_LINE
Number of Larger Elements on right side in a string | Python3 program to find counts of right greater characters for every character . ; Driver code
def printGreaterCount ( str ) : NEW_LINE INDENT len__ = len ( str ) NEW_LINE right = [ 0 for i in range ( len__ ) ] NEW_LINE for i in range ( len__ ) : NEW_LINE INDENT for j in range ( i + 1 , len__ , 1 ) : NEW_LINE INDENT if ( str [ i ] < str [ j ] ) : NEW_LINE INDENT right [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( len__ ) : NEW_LINE INDENT print ( right [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " abcd " NEW_LINE printGreaterCount ( str ) NEW_LINE DEDENT