codeparrot/xlcost-text-to-code huggingface dataset:
Count subarrays for every array element in which they are the minimum | Set 2 | Function to calculate total number of sub - arrays for each element where that element is occurring as the minimum element ; Map for storing the number of sub - arrays for each element ; Traverse over all possible subarrays ; Minimum in each subarray ; Print result ; Driver Code ; Function Call
def minSubarray ( arr , N ) : NEW_LINE INDENT m = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT mini = 10 ** 9 NEW_LINE for j in range ( i , N ) : NEW_LINE INDENT mini = min ( mini , arr [ j ] ) NEW_LINE m [ mini ] = m . get ( mini , 0 ) + 1 NEW_LINE DEDENT DEDENT for i in arr : NEW_LINE INDENT print ( m [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE minSubarray ( arr , N ) NEW_LINE DEDENT
Count subarrays for every array element in which they are the minimum | Set 2 | Function to count subarrays for each element where it is the minimum ; For the length of strictly larger numbers on the left of A [ i ] ; Storing x in result [ i ] ; For the length of strictly larger numbers on the right of A [ i ] ; Store x * y in result array ; Print the result ; Driver Code ; Function Call
def minSubarray ( arr , N ) : NEW_LINE INDENT result = [ 0 ] * N NEW_LINE l = [ ] NEW_LINE r = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT count = 1 ; NEW_LINE while ( len ( l ) != 0 and l [ - 1 ] [ 0 ] > arr [ i ] ) : NEW_LINE INDENT count += l [ - 1 ] [ 1 ] NEW_LINE l . pop ( ) NEW_LINE DEDENT l . append ( [ arr [ i ] , count ] ) NEW_LINE result [ i ] = count ; NEW_LINE DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT count = 1 ; NEW_LINE while ( len ( r ) != 0 and r [ - 1 ] [ 0 ] >= arr [ i ] ) : NEW_LINE INDENT count += r [ - 1 ] [ 1 ] NEW_LINE r . pop ( ) ; NEW_LINE DEDENT r . append ( [ arr [ i ] , count ] ) ; NEW_LINE result [ i ] *= count NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( result [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE minSubarray ( arr , N ) NEW_LINE DEDENT
Count Full Prime numbers in a given range | Function to check if a number is prime or not ; If a divisor of n exists ; Function to check if a number is Full Prime or not ; If n is not a prime ; Otherwise ; Extract digit ; If any digit of n is non - prime ; Function to prcount of Full Primes in a range [ L , R ] ; Stores count of full primes ; Check if i is full prime ; Driver Code ; Stores count of full primes
def isPrime ( num ) : NEW_LINE INDENT if ( num <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , num + 1 ) : NEW_LINE INDENT if i * i > num : NEW_LINE INDENT break NEW_LINE DEDENT if ( num % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isFulPrime ( n ) : NEW_LINE INDENT if ( not isPrime ( n ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT while ( n > 0 ) : NEW_LINE INDENT rem = n % 10 NEW_LINE if ( not ( rem == 2 or rem == 3 or rem == 5 or rem == 7 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def countFulPrime ( L , R ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( ( i % 2 ) != 0 and isFulPrime ( i ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 1 NEW_LINE R = 100 NEW_LINE ans = 0 NEW_LINE if ( L < 3 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT print ( ans + countFulPrime ( L , R ) ) NEW_LINE DEDENT
Print alternate elements of an array | Function to print Alternate elements of the given array ; Print elements at odd positions ; If currIndex stores even index or odd position ; Driver Code
def printAlter ( arr , N ) : NEW_LINE INDENT for currIndex in range ( 0 , N ) : NEW_LINE INDENT if ( currIndex % 2 == 0 ) : NEW_LINE INDENT print ( arr [ currIndex ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE printAlter ( arr , N ) NEW_LINE DEDENT
Minimize deletions in a Binary String to remove all subsequences of the form "0101" | Function to find minimum characters to be removed such that no subsequence of the form "0101" exists in the string ; Stores the partial sums ; Calculate partial sums ; Setting endpoints and deleting characters indices . ; Return count of deleted characters ; Driver Code
def findmin ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE maximum = 0 NEW_LINE incr = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT incr [ i + 1 ] = incr [ i ] NEW_LINE if ( s [ i ] == '0' ) : NEW_LINE INDENT incr [ i + 1 ] = incr [ i + 1 ] + 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , n + 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT maximum = max ( maximum , incr [ i ] + j - i + 1 - ( incr [ j + 1 ] - incr [ i ] ) + incr [ n ] - incr [ j + 1 ] ) NEW_LINE DEDENT DEDENT return n - maximum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "0110100110" NEW_LINE minimum = findmin ( S ) NEW_LINE print ( minimum ) NEW_LINE DEDENT
Length of longest subarray with product equal to a power of 2 | Function to check whether a number is power of 2 or not ; Function to find maximum length subarray having product of element as a perfect power of 2 ; Stores current subarray length ; Stores maximum subarray length ; Traverse the given array ; If arr [ i ] is power of 2 ; Increment max_length ; Update max_len_subarray ; Otherwise ; Prthe maximum length ; Driver Code ; Given arr ; Function Call
def isPower ( x ) : NEW_LINE INDENT if ( x != 0 and ( x & ( x - 1 ) ) == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT def maximumlength ( arr , N ) : NEW_LINE INDENT max_length = 0 ; NEW_LINE max_len_subarray = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( isPower ( arr [ i ] ) ) : NEW_LINE INDENT max_length += 1 ; NEW_LINE max_len_subarray = max ( max_length , max_len_subarray ) ; NEW_LINE DEDENT else : NEW_LINE INDENT max_length = 0 ; NEW_LINE DEDENT DEDENT print ( max_len_subarray ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 5 , 4 , 6 , 8 , 8 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE maximumlength ( arr , N ) ; NEW_LINE DEDENT
Length of smallest subarray to be removed such that the remaining array is sorted | Python3 program for the above approach ; Find the length of the shortest subarray ; To store the result ; Calculate the possible length of the sorted subarray from left ; Array is sorted ; Calculate the possible length of the sorted subarray from left ; Update the result ; Calculate the possible length in the middle we can delete and update the result ; Update the result ; Return the result ; Driver Code ; Function Call
import sys NEW_LINE def findLengthOfShortestSubarray ( arr ) : NEW_LINE INDENT minlength = sys . maxsize NEW_LINE left = 0 NEW_LINE right = len ( arr ) - 1 NEW_LINE while left < right and arr [ left + 1 ] >= arr [ left ] : NEW_LINE INDENT left += 1 NEW_LINE DEDENT if left == len ( arr ) - 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT while right > left and arr [ right - 1 ] <= arr [ right ] : NEW_LINE INDENT right -= 1 NEW_LINE DEDENT minlength = min ( len ( arr ) - left - 1 , right ) NEW_LINE j = right NEW_LINE for i in range ( left + 1 ) : NEW_LINE INDENT if arr [ i ] <= arr [ j ] : NEW_LINE INDENT minlength = min ( minlength , j - i - 1 ) NEW_LINE DEDENT elif j < len ( arr ) - 1 : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return minlength NEW_LINE DEDENT arr = [ 6 , 3 , 10 , 11 , 15 , 20 , 13 , 3 , 18 , 12 ] NEW_LINE print ( findLengthOfShortestSubarray ( arr ) ) NEW_LINE
Smallest number to be subtracted to convert given number to a palindrome | Function to evaluate minimum subtraction required to make a number palindrome ; Counts number of subtractions required ; Run a loop till N >= 0 ; Store the current number ; Store reverse of current number ; Reverse the number ; Check if N is palindrome ; Increment the counter ; Reduce the number by 1 ; Print the result ; Driver Code ; Function call
def minSub ( N ) : NEW_LINE INDENT count = 0 NEW_LINE while ( N >= 0 ) : NEW_LINE INDENT num = N NEW_LINE rev = 0 NEW_LINE while ( num != 0 ) : NEW_LINE INDENT digit = num % 10 NEW_LINE rev = ( rev * 10 ) + digit NEW_LINE num = num // 10 NEW_LINE DEDENT if ( N == rev ) : NEW_LINE INDENT break NEW_LINE DEDENT count += 1 NEW_LINE N -= 1 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3456 NEW_LINE minSub ( N ) NEW_LINE DEDENT
Count subarrays of atleast size 3 forming a Geometric Progression ( GP ) | Function to count all the subarrays of size at least 3 forming GP ; If array size is less than 3 ; Stores the count of subarray ; Stores the count of subarray for each iteration ; Traverse the array ; Check if L [ i ] forms GP ; Otherwise , update count to 0 ; Update the final count ; Return the final count ; Driver Code ; Given array arr [ ] ; Function Call
def numberOfGP ( L , N ) : NEW_LINE INDENT if ( N <= 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT count = 0 NEW_LINE res = 0 NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( L [ i - 1 ] * L [ i - 1 ] == L [ i ] * L [ i - 2 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count = 0 NEW_LINE DEDENT res += count NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 8 , 16 , 24 ] NEW_LINE N = len ( arr ) NEW_LINE print ( numberOfGP ( arr , N ) ) NEW_LINE DEDENT
Count even length subarrays having bitwise XOR equal to 0 | Function to count the number of even - length subarrays having Bitwise XOR equal to 0 ; Stores the count of required subarrays ; Stores prefix - XOR of arr [ i , i + 1 , ... N - 1 ] ; Traverse the array ; Calculate the prefix - XOR of current subarray ; Check if XOR of the current subarray is 0 and length is even ; Driver Code
def cntSubarr ( arr , N ) : NEW_LINE INDENT res = 0 NEW_LINE prefixXor = 0 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT prefixXor = arr [ i ] NEW_LINE for j in range ( i + 1 , N ) : NEW_LINE INDENT prefixXor ^= arr [ j ] NEW_LINE if ( prefixXor == 0 and ( j - i + 1 ) % 2 == 0 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 2 , 3 , 3 , 6 , 7 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE print ( cntSubarr ( arr , N ) ) NEW_LINE DEDENT
Count even length subarrays having bitwise XOR equal to 0 | Python3 program to implement the above approach ; Function to get the count of even length subarrays having bitwise xor 0 ; Stores prefix - xor of the given array ; Stores prefix - xor at even index of the array . ; Stores prefix - xor at odd index of the array . ; Stores count of subarrays that satisfy the condition ; length from 0 index to odd index is even ; Traverse the array . ; Take prefix - xor ; If index is odd ; Calculate pairs ; Increment prefix - xor at odd index ; Calculate pairs ; Increment prefix - xor at odd index ; Driver Code
M = 1000000 ; NEW_LINE def cntSubXor ( arr , N ) : NEW_LINE INDENT prefixXor = 0 ; NEW_LINE Even = [ 0 ] * M ; NEW_LINE Odd = [ 0 ] * M ; NEW_LINE cntSub = 0 ; NEW_LINE Odd [ 0 ] = 1 ; NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT prefixXor ^= arr [ i ] ; NEW_LINE if ( i % 2 == 1 ) : NEW_LINE INDENT cntSub += Odd [ prefixXor ] ; NEW_LINE Odd [ prefixXor ] += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT cntSub += Even [ prefixXor ] ; NEW_LINE Even [ prefixXor ] += 1 ; NEW_LINE DEDENT DEDENT return cntSub ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 2 , 3 , 3 , 6 , 7 , 8 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( cntSubXor ( arr , N ) ) ; NEW_LINE DEDENT
Maximize sum of array elements removed by performing the given operations | Function to find the maximum sum of the array arr [ ] where each element can be reduced to at most min [ i ] ; Stores the pair of arr [ i ] & min [ i ] ; Sorting vector of pairs ; Traverse the vector of pairs ; Add to the value of S ; Update K ; Driver Code ; Given array arr [ ] , min [ ] ; Given K ; Function Call ; Print the value of S
def findMaxSum ( arr , n , min , k , S ) : NEW_LINE INDENT A = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT A . append ( ( arr [ i ] , min [ i ] ) ) NEW_LINE DEDENT A = sorted ( A ) NEW_LINE A = A [ : : - 1 ] NEW_LINE K = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT S += max ( A [ i ] [ 0 ] - K , A [ i ] [ 1 ] ) NEW_LINE K += k NEW_LINE DEDENT return S NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr , min = [ ] , [ ] NEW_LINE arr = [ 3 , 5 , 2 , 1 ] NEW_LINE min = [ 3 , 2 , 1 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE S = 0 NEW_LINE S = findMaxSum ( arr , N , min , K , S ) NEW_LINE print ( S ) NEW_LINE DEDENT
Length of smallest substring of a given string which contains another string as subsequence | Python3 program to implement the above approach ; Function to find the length of smallest substring of a having string b as a subsequence ; Stores the characters present in string b ; Find index of characters of a that are also present in string b ; If character is present in string b ; Store the index of character ; Flag is used to check if substring is possible ; Assume that substring is possible ; Stores first and last indices of the substring respectively ; For first character of string b ; If the first character of b is not present in a ; If the first character of b is present in a ; Remove the index from map ; Update indices of the substring ; For the remaining characters of b ; If index possible for current character ; If no index is possible ; If no more substring is possible ; Update the minimum length of substring ; Return the result ; Driver Code ; Given two string
import sys NEW_LINE def minLength ( a , b ) : NEW_LINE INDENT Char = { } NEW_LINE for i in range ( len ( b ) ) : NEW_LINE INDENT Char [ b [ i ] ] = Char . get ( b [ i ] , 0 ) + 1 NEW_LINE DEDENT CharacterIndex = { } NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT x = a [ i ] NEW_LINE if ( x in Char ) : NEW_LINE INDENT CharacterIndex [ x ] = CharacterIndex . get ( x , [ ] ) NEW_LINE CharacterIndex [ x ] . append ( i ) NEW_LINE DEDENT DEDENT l = sys . maxsize NEW_LINE while ( True ) : NEW_LINE INDENT flag = 1 NEW_LINE firstVar = 0 NEW_LINE lastVar = 0 NEW_LINE for i in range ( len ( b ) ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT if ( b [ i ] not in CharacterIndex ) : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT x = CharacterIndex [ b [ i ] ] [ 0 ] NEW_LINE CharacterIndex [ b [ i ] ] . remove ( CharacterIndex [ b [ i ] ] [ 0 ] ) NEW_LINE firstVar = x NEW_LINE lastVar = x NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT elementFound = 0 NEW_LINE for e in CharacterIndex [ b [ i ] ] : NEW_LINE INDENT if ( e > lastVar ) : NEW_LINE INDENT elementFound = 1 NEW_LINE lastVar = e NEW_LINE break NEW_LINE DEDENT DEDENT if ( elementFound == 0 ) : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT l = min ( l , abs ( lastVar - firstVar ) + 1 ) NEW_LINE DEDENT return l NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = " abcdefababaef " NEW_LINE b = " abf " NEW_LINE l = minLength ( a , b ) NEW_LINE if ( l != sys . maxsize ) : NEW_LINE INDENT print ( l ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Impossible " ) NEW_LINE DEDENT DEDENT
Count smaller primes on the right of each array element | Function to check if a number is prime or not ; Function to find the count of smaller primes on the right of each array element ; Stores the count of smaller primes ; If A [ j ] <= A [ i ] and A [ j ] is prime ; Increase count ; Print the count for the current element ; Driver Code ; Function call
def is_prime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if i * i > n : NEW_LINE INDENT break NEW_LINE DEDENT if ( n % i == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT def countSmallerPrimes ( ar , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( ar [ j ] <= ar [ i ] and is_prime ( ar [ j ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ar = [ 43 , 3 , 5 , 7 , 2 , 41 ] NEW_LINE N = len ( ar ) NEW_LINE countSmallerPrimes ( ar , N ) NEW_LINE DEDENT
Check if a string is concatenation of another given string | Function to check if a is concatenation of another string ; Stores the length of str2 ; Stores the length of str1 ; If M is not multiple of N ; Traverse both the strings ; If str1 is not concatenation of str2 ; Driver Code
def checkConcat ( str1 , str2 ) : NEW_LINE INDENT N = len ( str1 ) NEW_LINE M = len ( str2 ) NEW_LINE if ( N % M != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( str1 [ i ] != str2 [ i % M ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abcabcabc " NEW_LINE str2 = " abc " NEW_LINE if ( checkConcat ( str1 , str2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimize length of string by replacing K pairs of distinct adjacent characters | Function to minimize the length of the by replacing distinct pairs of adjacent characters ; Stores the length of the given string ; Stores the index of the given string ; Traverse the given string ; If two adjacent haracters are distinct ; If all characters are equal ; If all characters are distinct ; Driver Code
def MinLen ( str , K ) : NEW_LINE INDENT N = len ( str ) NEW_LINE i = 0 NEW_LINE while ( i < N - 1 ) : NEW_LINE INDENT if ( str [ i ] != str [ i + 1 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( i == N - 1 ) : NEW_LINE INDENT return N NEW_LINE DEDENT return max ( 1 , N - K ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " aabc " NEW_LINE K = 1 NEW_LINE print ( MinLen ( str , K ) ) NEW_LINE DEDENT
Length of smallest sequence having sum X and product Y | Python3 program for the above approach ; Function for checking valid or not ; Function for checking boundary of binary search ; Function to calculate the minimum sequence size using binary search ; Initialize high and low ; Base case ; Print - 1 if a sequence cannot be generated ; Otherwise ; Iterate until difference between high and low exceeds 1 ; Calculate mid ; Reset values of high and low accordingly ; Print the answer ; Driver Code ; Function call
from math import floor , exp NEW_LINE def temp ( n , x ) : NEW_LINE INDENT return pow ( x * 1 / n , n ) NEW_LINE DEDENT def check ( n , y , x ) : NEW_LINE INDENT v = temp ( n , x ) NEW_LINE return ( v >= y ) NEW_LINE DEDENT def find ( x , y ) : NEW_LINE INDENT high = floor ( x / exp ( 1.0 ) ) NEW_LINE low = 1 NEW_LINE if ( x == y ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT elif ( not check ( high , y , x ) ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT while ( high - low > 1 ) : NEW_LINE INDENT mid = ( high + low ) // 2 NEW_LINE if ( check ( mid , y , x ) ) : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid NEW_LINE DEDENT DEDENT print ( high ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 9 NEW_LINE y = 8 NEW_LINE find ( x , y ) NEW_LINE DEDENT
Print the middle nodes of each level of a Binary Tree | Structure Node of Binary Tree ; Function to create a new node ; Return the created node ; Function that performs the DFS traversal on Tree to store all the nodes at each level in map M ; Base Case ; Push the current level node ; Left Recursion ; Right Recursion ; Function that print all the middle nodes for each level in Binary Tree ; Stores all node in each level ; Perform DFS traversal ; Traverse the map M ; Get the size of vector ; For odd number of elements ; Print ( M / 2 ) th Element ; Otherwise ; Print ( M / 2 ) th and ( M / 2 + 1 ) th Element ; Driver Code ; Given Tree ; Function Call
class node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def newnode ( d ) : NEW_LINE INDENT temp = node ( d ) NEW_LINE return temp NEW_LINE DEDENT def dfs ( root , l , M ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if l not in M : NEW_LINE INDENT M [ l ] = [ ] NEW_LINE DEDENT M [ l ] . append ( root . data ) NEW_LINE dfs ( root . left , l + 1 , M ) NEW_LINE dfs ( root . right , l + 1 , M ) NEW_LINE DEDENT def printMidNodes ( root ) : NEW_LINE INDENT M = dict ( ) NEW_LINE dfs ( root , 0 , M ) NEW_LINE for it in M . values ( ) : NEW_LINE INDENT size = len ( it ) NEW_LINE if ( size & 1 ) : NEW_LINE INDENT print ( it [ ( size - 1 ) // 2 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( str ( it [ ( size - 1 ) // 2 ] ) + ' ▁ ' + str ( it [ ( size - 1 ) // 2 + 1 ] ) ) NEW_LINE DEDENT DEDENT 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 . left = newnode ( 11 ) NEW_LINE root . left . right . right = newnode ( 6 ) NEW_LINE root . left . right . right . left = newnode ( 7 ) NEW_LINE root . left . right . right . right = newnode ( 9 ) NEW_LINE root . right . left = newnode ( 10 ) NEW_LINE root . right . right = newnode ( 8 ) NEW_LINE printMidNodes ( root ) NEW_LINE DEDENT
Smallest array that can be obtained by replacing adjacent pairs with their products | Function to minimize the size of array by performing given operations ; If all array elements are not same ; If all array elements are same ; Driver Code ; Function call
def minLength ( arr , N ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ 0 ] != arr [ i ] ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT return N NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 1 , 3 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minLength ( arr , N ) ) NEW_LINE DEDENT
Subarrays whose sum is a perfect square | Python3 program to implement the above approach ; Function to print the start and end indices of all subarrays whose sum is a perfect square ; Stores the current subarray sum ; Update current subarray sum ; Stores the square root of currSubSum ; Check if currSubSum is a perfect square or not ; Driver Code
import math NEW_LINE def PrintIndexes ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT currSubSum = 0 NEW_LINE for j in range ( i , N , 1 ) : NEW_LINE INDENT currSubSum += arr [ j ] NEW_LINE sq = int ( math . sqrt ( currSubSum ) ) NEW_LINE if ( sq * sq == currSubSum ) : NEW_LINE INDENT print ( " ( " , i , " , " , j , " ) " , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 65 , 79 , 81 ] NEW_LINE N = len ( arr ) NEW_LINE PrintIndexes ( arr , N ) NEW_LINE