codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs from two sorted arrays whose sum is equal to a given value x | function to search ' value ' in the given array ' arr [ ] ' it uses binary search technique as ' arr [ ] ' is sorted ; value found ; value not found ; function to count all pairs from both the sorted arrays whose sum is equal to a given value ; for each arr1 [ i ] ; check if the ' value ' is present in 'arr2[]   ; required count of pairs ; Driver Code
def isPresent ( arr , low , high , value ) : NEW_LINE INDENT while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( arr [ mid ] == value ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( arr [ mid ] > value ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def countPairs ( arr1 , arr2 , m , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT value = x - arr1 [ i ] NEW_LINE if ( isPresent ( arr2 , 0 , n - 1 , value ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 1 , 3 , 5 , 7 ] NEW_LINE arr2 = [ 2 , 3 , 5 , 8 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE x = 10 NEW_LINE print ( " Count ▁ = ▁ " , countPairs ( arr1 , arr2 , m , n , x ) ) NEW_LINE DEDENT
Count pairs from two sorted arrays whose sum is equal to a given value x | function to count all pairs from both the sorted arrays whose sum is equal to a given value ; insert all the elements of 1 st array in the hash table ( unordered_set ' us ' ) ; or each element of 'arr2[] ; find ( x - arr2 [ j ] ) in 'us   ; required count of pairs ; Driver code
def countPairs ( arr1 , arr2 , m , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE us = set ( ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT us . add ( arr1 [ i ] ) NEW_LINE DEDENT for j in range ( n ) : NEW_LINE INDENT if x - arr2 [ j ] in us : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr1 = [ 1 , 3 , 5 , 7 ] NEW_LINE arr2 = [ 2 , 3 , 5 , 8 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE x = 10 NEW_LINE print ( " Count ▁ = " , countPairs ( arr1 , arr2 , m , n , x ) ) NEW_LINE
Count pairs from two sorted arrays whose sum is equal to a given value x | function to count all pairs from both the sorted arrays whose sum is equal to a given value ; traverse ' arr1 [ ] ' from left to right traverse ' arr2 [ ] ' from right to left ; if this sum is equal to ' x ' , then increment ' l ' , decrement ' r ' and increment 'count   ; if this sum is less than x , then increment l ; else decrement 'r   ; required count of pairs ; Driver Code
def countPairs ( arr1 , arr2 , m , n , x ) : NEW_LINE INDENT count , l , r = 0 , 0 , n - 1 NEW_LINE while ( l < m and r >= 0 ) : NEW_LINE INDENT if ( ( arr1 [ l ] + arr2 [ r ] ) == x ) : NEW_LINE INDENT l += 1 NEW_LINE r -= 1 NEW_LINE count += 1 NEW_LINE DEDENT elif ( ( arr1 [ l ] + arr2 [ r ] ) < x ) : NEW_LINE INDENT l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT r -= 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 1 , 3 , 5 , 7 ] NEW_LINE arr2 = [ 2 , 3 , 5 , 8 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE x = 10 NEW_LINE print ( " Count ▁ = " , countPairs ( arr1 , arr2 , m , n , x ) ) NEW_LINE DEDENT
Count pairs from two linked lists whose sum is equal to a given value | A Linked list node ; function to count all pairs from both the linked lists whose sum is equal to a given value ; traverse the 1 st linked list ; for each node of 1 st list traverse the 2 nd list ; if sum of pair is equal to ' x ' increment count ; required count of pairs ; Driver program to test above
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . next = head_ref NEW_LINE head_ref = new_node NEW_LINE return head_ref NEW_LINE DEDENT def countPairs ( head1 , head2 , x ) : NEW_LINE INDENT count = 0 NEW_LINE p1 = head1 NEW_LINE while ( p1 != None ) : NEW_LINE INDENT p2 = head2 NEW_LINE while ( p2 != None ) : NEW_LINE INDENT if ( ( p1 . data + p2 . data ) == x ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT p2 = p2 . next NEW_LINE DEDENT p1 = p1 . next NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head1 = None NEW_LINE head2 = None NEW_LINE head1 = push ( head1 , 7 ) NEW_LINE head1 = push ( head1 , 5 ) NEW_LINE head1 = push ( head1 , 1 ) NEW_LINE head1 = push ( head1 , 3 ) NEW_LINE head2 = push ( head2 , 3 ) NEW_LINE head2 = push ( head2 , 5 ) NEW_LINE head2 = push ( head2 , 2 ) NEW_LINE head2 = push ( head2 , 8 ) NEW_LINE x = 10 NEW_LINE print ( " Count ▁ = ▁ " , countPairs ( head1 , head2 , x ) ) NEW_LINE DEDENT
Count quadruples from four sorted arrays whose sum is equal to a given value x | count pairs from the two sorted array whose sum is equal to the given ' value ' ; traverse ' arr1 [ ] ' from left to right traverse ' arr2 [ ] ' from right to left ; if the ' sum ' is equal to ' value ' , then increment ' l ' , decrement ' r ' and increment ' count ' ; if the ' sum ' is greater than ' value ' , then decrement r ; else increment l ; required count of pairs print ( count ) ; function to count all quadruples from four sorted arrays whose sum is equal to a given value x ; generate all pairs from arr1 [ ] and arr2 [ ] ; calculate the sum of elements in the pair so generated ; count pairs in the 3 rd and 4 th array having value ' x - p _ sum ' and then accumulate it to ' count ' ; required count of quadruples ; four sorted arrays each of size ' n '
def countPairs ( arr1 , arr2 , n , value ) : NEW_LINE INDENT count = 0 NEW_LINE l = 0 NEW_LINE r = n - 1 NEW_LINE while ( l < n and r >= 0 ) : NEW_LINE INDENT sum = arr1 [ l ] + arr2 [ r ] NEW_LINE if ( sum == value ) : NEW_LINE INDENT l += 1 NEW_LINE r -= 1 NEW_LINE count += 1 NEW_LINE DEDENT elif ( sum > value ) : NEW_LINE INDENT r -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT l += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT p_sum = arr1 [ i ] + arr2 [ j ] NEW_LINE count += int ( countPairs ( arr3 , arr4 , n , x - p_sum ) ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr1 = [ 1 , 4 , 5 , 6 ] NEW_LINE arr2 = [ 2 , 3 , 7 , 8 ] NEW_LINE arr3 = [ 1 , 4 , 6 , 10 ] NEW_LINE arr4 = [ 2 , 4 , 7 , 8 ] NEW_LINE n = len ( arr1 ) NEW_LINE x = 30 NEW_LINE print ( " Count ▁ = ▁ " , countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ) NEW_LINE
Count quadruples from four sorted arrays whose sum is equal to a given value x | function to count all quadruples from four sorted arrays whose sum is equal to a given value x ; unordered_map ' um ' implemented as hash table for < sum , frequency > tuples ; count frequency of each sum obtained from the pairs of arr1 [ ] and arr2 [ ] and store them in 'um   ; generate pair from arr3 [ ] and arr4 [ ] ; calculate the sum of elements in the pair so generated ; if ' x - p _ sum ' is present in ' um ' then add frequency of ' x - p _ sum ' to ' count ' ; required count of quadruples ; four sorted arrays each of size n
def countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE m = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( arr1 [ i ] + arr2 [ j ] ) in m : NEW_LINE INDENT m [ arr1 [ i ] + arr2 [ j ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ arr1 [ i ] + arr2 [ j ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT for k in range ( n ) : NEW_LINE INDENT for l in range ( n ) : NEW_LINE INDENT p_sum = arr3 [ k ] + arr4 [ l ] NEW_LINE if ( x - p_sum ) in m : NEW_LINE INDENT count += m [ x - p_sum ] NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr1 = [ 1 , 4 , 5 , 6 ] NEW_LINE arr2 = [ 2 , 3 , 7 , 8 ] NEW_LINE arr3 = [ 1 , 4 , 6 , 10 ] NEW_LINE arr4 = [ 2 , 4 , 7 , 8 ] NEW_LINE n = len ( arr1 ) NEW_LINE x = 30 NEW_LINE print ( " Count ▁ = " , countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ) NEW_LINE
How to learn Pattern printing easily ? | Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 ; NEW_LINE print ( " Value ▁ of ▁ N : ▁ " , N ) ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT min = i if i < j else j ; NEW_LINE print ( N - min + 1 , end = " " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT DEDENT
Number of subarrays having sum exactly equal to k | Python3 program for the above approach ; Calculate all subarrays ; Calculate required sum ; Check if sum is equal to required sum
arr = [ 10 , 2 , - 2 , - 20 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE k = - 10 NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT summ += arr [ j ] NEW_LINE if summ == k : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT print ( res ) NEW_LINE
Number of subarrays having sum exactly equal to k | Python3 program to find the number of subarrays with sum exactly equal to k . ; Function to find number of subarrays with sum exactly equal to k . ; Dictionary to store number of subarrays starting from index zero having particular value of sum . ; Sum of elements so far . ; Add current element to sum so far . ; If currsum is equal to desired sum , then a new subarray is found . So increase count of subarrays . ; currsum exceeds given sum by currsum - sum . Find number of subarrays having this sum and exclude those subarrays from currsum by increasing count by same amount . ; Add currsum value to count of different values of sum . ; Driver Code
from collections import defaultdict NEW_LINE def findSubarraySum ( arr , n , Sum ) : NEW_LINE INDENT prevSum = defaultdict ( lambda : 0 ) NEW_LINE res = 0 NEW_LINE currsum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT currsum += arr [ i ] NEW_LINE if currsum == Sum : NEW_LINE INDENT res += 1 NEW_LINE DEDENT if ( currsum - Sum ) in prevSum : NEW_LINE INDENT res += prevSum [ currsum - Sum ] NEW_LINE DEDENT prevSum [ currsum ] += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 2 , - 2 , - 20 , 10 ] NEW_LINE Sum = - 10 NEW_LINE n = len ( arr ) NEW_LINE print ( findSubarraySum ( arr , n , Sum ) ) NEW_LINE DEDENT
Count pairs whose products exist in array | Returns count of pairs whose product exists in arr [ ] ; find product in an array ; if product found increment counter ; return Count of all pair whose product exist in array ; Driver program
def countPairs ( arr , n ) : NEW_LINE INDENT result = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT product = arr [ i ] * arr [ j ] ; NEW_LINE for k in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ k ] == product ) : NEW_LINE INDENT result = result + 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return result ; NEW_LINE DEDENT arr = [ 6 , 2 , 4 , 12 , 5 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countPairs ( arr , n ) ) ; NEW_LINE
Count pairs whose products exist in array | Returns count of pairs whose product exists in arr [ ] ; Create an empty hash - set that store all array element ; Insert all array element into set ; Generate all pairs and check is exist in ' Hash ' or not ; if product exists in set then we increment count by 1 ; return count of pairs whose product exist in array ; Driver Code
def countPairs ( arr , n ) : NEW_LINE INDENT result = 0 NEW_LINE Hash = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT Hash . add ( arr [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT product = arr [ i ] * arr [ j ] NEW_LINE if product in ( Hash ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 6 , 2 , 4 , 12 , 5 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE DEDENT
Master Theorem For Subtract and Conquer Recurrences | ''Python3 code for the above approach ; ''Driver code
def fib ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return n NEW_LINE DEDENT return fib ( n - 1 ) + fib ( n - 2 ) NEW_LINE DEDENT n = 9 NEW_LINE print ( fib ( n ) ) NEW_LINE
Tail Recursion | ''A tail recursive function to calculate factorial ; ''Driver program to test  above function
def fact ( n , a = 1 ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return fact ( n - 1 , n * a ) NEW_LINE DEDENT print ( fact ( 5 ) ) NEW_LINE
Minimum and maximum count of elements at D distance from arr [ i ] in either direction | ''Function to find the minimum and maximum number of points included in a range of distance D ; '' Stores the minimum and maximum  number of points that lies  over the distance of D ; '' Iterate the array ; '' Count of elements included  to left of point at index i ; '' Update the minimum number  of points ; '' Update the maximum number  of points ; '' Count of elements included  to right of point at index i ; '' Update the minimum number  of points ; '' Update the maximum number  of points ; '' Return the array ; ''Function to perform the Binary Search to the left of arr[i] over the given range ; '' Base Case ; '' Binary Search for index to left ; '' update index ; '' Return the number of elements  by subtracting indices ; ''Function to perform the Binary Search to the right of arr[i] over the given range ; '' Base Case ; '' Binary Search for index to right ; '' Update the index ; '' Return the number of elements  by subtracting indices ; ''Driver Code ; ''Function Call
def minMaxRange ( arr , D , N ) : NEW_LINE INDENT Max = 1 NEW_LINE Min = N NEW_LINE for i in range ( N ) : NEW_LINE INDENT dist = leftSearch ( arr , arr [ i ] - D , i ) NEW_LINE Min = min ( Min , dist ) NEW_LINE Max = max ( Max , dist ) NEW_LINE dist = rightSearch ( arr , arr [ i ] + D , i ) NEW_LINE Min = min ( Min , dist ) NEW_LINE Max = max ( Max , dist ) NEW_LINE DEDENT return [ Min , Max ] NEW_LINE DEDENT def leftSearch ( arr , val , i ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT left = 0 NEW_LINE right = i - 1 NEW_LINE ind = - 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE if ( arr [ mid ] < val ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid - 1 NEW_LINE ind = mid NEW_LINE DEDENT DEDENT return i - ind + 1 if ind != - 1 else 1 NEW_LINE DEDENT def rightSearch ( arr , val , i ) : NEW_LINE INDENT if ( i == len ( arr ) - 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT left = i + 1 NEW_LINE right = len ( arr ) - 1 NEW_LINE ind = - 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE if ( arr [ mid ] > val ) : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = mid + 1 NEW_LINE ind = mid NEW_LINE DEDENT DEDENT return ind - i + 1 if ind != - 1 else 1 NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 9 , 14 ] NEW_LINE N = len ( arr ) NEW_LINE D = 4 NEW_LINE minMax = minMaxRange ( arr , D , N ) NEW_LINE print ( f " { minMax [ 0 ] } ▁ { minMax [ 1 ] } " ) NEW_LINE
Given two unsorted arrays , find all pairs whose sum is x | Function to print all pairs in both arrays whose sum is equal to given value x ; Driver code
def findPairs ( arr1 , arr2 , n , m , x ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , m ) : NEW_LINE INDENT if ( arr1 [ i ] + arr2 [ j ] == x ) : NEW_LINE INDENT print ( arr1 [ i ] , arr2 [ j ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT arr1 = [ 1 , 2 , 3 , 7 , 5 , 4 ] NEW_LINE arr2 = [ 0 , 7 , 4 , 3 , 2 , 1 ] NEW_LINE n = len ( arr1 ) NEW_LINE m = len ( arr2 ) NEW_LINE x = 8 NEW_LINE findPairs ( arr1 , arr2 , n , m , x ) NEW_LINE
Smallest possible integer K such that ceil of each Array element when divided by K is at most M | ''python program for the above approach ; ''Function to check if the sum of ceil values of the arr[] for the K value is at most M or not ; '' Stores the sum of ceil values ; '' Update the sum ; '' Return true if sum is less than  or equal to M, false otherwise ; ''Function to find the smallest possible integer K such that ceil(arr[0]/K) + ceil(arr[1]/K) +....+ ceil(arr[N-1]/K) is less than or equal to M ; '' Stores the low value ; '' Stores the high value ; '' Stores the middle value ; '' Update the mid value ; '' Check if the mid value is K ; '' Update the low value ; '' Update the high value ; '' Check if low is K or high is K  and return it ; ''Driver Code
import math NEW_LINE def isvalid ( arr , K , N , M ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT sum += math . ceil ( arr [ i ] * 1.0 / K ) NEW_LINE DEDENT return sum <= M NEW_LINE DEDENT def smallestValueForK ( arr , N , M ) : NEW_LINE INDENT low = 1 NEW_LINE high = arr [ 0 ] NEW_LINE for i in range ( 1 , len ( arr ) ) : NEW_LINE INDENT high = max ( high , arr [ i ] ) NEW_LINE DEDENT mid = 0 NEW_LINE while ( high - low > 1 ) : NEW_LINE INDENT mid = ( high + low ) // 2 NEW_LINE if ( not isvalid ( arr , mid , N , M ) ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid NEW_LINE DEDENT DEDENT if ( isvalid ( arr , low , N , M ) ) : NEW_LINE INDENT return low NEW_LINE DEDENT else : NEW_LINE INDENT return high NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 3 , 2 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE M = 5 NEW_LINE print ( smallestValueForK ( arr , N , M ) ) NEW_LINE DEDENT
Given two unsorted arrays , find all pairs whose sum is x | Function to find all pairs in both arrays whose sum is equal to given value x ; Insert all elements of first array in a hash ; Subtract sum from second array elements one by one and check it 's present in  array first or not ; Driver code
def findPairs ( arr1 , arr2 , n , m , x ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT s . add ( arr1 [ i ] ) NEW_LINE DEDENT for j in range ( 0 , m ) : NEW_LINE INDENT if ( ( x - arr2 [ j ] ) in s ) : NEW_LINE INDENT print ( ( x - arr2 [ j ] ) , ' ' , arr2 [ j ] ) NEW_LINE DEDENT DEDENT DEDENT arr1 = [ 1 , 0 , - 4 , 7 , 6 , 4 ] NEW_LINE arr2 = [ 0 , 2 , 4 , - 3 , 2 , 1 ] NEW_LINE x = 8 NEW_LINE n = len ( arr1 ) NEW_LINE m = len ( arr2 ) NEW_LINE findPairs ( arr1 , arr2 , n , m , x ) NEW_LINE
Check if a given Binary Tree is height balanced like a Red | Helper function that allocates a new node with the given data and None left and right poers . ; Returns returns tree if the Binary tree is balanced like a Red - Black tree . This function also sets value in maxh and minh ( passed by reference ) . maxh and minh are set as maximum and minimum heights of root . ; Base case ; To store max and min heights of left subtree ; To store max and min heights of right subtree ; Check if left subtree is balanced , also set lmxh and lmnh ; Check if right subtree is balanced , also set rmxh and rmnh ; Set the max and min heights of this node for the parent call ; See if this node is balanced ; A wrapper over isBalancedUtil ( ) ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isBalancedUtil ( root , maxh , minh ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT maxh = minh = 0 NEW_LINE return True NEW_LINE DEDENT lmxh = 0 NEW_LINE lmnh = 0 NEW_LINE rmxh , rmnh = 0 , 0 NEW_LINE if ( isBalancedUtil ( root . left , lmxh , lmnh ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( isBalancedUtil ( root . right , rmxh , rmnh ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT maxh = max ( lmxh , rmxh ) + 1 NEW_LINE minh = min ( lmnh , rmnh ) + 1 NEW_LINE if ( maxh <= 2 * minh ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def isBalanced ( root ) : NEW_LINE INDENT maxh , minh = 0 , 0 NEW_LINE return isBalancedUtil ( root , maxh , minh ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 5 ) NEW_LINE root . right = newNode ( 100 ) NEW_LINE root . right . left = newNode ( 50 ) NEW_LINE root . right . right = newNode ( 150 ) NEW_LINE root . right . left . left = newNode ( 40 ) NEW_LINE if ( isBalanced ( root ) ) : NEW_LINE INDENT print ( " Balanced " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Balanced " ) NEW_LINE DEDENT DEDENT
Maximum Fixed Point ( Value equal to index ) in a given Array | ''Python implementation of the above approach Function to find the maximum index i such that arr[i] is equal to i ; '' Traversing the array from  backwards ; '' If arr[i] is equal to i ; '' If there is no such index ; Given Input ; ''Function Call
def findLargestIndex ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == i ) : NEW_LINE INDENT print ( i ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( - 1 ) NEW_LINE DEDENT arr = [ - 10 , - 5 , 0 , 3 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE findLargestIndex ( arr , n ) NEW_LINE
Cumulative frequency of count of each element in an unsorted array | Function to print the cumulative frequency according to the order given ; Insert elements and their frequencies in hash map . ; traverse in the array ; add the frequencies ; if the element has not been visited previously ; mark the hash 0 as the element 's cumulative  frequency has been printed ; Driver Code
def countFreq ( a , n ) : NEW_LINE INDENT hm = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT hm [ a [ i ] ] = hm . get ( a [ i ] , 0 ) + 1 NEW_LINE DEDENT cumul = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT cumul += hm [ a [ i ] ] NEW_LINE if ( hm [ a [ i ] ] > 0 ) : NEW_LINE INDENT print ( a [ i ] , " - > " , cumul ) NEW_LINE DEDENT hm [ a [ i ] ] = 0 NEW_LINE DEDENT DEDENT a = [ 1 , 3 , 2 , 4 , 2 , 1 ] NEW_LINE n = len ( a ) NEW_LINE countFreq ( a , n ) NEW_LINE
Find pairs in array whose sums already exist in array | Function to find pair whose sum exists in arr [ ] ; Driver code
def findPair ( arr , n ) : NEW_LINE INDENT found = False NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT for k in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] + arr [ j ] == arr [ k ] ) : NEW_LINE INDENT print ( arr [ i ] , arr [ j ] ) NEW_LINE found = True NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( found == False ) : NEW_LINE INDENT print ( " Not ▁ exist " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 4 , 8 , 13 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE findPair ( arr , n ) NEW_LINE DEDENT