codeparrot/xlcost-text-to-code huggingface dataset:
Tridecagonal Number | Function to find N - th tridecagonal number ; Formula to calculate nth tridecagonal number ; Driver Code
def Tridecagonal_num ( n ) : NEW_LINE INDENT return ( 11 * n * n - 9 * n ) / 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( int ( Tridecagonal_num ( n ) ) ) NEW_LINE n = 10 NEW_LINE print ( int ( Tridecagonal_num ( n ) ) ) NEW_LINE
Number of N digit integers with weight W | Function to find total possible numbers with n digits and weight w ; When Weight of an integer is Positive ; Subtract the weight from 9 ; When weight of an integer is negative ; add the weight to 10 to make it positive ; number of digits in an integer and w as weight ; print the total possible numbers with n digits and weight w
def findNumbers ( n , w ) : NEW_LINE INDENT x = 0 ; NEW_LINE sum = 0 ; NEW_LINE if ( w >= 0 and w <= 8 ) : NEW_LINE INDENT x = 9 - w ; NEW_LINE DEDENT elif ( w >= - 9 and w <= - 1 ) : NEW_LINE INDENT x = 10 + w ; NEW_LINE DEDENT sum = pow ( 10 , n - 2 ) ; NEW_LINE sum = ( x * sum ) ; NEW_LINE return sum ; NEW_LINE DEDENT n = 3 ; NEW_LINE w = 4 ; NEW_LINE print ( findNumbers ( n , w ) ) ; NEW_LINE
Maximum height of triangular arrangement of array values | Python program to find the maximum height of Pyramidal Arrangement of array values ; Just checking whether ith level is possible or not if possible then we must have atleast ( i * ( i + 1 ) ) / 2 elements in the array ; updating the result value each time ; otherwise we have exceeded n value ; Driver Code
def MaximumHeight ( a , n ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT y = ( i * ( i + 1 ) ) / 2 NEW_LINE if ( y < n ) : NEW_LINE INDENT result = i NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 40 , 100 , 20 , 30 ] NEW_LINE n = len ( arr ) NEW_LINE print ( MaximumHeight ( arr , n ) ) NEW_LINE
k | Python3 code to find k - th element in the Odd - Even sequence . ; insert all the odd numbers from 1 to n . ; insert all the even numbers from 1 to n . ; Driver code
def findK ( n , k ) : NEW_LINE INDENT a = list ( ) NEW_LINE i = 1 NEW_LINE while i < n : NEW_LINE INDENT a . append ( i ) NEW_LINE i = i + 2 NEW_LINE DEDENT i = 2 NEW_LINE while i < n : NEW_LINE INDENT a . append ( i ) NEW_LINE i = i + 2 NEW_LINE DEDENT return ( a [ k - 1 ] ) NEW_LINE DEDENT n = 10 NEW_LINE k = 3 NEW_LINE print ( findK ( n , k ) ) NEW_LINE
One line function for factorial of a number | Python3 program to find factorial of given number ; single line to find factorial ; Driver Code
def factorial ( n ) : NEW_LINE INDENT return 1 if ( n == 1 or n == 0 ) else n * factorial ( n - 1 ) ; NEW_LINE DEDENT num = 5 ; NEW_LINE print ( " Factorial ▁ of " , num , " is " , factorial ( num ) ) ; NEW_LINE
Pell Number | calculate nth pell number ; driver function
def pell ( n ) : NEW_LINE INDENT if ( n <= 2 ) : NEW_LINE INDENT return n NEW_LINE DEDENT a = 1 NEW_LINE b = 2 NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT c = 2 * b + a NEW_LINE a = b NEW_LINE b = c NEW_LINE DEDENT return b NEW_LINE DEDENT n = 4 NEW_LINE print ( pell ( n ) ) NEW_LINE
An efficient way to check whether n | Returns true if n - th Fibonacci number is multiple of 10. ; Driver Code
def isMultipleOf10 ( n ) : NEW_LINE INDENT return ( n % 15 == 0 ) NEW_LINE DEDENT n = 30 NEW_LINE if ( isMultipleOf10 ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Find politeness of a number | A function to count all odd prime factors of a given number n ; Eliminate all even prime factor of number of n ; n must be odd at this point , so iterate for only odd numbers till sqrt ( n ) ; if i divides n , then start counting of Odd divisors ; If n odd prime still remains then count it ; Driver program to test above function
def countOddPrimeFactors ( n ) : NEW_LINE INDENT result = 1 ; NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n /= 2 NEW_LINE DEDENT i = 3 NEW_LINE while i * i <= n : NEW_LINE INDENT divCount = 0 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT n /= i NEW_LINE divCount = divCount + 1 NEW_LINE DEDENT result = result * divCount + 1 NEW_LINE i = i + 2 NEW_LINE DEDENT if ( n > 2 ) : NEW_LINE INDENT result = result * 2 NEW_LINE DEDENT return result NEW_LINE DEDENT def politness ( n ) : NEW_LINE INDENT return countOddPrimeFactors ( n ) - 1 ; NEW_LINE DEDENT n = 90 NEW_LINE print " Politness ▁ of ▁ " , n , " ▁ = ▁ " , politness ( n ) NEW_LINE n = 15 NEW_LINE print " Politness ▁ of ▁ " , n , " ▁ = ▁ " , politness ( n ) NEW_LINE
Nearest prime less than given number n | Python3 program to find the nearest prime to n . ; array to store all primes less than 10 ^ 6 ; Utility function of Sieve of Sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x ; This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; eliminate indexes which does not produce primes ; Since 2 is a prime number ; Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; modified binary search to find nearest prime less than N ; base condition is , if we are reaching at left corner or right corner of primes [ ] array then return that corner element because before or after that we don 't have any prime number in  primes array ; now if n is itself a prime so it will be present in primes array and here we have to find nearest prime less than n so we will return primes [ mid - 1 ] ; now if primes [ mid ] < n and primes [ mid + 1 ] > n that means we reached at nearest prime ; Driver Code
import math NEW_LINE MAX = 10000 ; NEW_LINE primes = [ ] ; NEW_LINE def Sieve ( ) : NEW_LINE INDENT n = MAX ; NEW_LINE nNew = int ( math . sqrt ( n ) ) ; NEW_LINE marked = [ 0 ] * ( int ( n / 2 + 500 ) ) ; NEW_LINE for i in range ( 1 , int ( ( nNew - 1 ) / 2 ) + 1 ) : NEW_LINE INDENT for j in range ( ( ( i * ( i + 1 ) ) << 1 ) , ( int ( n / 2 ) + 1 ) , ( 2 * i + 1 ) ) : NEW_LINE INDENT marked [ j ] = 1 ; NEW_LINE DEDENT DEDENT primes . append ( 2 ) ; NEW_LINE for i in range ( 1 , int ( n / 2 ) + 1 ) : NEW_LINE INDENT if ( marked [ i ] == 0 ) : NEW_LINE INDENT primes . append ( 2 * i + 1 ) ; NEW_LINE DEDENT DEDENT DEDENT def binarySearch ( left , right , n ) : NEW_LINE INDENT if ( left <= right ) : NEW_LINE INDENT mid = int ( ( left + right ) / 2 ) ; NEW_LINE if ( mid == 0 or mid == len ( primes ) - 1 ) : NEW_LINE INDENT return primes [ mid ] ; NEW_LINE DEDENT if ( primes [ mid ] == n ) : NEW_LINE INDENT return primes [ mid - 1 ] ; NEW_LINE DEDENT if ( primes [ mid ] < n and primes [ mid + 1 ] > n ) : NEW_LINE INDENT return primes [ mid ] ; NEW_LINE DEDENT if ( n < primes [ mid ] ) : NEW_LINE INDENT return binarySearch ( left , mid - 1 , n ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return binarySearch ( mid + 1 , right , n ) ; NEW_LINE DEDENT DEDENT return 0 ; NEW_LINE DEDENT Sieve ( ) ; NEW_LINE n = 17 ; NEW_LINE print ( binarySearch ( 0 , len ( primes ) - 1 , n ) ) ; NEW_LINE
Program for factorial of a number | Function to find factorial of given number ; Driver Code
def factorial ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return n * factorial ( n - 1 ) NEW_LINE DEDENT num = 5 ; NEW_LINE print ( " Factorial ▁ of " , num , " is " , factorial ( num ) ) NEW_LINE
Turn off the rightmost set bit | Set 2 | Unsets the rightmost set bit of n and returns the result ; Driver Code
def FlipBits ( n ) : NEW_LINE INDENT n -= ( n & ( - n ) ) ; NEW_LINE return n ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 12 ; NEW_LINE print ( " The ▁ number ▁ after ▁ unsetting ▁ the " , end = " " ) ; NEW_LINE print ( " ▁ rightmost ▁ set ▁ bit : ▁ " , FlipBits ( N ) ) ; NEW_LINE DEDENT
Maximum value of XOR among all triplets of an array | function to count maximum XOR value for a triplet ; set is used to avoid repetitions ; store all possible unique XOR value of pairs ; store maximum value ; Driver code
def Maximum_xor_Triplet ( n , a ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT s . add ( a [ i ] ^ a [ j ] ) NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in s : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT ans = max ( ans , i ^ a [ j ] ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 3 , 8 , 15 ] NEW_LINE n = len ( a ) NEW_LINE Maximum_xor_Triplet ( n , a ) NEW_LINE DEDENT
Find missing elements of a range | Python library for binary search ; Print all elements of range [ low , high ] that are not present in arr [ 0. . n - 1 ] ; Do binary search for ' low ' in sorted array and find index of first element which either equal to or greater than low . ; Start from the found index and linearly search every range element x after this index in arr [ ] ; If x doesn 't math with current element  print it ; If x matches , move to next element in arr [ ] ; Move to next element in range [ low , high ] ; Print range elements thar are greater than the last element of sorted array . ; Driver code
from bisect import bisect_left NEW_LINE def printMissing ( arr , n , low , high ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE ptr = bisect_left ( arr , low ) NEW_LINE index = ptr NEW_LINE i = index NEW_LINE x = low NEW_LINE while ( i < n and x <= high ) : NEW_LINE INDENT if ( arr [ i ] != x ) : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT i = i + 1 NEW_LINE DEDENT x = x + 1 NEW_LINE DEDENT while ( x <= high ) : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE x = x + 1 NEW_LINE DEDENT DEDENT arr = [ 1 , 3 , 5 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE low = 1 NEW_LINE high = 10 NEW_LINE printMissing ( arr , n , low , high ) ; NEW_LINE
Find missing elements of a range | Print all elements of range [ low , high ] that are not present in arr [ 0. . n - 1 ] ; Create boolean list of size high - low + 1 , each index i representing wether ( i + low ) th element found or not . ; if ith element of arr is in range low to high then mark corresponding index as true in array ; Traverse through the range and print all elements whose value is false ; Driver Code
def printMissing ( arr , n , low , high ) : NEW_LINE INDENT points_of_range = [ False ] * ( high - low + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( low <= arr [ i ] and arr [ i ] <= high ) : NEW_LINE INDENT points_of_range [ arr [ i ] - low ] = True NEW_LINE DEDENT DEDENT for x in range ( high - low + 1 ) : NEW_LINE INDENT if ( points_of_range [ x ] == False ) : NEW_LINE INDENT print ( low + x , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 3 , 5 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE low , high = 1 , 10 NEW_LINE printMissing ( arr , n , low , high ) NEW_LINE
Find missing elements of a range | Print all elements of range [ low , high ] that are not present in arr [ 0. . n - 1 ] ; Insert all elements of arr [ ] in set ; Traverse through the range and print all missing elements ; Driver Code
def printMissing ( arr , n , low , high ) : NEW_LINE INDENT s = set ( arr ) NEW_LINE for x in range ( low , high + 1 ) : NEW_LINE INDENT if x not in s : NEW_LINE INDENT print ( x , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 3 , 5 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE low , high = 1 , 10 NEW_LINE printMissing ( arr , n , low , high ) NEW_LINE
k | Returns k - th missing element . It returns - 1 if no k is more than number of missing elements . ; insert all elements of given sequence b [ ] . ; Traverse through increasing sequence and keep track of count of missing numbers . ; Driver code
def find ( a , b , k , n1 , n2 ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( n2 ) : NEW_LINE INDENT s . add ( b [ i ] ) NEW_LINE DEDENT missing = 0 NEW_LINE for i in range ( n1 ) : NEW_LINE INDENT if a [ i ] not in s : NEW_LINE INDENT missing += 1 NEW_LINE DEDENT if missing == k : NEW_LINE INDENT return a [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT a = [ 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 15 ] NEW_LINE b = [ 4 , 10 , 6 , 8 , 12 ] NEW_LINE n1 = len ( a ) NEW_LINE n2 = len ( b ) NEW_LINE k = 3 NEW_LINE print ( find ( a , b , k , n1 , n2 ) ) NEW_LINE
Minimize steps to form string S from any random string of length K using a fixed length subsequences | ''Function to find the minimum number of string required to generate the original string ; '' Stores the frequency of each  character of String S ; '' Stores the frequency of each  character of String S ; '' Count unique characters in S ; '' If unique characters is greater  then N, then return -1 ; '' Otherwise ; '' Perform Binary Search ; '' Find the value of mid ; '' Iterate over the range  [0, 26] ; '' If the amount[i] is  greater than 0 ; '' Update the ranges ; '' Find the resultant string ; Generate the subsequence ; ' ' ▁ If ▁ the ▁ length ▁ of ▁ resultant ▁ ▁ string ▁ is ▁ less ▁ than ▁ N ▁ than ▁ ▁ add ▁ a ▁ character ▁ ' a '  ; '' Print the string ; ''Driver code
def findString ( S , N ) : NEW_LINE INDENT amounts = [ 0 ] * 26 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT amounts [ ord ( S [ i ] ) - 97 ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if amounts [ i ] > 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if count > N : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = " " NEW_LINE high = 100001 NEW_LINE low = 0 NEW_LINE while ( high - low ) > 1 : NEW_LINE INDENT total = 0 NEW_LINE mid = ( high + low ) // 2 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if amounts [ i ] > 0 : NEW_LINE INDENT total += ( amounts [ i ] - 1 ) // mid + 1 NEW_LINE DEDENT DEDENT if total <= N : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid NEW_LINE DEDENT DEDENT print ( high , end = " ▁ " ) NEW_LINE total = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if amounts [ i ] > 0 : NEW_LINE INDENT total += ( amounts [ i ] - 1 ) // high + 1 NEW_LINE for j in range ( ( amounts [ i ] - 1 ) // high + 1 ) : NEW_LINE INDENT ans += chr ( i + 97 ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( total , N ) : NEW_LINE INDENT ans += ' a ' NEW_LINE DEDENT ans = ans [ : : - 1 ] NEW_LINE print ( ans ) NEW_LINE DEDENT DEDENT S = " toffee " NEW_LINE K = 4 NEW_LINE findString ( S , K ) NEW_LINE
Find the first repeating element in an array of integers | This function prints the first repeating element in arr [ ] ; Initialize index of first repeating element ; Creates an empty hashset ; Traverse the input array from right to left ; If element is already in hash set , update Min ; Else add element to hash set ; Print the result ; Driver Code
def printFirstRepeating ( arr , n ) : NEW_LINE INDENT Min = - 1 NEW_LINE myset = dict ( ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if arr [ i ] in myset . keys ( ) : NEW_LINE INDENT Min = i NEW_LINE DEDENT else : NEW_LINE INDENT myset [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT if ( Min != - 1 ) : NEW_LINE INDENT print ( " The ▁ first ▁ repeating ▁ element ▁ is " , arr [ Min ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ are ▁ no ▁ repeating ▁ elements " ) NEW_LINE DEDENT DEDENT arr = [ 10 , 5 , 3 , 4 , 3 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE printFirstRepeating ( arr , n ) NEW_LINE
Find the first repeating element in an array of integers | This function prints the first repeating element in arr [ ] ; This will set k = 1 , if any repeating element found ; max = maximum from ( all elements & n ) ; Array a is for storing 1 st time occurence of element initialized by 0 ; Store 1 in array b if element is duplicate initialized by 0 ; Duplicate element found ; Storing 1 st occurence of arr [ i ] ; Trace array a & find repeating element with min index ; Driver code
def printFirstRepeating ( arr , n ) : NEW_LINE INDENT k = 0 NEW_LINE max = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( max < arr [ i ] ) : NEW_LINE INDENT max = arr [ i ] NEW_LINE DEDENT DEDENT a = [ 0 for i in range ( max + 1 ) ] NEW_LINE b = [ 0 for i in range ( max + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ arr [ i ] ] ) : NEW_LINE INDENT b [ arr [ i ] ] = 1 NEW_LINE k = 1 NEW_LINE continue NEW_LINE DEDENT else : NEW_LINE INDENT a [ arr [ i ] ] = i NEW_LINE DEDENT DEDENT if ( k == 0 ) : NEW_LINE INDENT print ( " No ▁ repeating ▁ element ▁ found " ) NEW_LINE DEDENT else : NEW_LINE INDENT min = max + 1 NEW_LINE for i in range ( max + 1 ) : NEW_LINE INDENT if ( a [ i ] and ( min > ( a [ i ] ) ) and b [ i ] ) : NEW_LINE INDENT min = a [ i ] NEW_LINE DEDENT DEDENT print ( arr [ min ] ) NEW_LINE DEDENT DEDENT arr = [ 10 , 5 , 3 , 4 , 3 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE printFirstRepeating ( arr , n ) NEW_LINE
k | Returns k - th distinct element in arr . ; Check if current element is present somewhere else . ; If element is unique ; Driver Code
def printKDistinct ( arr , n , k ) : NEW_LINE INDENT dist_count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT j = 0 NEW_LINE while j < n : NEW_LINE INDENT if ( i != j and arr [ j ] == arr [ i ] ) : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( j == n ) : NEW_LINE INDENT dist_count += 1 NEW_LINE DEDENT if ( dist_count == k ) : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT ar = [ 1 , 2 , 1 , 3 , 4 , 2 ] NEW_LINE n = len ( ar ) NEW_LINE k = 2 NEW_LINE print ( printKDistinct ( ar , n , k ) ) NEW_LINE
Count subarrays having an equal count of 0 s and 1 s segregated | ''Function to count subarrays having equal count of 0s and 1s with all 0s and all 1s grouped together ; '' Stores the count ; '' Initialize cur with first element ; '' If the next element is same  as the current element ; '' Increment count ; '' Update curr ; '' Iterate over the array count ; '' Consider the minimum ;  ; ''Given arr[] ; ''Function Call
def countSubarrays ( A ) : NEW_LINE INDENT res = 0 NEW_LINE curr , cnt = A [ 0 ] , [ 1 ] NEW_LINE for c in A [ 1 : ] : NEW_LINE INDENT if c == curr : NEW_LINE INDENT cnt [ - 1 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT curr = c NEW_LINE DEDENT cnt . append ( 1 ) NEW_LINE DEDENT for i in range ( 1 , len ( cnt ) ) : NEW_LINE INDENT res += min ( cnt [ i - 1 ] , cnt [ i ] ) NEW_LINE DEDENT print ( res - 1 ) NEW_LINE DEDENT / * Driver code * / NEW_LINE A = [ 1 , 1 , 0 , 0 , 1 , 0 ] NEW_LINE countSubarrays ( A ) NEW_LINE
Check if a Binary Tree is an Even | ''Tree node ; ''Function to return new tree node ; ''Function to check if the tree is even-odd tree ; '' Stores nodes of each level ; '' Store the current level  of the binary tree ; '' Traverse until the  queue is empty ; '' Stores the number of nodes  present in the current level ; '' Check if the level  is even or odd ; '' Add the nodes of the next  level into the queue ; '' Increment the level count ; ''Driver code ; '' Construct a Binary Tree ; '' Check if the binary tree  is even-odd tree or not
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . left = None NEW_LINE self . right = None NEW_LINE self . val = data NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT temp = Node ( data ) NEW_LINE return temp NEW_LINE DEDENT def isEvenOddBinaryTree ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return True NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE level = 0 NEW_LINE while ( len ( q ) != 0 ) : NEW_LINE INDENT size = len ( q ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT node = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( level % 2 == 0 ) : NEW_LINE INDENT if ( node . val % 2 == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT elif ( level % 2 == 1 ) : NEW_LINE INDENT if ( node . val % 2 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if ( node . left != None ) : NEW_LINE INDENT q . append ( node . left ) NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT q . append ( node . right ) NEW_LINE DEDENT DEDENT level += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = None NEW_LINE root = newNode ( 2 ) NEW_LINE root . left = newNode ( 3 ) NEW_LINE root . right = newNode ( 9 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 10 ) NEW_LINE root . right . right = newNode ( 6 ) NEW_LINE if ( isEvenOddBinaryTree ( root ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Maximize minimum distance between repetitions from any permutation of the given Array | Python3 program to implement the above approach ; Size of the array ; Stores the frequency of array elements ; Find the highest frequency in the array ; Increase count of max frequent element ; If no repetition is present ; Find the maximum distance ; Return the max distance ; Driver Code
import sys NEW_LINE def findMaxLen ( a ) : NEW_LINE INDENT n = len ( a ) NEW_LINE freq = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ a [ i ] ] += 1 NEW_LINE DEDENT maxFreqElement = - sys . maxsize - 1 NEW_LINE maxFreqCount = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( freq [ i ] > maxFreqElement ) : NEW_LINE INDENT maxFreqElement = freq [ i ] NEW_LINE maxFreqCount = 1 NEW_LINE DEDENT elif ( freq [ i ] == maxFreqElement ) : NEW_LINE INDENT maxFreqCount += 1 NEW_LINE DEDENT DEDENT if ( maxFreqElement == 1 ) : NEW_LINE INDENT ans = 0 NEW_LINE DEDENT else : NEW_LINE INDENT ans = ( ( n - maxFreqCount ) // ( maxFreqElement - 1 ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT a = [ 1 , 2 , 1 , 2 ] NEW_LINE print ( findMaxLen ( a ) ) NEW_LINE