codeparrot/xlcost-text-to-code huggingface dataset:
Maximize count of subsets having product of smallest element and size of the subset at least X | Function to return the maximum count of subsets possible which satisfy the above condition ; Sort the array in descending order ; Stores the count of subsets ; Stores the size of the current subset ; Check for the necessary conditions ; Driver Code
def maxSubset ( arr , N , X ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE counter = 0 NEW_LINE sz = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sz += 1 NEW_LINE if ( arr [ i ] * sz >= X ) : NEW_LINE INDENT counter += 1 NEW_LINE sz = 0 NEW_LINE DEDENT DEDENT return counter NEW_LINE DEDENT arr = [ 7 , 11 , 2 , 9 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE X = 10 NEW_LINE print ( maxSubset ( arr , N , X ) ) NEW_LINE
Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array | Python3 program to implement the above approach ; Function to find the maximum value possible of the given expression from all permutations of the array ; Stores the minimum value from the array ; Return the answer ; Driver Code
import sys NEW_LINE def maximumModuloValue ( A , n ) : NEW_LINE INDENT mn = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT mn = min ( A [ i ] , mn ) NEW_LINE DEDENT return mn NEW_LINE DEDENT A = [ 7 , 10 , 12 ] NEW_LINE n = len ( A ) NEW_LINE print ( maximumModuloValue ( A , n ) ) NEW_LINE
Final Matrix after incrementing submatrices by K in range given by Q queries | Function to update the given query ; Update top cell ; Update bottom left cell ; Update bottom right cell ; Update top right cell ; Function that updates the matrix mat [ ] [ ] by adding elements of aux [ ] [ ] ; Compute the prefix sum of all columns ; Compute the prefix sum of all rows ; Get the final matrix by adding mat and aux matrix at each cell ; Function that prints matrix mat [ ] ; Traverse each row ; Traverse each columns ; Function that performs each query in the given matrix and print the updated matrix after each operation performed ; Initialize all elements to 0 ; Update auxiliary matrix by traversing each query ; Update Query ; Compute the final answer ; Print the updated matrix ; Driver Code ; Given 4 atrix ; Given Queries ; Function Call
def updateQuery ( from_x , from_y , to_x , to_y , k , aux ) : NEW_LINE INDENT aux [ from_x ] [ from_y ] += k NEW_LINE if ( to_x + 1 < 3 ) : NEW_LINE INDENT aux [ to_x + 1 ] [ from_y ] -= k NEW_LINE DEDENT if ( to_x + 1 < 3 and to_y + 1 < 4 ) : NEW_LINE INDENT aux [ to_x + 1 ] [ to_y + 1 ] += k NEW_LINE DEDENT if ( to_y + 1 < 4 ) : NEW_LINE INDENT aux [ from_x ] [ to_y + 1 ] -= k NEW_LINE DEDENT return aux NEW_LINE DEDENT def updatematrix ( mat , aux ) : NEW_LINE INDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 1 , 4 ) : NEW_LINE INDENT aux [ i ] [ j ] += aux [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT for i in range ( 4 ) : NEW_LINE INDENT for j in range ( 1 , 3 ) : NEW_LINE INDENT aux [ j ] [ i ] += aux [ j - 1 ] [ i ] NEW_LINE DEDENT DEDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 4 ) : NEW_LINE INDENT mat [ i ] [ j ] += aux [ i ] [ j ] NEW_LINE DEDENT DEDENT return mat NEW_LINE DEDENT def printmatrix ( mat ) : NEW_LINE INDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 4 ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def matrixQuery ( mat , Q , q ) : NEW_LINE INDENT aux = [ [ 0 for i in range ( 4 ) ] for i in range ( 3 ) ] NEW_LINE for i in range ( Q ) : NEW_LINE INDENT updateQuery ( q [ i ] [ 0 ] , q [ i ] [ 1 ] , q [ i ] [ 2 ] , q [ i ] [ 3 ] , q [ i ] [ 4 ] , aux ) NEW_LINE DEDENT updatematrix ( mat , aux ) NEW_LINE printmatrix ( mat ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 0 , 1 , 2 ] , [ 0 , 2 , 4 , 1 ] , [ 1 , 2 , 1 , 0 ] ] NEW_LINE Q = 1 NEW_LINE q = [ [ 0 , 0 , 1 , 1 , 2 ] ] NEW_LINE matrixQuery ( mat , Q , q ) NEW_LINE DEDENT
Find Second largest element in an array | Function to print the second largest elements ; There should be atleast two elements ; Sort the array ; Start from second last element as the largest element is at last ; If the element is not equal to largest element ; Driver code
def print2largest ( arr , arr_size ) : NEW_LINE INDENT if ( arr_size < 2 ) : NEW_LINE INDENT print ( " ▁ Invalid ▁ Input ▁ " ) NEW_LINE return NEW_LINE DEDENT arr . sort NEW_LINE for i in range ( arr_size - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ arr_size - 1 ] ) : NEW_LINE print ( " The ▁ second ▁ largest ▁ element ▁ is " , arr [ i ] ) NEW_LINE return NEW_LINE DEDENT print ( " There ▁ is ▁ no ▁ second ▁ largest ▁ element " ) NEW_LINE DEDENT arr = [ 12 , 35 , 1 , 10 , 34 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print2largest ( arr , n ) NEW_LINE
Find sum of all left leaves in a given Binary Tree | Python3 program to find sum of all left leaves ; A binary tree node ; Return the sum of left leaf nodes ; A queue of pairs to do bfs traversal and keep track if the node is a left or right child if boolean value is true then it is a left child . ; Do bfs traversal ; If temp is a leaf node and left child of its parent ; If it is not leaf then push its children nodes into queue ; Boolean value is true here because it is left child of its parent ; Boolean value is false here because it is right child of its parent ; Driver Code
from collections import deque NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . key = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def sumOfLeftLeaves ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT q = deque ( ) NEW_LINE q . append ( [ root , 0 ] ) NEW_LINE sum = 0 NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT temp = q [ 0 ] [ 0 ] NEW_LINE is_left_child = q [ 0 ] [ 1 ] NEW_LINE q . popleft ( ) NEW_LINE if ( not temp . left and not temp . right and is_left_child ) : NEW_LINE INDENT sum = sum + temp . key NEW_LINE DEDENT if ( temp . left ) : NEW_LINE INDENT q . append ( [ temp . left , 1 ] ) NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( [ temp . right , 0 ] ) NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 20 ) NEW_LINE root . left = Node ( 9 ) NEW_LINE root . right = Node ( 49 ) NEW_LINE root . right . left = Node ( 23 ) NEW_LINE root . right . right = Node ( 52 ) NEW_LINE root . right . right . left = Node ( 50 ) NEW_LINE root . left . left = Node ( 5 ) NEW_LINE root . left . right = Node ( 12 ) NEW_LINE root . left . right . right = Node ( 12 ) NEW_LINE print ( " Sum ▁ of ▁ left ▁ leaves ▁ is " , sumOfLeftLeaves ( root ) ) NEW_LINE DEDENT
Count of nested polygons that can be drawn by joining vertices internally | Function that counts the nested polygons inside another polygons ; Stores the count ; Child polygons can only exists if parent polygon has sides > 5 ; Get next nested polygon ; Return the count ; Given side of polygon ; Function call
def countNestedPolygons ( sides ) : NEW_LINE INDENT count = 0 NEW_LINE while ( sides > 5 ) : NEW_LINE INDENT sides //= 2 NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT N = 12 NEW_LINE print ( countNestedPolygons ( N ) ) NEW_LINE
Find Second largest element in an array | Function to print second largest elements ; There should be atleast two elements ; Find the largest element ; Find the second largest element ; Driver code
def print2largest ( arr , arr_size ) : NEW_LINE INDENT if ( arr_size < 2 ) : NEW_LINE INDENT print ( " ▁ Invalid ▁ Input ▁ " ) ; NEW_LINE return ; NEW_LINE DEDENT largest = second = - 2454635434 ; NEW_LINE for i in range ( 0 , arr_size ) : NEW_LINE INDENT largest = max ( largest , arr [ i ] ) ; NEW_LINE DEDENT for i in range ( 0 , arr_size ) : NEW_LINE INDENT if ( arr [ i ] != largest ) : NEW_LINE INDENT second = max ( second , arr [ i ] ) ; NEW_LINE DEDENT DEDENT if ( second == - 2454635434 ) : NEW_LINE INDENT print ( " There ▁ is ▁ no ▁ second ▁ " + " largest ▁ element " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ second ▁ largest ▁ " +   " element is " , second ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 35 , 1 , 10 , 34 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print2largest ( arr , n ) ; NEW_LINE DEDENT
Find Second largest element in an array | Function to print the second largest elements ; There should be atleast two elements ; If current element is smaller than first then update both first and second ; If arr [ i ] is in between first and second then update second ; Driver program to test above function
def print2largest ( arr , arr_size ) : NEW_LINE INDENT if ( arr_size < 2 ) : NEW_LINE INDENT print ( " ▁ Invalid ▁ Input ▁ " ) NEW_LINE return NEW_LINE DEDENT first = second = - 2147483648 NEW_LINE for i in range ( arr_size ) : NEW_LINE INDENT if ( arr [ i ] > first ) : NEW_LINE INDENT second = first NEW_LINE first = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > second and arr [ i ] != first ) : NEW_LINE INDENT second = arr [ i ] NEW_LINE DEDENT DEDENT if ( second == - 2147483648 ) : NEW_LINE INDENT print ( " There ▁ is ▁ no ▁ second ▁ largest ▁ element " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ second ▁ largest ▁ element ▁ is " , second ) NEW_LINE DEDENT DEDENT arr = [ 12 , 35 , 1 , 10 , 34 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print2largest ( arr , n ) NEW_LINE
Queries to count characters having odd frequency in a range [ L , R ] | Function to print the number of characters having odd frequencies for each query ; A function to construct the arr [ ] and prefix [ ] ; Stores array length ; Stores the unique powers of 2 associated to each character ; Prefix array to store the XOR values from array elements ; Driver Code
def queryResult ( prefix , Q ) : NEW_LINE INDENT l = Q [ 0 ] NEW_LINE r = Q [ 1 ] NEW_LINE if ( l == 0 ) : NEW_LINE INDENT xorval = prefix [ r ] NEW_LINE print ( bin ( xorval ) . count ( '1' ) ) NEW_LINE DEDENT else : NEW_LINE INDENT xorval = prefix [ r ] ^ prefix [ l - 1 ] NEW_LINE print ( bin ( xorval ) . count ( '1' ) ) NEW_LINE DEDENT DEDENT def calculateCount ( S , Q , m ) : NEW_LINE INDENT n = len ( S ) NEW_LINE arr = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = ( 1 << ( ord ( S [ i ] ) - ord ( ' a ' ) ) ) NEW_LINE DEDENT prefix = [ 0 ] * n NEW_LINE x = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT x ^= arr [ i ] NEW_LINE prefix [ i ] = x NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT queryResult ( prefix , Q [ i ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE Q = [ [ 2 , 4 ] , [ 0 , 3 ] , [ 0 , 12 ] ] NEW_LINE calculateCount ( S , Q , 3 ) NEW_LINE DEDENT
Count of Binary Strings possible as per given conditions | Function to generate maximum possible strings that can be generated ; Maximum possible strings ; Driver code
def countStrings ( A , B , K ) : NEW_LINE INDENT X = ( A + B ) // ( K + 1 ) NEW_LINE return ( min ( A , min ( B , X ) ) * ( K + 1 ) ) NEW_LINE DEDENT N , M , K = 101 , 231 , 15 NEW_LINE print ( countStrings ( N , M , K ) ) NEW_LINE
Construct a Matrix N x N with first N ^ 2 natural numbers for an input N | Function to print the desired matrix ; Iterate ove all [ 0 , N ] ; If is even ; If row number is even print the row in forward order ; if row number is odd print the row in reversed order ; Given Matrix Size ; Function Call
def UniqueMatrix ( N ) : NEW_LINE INDENT element_value = 1 NEW_LINE i = 0 NEW_LINE while ( i < N ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT for f in range ( element_value , element_value + N , 1 ) : NEW_LINE INDENT print ( f , end = ' ▁ ' ) NEW_LINE DEDENT element_value += N NEW_LINE DEDENT else : NEW_LINE INDENT for k in range ( element_value + N - 1 , element_value - 1 , - 1 ) : NEW_LINE INDENT print ( k , end = ' ▁ ' ) NEW_LINE DEDENT element_value += N NEW_LINE DEDENT print ( ) NEW_LINE i = i + 1 NEW_LINE DEDENT DEDENT N = 4 NEW_LINE UniqueMatrix ( N ) NEW_LINE
Maximum and minimum of an array using minimum number of comparisons | structure is used to return two values from minMax ( ) ; If there is only one element then return it as min and max both ; If there are more than one elements , then initialize min and max ; Driver Code
class pair : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . min = 0 NEW_LINE self . max = 0 NEW_LINE DEDENT DEDENT def getMinMax ( arr : list , n : int ) -> pair : NEW_LINE INDENT minmax = pair ( ) NEW_LINE if n == 1 : NEW_LINE INDENT minmax . max = arr [ 0 ] NEW_LINE minmax . min = arr [ 0 ] NEW_LINE return minmax NEW_LINE DEDENT if arr [ 0 ] > arr [ 1 ] : NEW_LINE INDENT minmax . max = arr [ 0 ] NEW_LINE minmax . min = arr [ 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT minmax . max = arr [ 1 ] NEW_LINE minmax . min = arr [ 0 ] NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if arr [ i ] > minmax . max : NEW_LINE INDENT minmax . max = arr [ i ] NEW_LINE DEDENT elif arr [ i ] < minmax . min : NEW_LINE INDENT minmax . min = arr [ i ] NEW_LINE DEDENT DEDENT return minmax NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1000 , 11 , 445 , 1 , 330 , 3000 ] NEW_LINE arr_size = 6 NEW_LINE minmax = getMinMax ( arr , arr_size ) NEW_LINE print ( " Minimum ▁ element ▁ is " , minmax . min ) NEW_LINE print ( " Maximum ▁ element ▁ is " , minmax . max ) NEW_LINE DEDENT
Find sum of all nodes of the given perfect binary tree | function to find Sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; list of vector to store nodes of all of the levels ; store the nodes of last level i . e . , the leaf nodes ; store nodes of rest of the level by moving in bottom - up manner ; loop to calculate values of parent nodes from the children nodes of lower level ; store the value of parent node as Sum of children nodes ; traverse the list of vector and calculate the Sum ; Driver Code
def SumNodes ( l ) : NEW_LINE INDENT leafNodeCount = pow ( 2 , l - 1 ) NEW_LINE vec = [ [ ] for i in range ( l ) ] NEW_LINE for i in range ( 1 , leafNodeCount + 1 ) : NEW_LINE INDENT vec [ l - 1 ] . append ( i ) NEW_LINE DEDENT for i in range ( l - 2 , - 1 , - 1 ) : NEW_LINE INDENT k = 0 NEW_LINE while ( k < len ( vec [ i + 1 ] ) - 1 ) : NEW_LINE INDENT vec [ i ] . append ( vec [ i + 1 ] [ k ] + vec [ i + 1 ] [ k + 1 ] ) NEW_LINE k += 2 NEW_LINE DEDENT DEDENT Sum = 0 NEW_LINE for i in range ( l ) : NEW_LINE INDENT for j in range ( len ( vec [ i ] ) ) : NEW_LINE INDENT Sum += vec [ i ] [ j ] NEW_LINE DEDENT DEDENT return Sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 3 NEW_LINE print ( SumNodes ( l ) ) NEW_LINE DEDENT
Maximum and minimum of an array using minimum number of comparisons | getMinMax ( ) ; If array has even number of elements then initialize the first two elements as minimum and maximum ; set the starting index for loop ; If array has odd number of elements then initialize the first element as minimum and maximum ; set the starting index for loop ; In the while loop , pick elements in pair and compare the pair with max and min so far ; Increment the index by 2 as two elements are processed in loop ; Driver Code
def getMinMax ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT mx = max ( arr [ 0 ] , arr [ 1 ] ) NEW_LINE mn = min ( arr [ 0 ] , arr [ 1 ] ) NEW_LINE i = 2 NEW_LINE DEDENT else : NEW_LINE INDENT mx = mn = arr [ 0 ] NEW_LINE i = 1 NEW_LINE DEDENT while ( i < n - 1 ) : NEW_LINE INDENT if arr [ i ] < arr [ i + 1 ] : NEW_LINE INDENT mx = max ( mx , arr [ i + 1 ] ) NEW_LINE mn = min ( mn , arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT mx = max ( mx , arr [ i ] ) NEW_LINE mn = min ( mn , arr [ i + 1 ] ) NEW_LINE DEDENT i += 2 NEW_LINE DEDENT return ( mx , mn ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1000 , 11 , 445 , 1 , 330 , 3000 ] NEW_LINE mx , mn = getMinMax ( arr ) NEW_LINE print ( " Minimum ▁ element ▁ is " , mn ) NEW_LINE print ( " Maximum ▁ element ▁ is " , mx ) NEW_LINE DEDENT
MO 's Algorithm (Query Square Root Decomposition) | Set 1 (Introduction) | Function that accepts array and list of queries and print sum of each query ; Traverse through each query ; Extract left and right indices ; Compute sum of current query range ; Print sum of current query range ; Driver script
def printQuerySum ( arr , Q ) : NEW_LINE INDENT for q in Q : NEW_LINE INDENT L , R = q NEW_LINE s = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT s += arr [ i ] NEW_LINE DEDENT print ( " Sum ▁ of " , q , " is " , s ) NEW_LINE DEDENT DEDENT arr = [ 1 , 1 , 2 , 1 , 3 , 4 , 5 , 2 , 8 ] NEW_LINE Q = [ [ 0 , 4 ] , [ 1 , 3 ] , [ 2 , 4 ] ] NEW_LINE printQuerySum ( arr , Q ) NEW_LINE
Count of unique palindromic strings of length X from given string | Function to count different palindromic string of length X from the given string S ; Base case ; Create the frequency array Intitalise frequency array with 0 ; Count the frequency in the string ; Store frequency of the char ; Check the frequency which is greater than zero ; No . of different char we can put at the position of the i and x - i ; Iterator pointing to the last element of the set ; Decrease the value of the char we put on the position i and n - i ; Different no of char we can put at the position x / 2 ; Return total no of different string ; Driver code
def findways ( s , x ) : NEW_LINE INDENT if ( x > len ( s ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT n = len ( s ) NEW_LINE freq = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT se = set ( ) NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] > 0 ) : NEW_LINE INDENT se . add ( freq [ i ] ) NEW_LINE DEDENT DEDENT ans = 1 NEW_LINE for i in range ( x // 2 ) : NEW_LINE INDENT count = 0 NEW_LINE for u in se : NEW_LINE INDENT if ( u >= 2 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT ans *= count NEW_LINE DEDENT p = list ( se ) NEW_LINE val = p [ - 1 ] NEW_LINE p . pop ( - 1 ) NEW_LINE se = set ( p ) NEW_LINE if ( val > 2 ) : NEW_LINE INDENT se . add ( val - 2 ) NEW_LINE DEDENT DEDENT if ( x % 2 != 0 ) : NEW_LINE INDENT count = 0 NEW_LINE for u in se : NEW_LINE INDENT if ( u > 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT ans = ans * count NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " aaa " NEW_LINE x = 2 NEW_LINE print ( findways ( s , x ) ) NEW_LINE DEDENT
Min operations to reduce N to 1 by multiplying by A or dividing by B | Function to check if it is possible to convert a number N to 1 by a minimum use of the two operations ; For the Case b % a != 0 ; Check if n equal to 1 ; Check if n is not divisible by b ; Initialize a variable c ; Loop until n is divisible by b ; Count number of divisions ; Loop until n is divisible by c ; Count number of operations ; Check if n is reduced to 1 ; Count steps ; Return the total number of steps ; Given n , a and b ; Function Call
def FindIfPossible ( n , a , b ) : NEW_LINE INDENT if ( b % a ) != 0 : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( n % b ) != 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return int ( n / b ) NEW_LINE DEDENT DEDENT c = b / a NEW_LINE x = 0 NEW_LINE y = 0 NEW_LINE while ( n % b == 0 ) : NEW_LINE INDENT n /= b NEW_LINE x += 1 NEW_LINE DEDENT while ( n % c == 0 ) : NEW_LINE INDENT n /= c NEW_LINE y += 1 NEW_LINE DEDENT if n == 1 : NEW_LINE INDENT total_steps = x + 2 * y NEW_LINE return total_steps NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT n = 48 NEW_LINE a = 3 NEW_LINE b = 12 NEW_LINE print ( FindIfPossible ( n , a , b ) ) NEW_LINE
Sqrt ( or Square Root ) Decomposition Technique | Set 1 ( Introduction ) | Python 3 program to demonstrate working of Square Root Decomposition . ; original array ; decomposed array ; block size ; Time Complexity : O ( 1 ) ; Time Complexity : O ( sqrt ( n ) ) ; traversing first block in range ; traversing completely overlapped blocks in range ; traversing last block in range ; Fills values in input [ ] ; initiating block pointer ; calculating size of block ; building the decomposed array ; entering next block incementing block pointer ; We have used separate array for input because the purpose of this code is to explain SQRT decomposition in competitive programming where we have multiple inputs .
from math import sqrt NEW_LINE MAXN = 10000 NEW_LINE SQRSIZE = 100 NEW_LINE arr = [ 0 ] * ( MAXN ) NEW_LINE block = [ 0 ] * ( SQRSIZE ) NEW_LINE blk_sz = 0 NEW_LINE def update ( idx , val ) : NEW_LINE INDENT blockNumber = idx // blk_sz NEW_LINE block [ blockNumber ] += val - arr [ idx ] NEW_LINE arr [ idx ] = val NEW_LINE DEDENT def query ( l , r ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( l < r and l % blk_sz != 0 and l != 0 ) : NEW_LINE INDENT sum += arr [ l ] NEW_LINE l += 1 NEW_LINE DEDENT while ( l + blk_sz <= r ) : NEW_LINE INDENT sum += block [ l // blk_sz ] NEW_LINE l += blk_sz NEW_LINE DEDENT while ( l <= r ) : NEW_LINE INDENT sum += arr [ l ] NEW_LINE l += 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT def preprocess ( input , n ) : NEW_LINE INDENT blk_idx = - 1 NEW_LINE global blk_sz NEW_LINE blk_sz = int ( sqrt ( n ) ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = input [ i ] ; NEW_LINE if ( i % blk_sz == 0 ) : NEW_LINE INDENT blk_idx += 1 ; NEW_LINE DEDENT block [ blk_idx ] += arr [ i ] NEW_LINE DEDENT DEDENT input = [ 1 , 5 , 2 , 4 , 6 , 1 , 3 , 5 , 7 , 10 ] NEW_LINE n = len ( input ) NEW_LINE preprocess ( input , n ) NEW_LINE print ( " query ( 3,8 ) ▁ : ▁ " , query ( 3 , 8 ) ) NEW_LINE print ( " query ( 1,6 ) ▁ : ▁ " , query ( 1 , 6 ) ) NEW_LINE update ( 8 , 0 ) NEW_LINE print ( " query ( 8,8 ) ▁ : ▁ " , query ( 8 , 8 ) ) NEW_LINE
Find two numbers with sum N such that neither of them contains digit K | Python3 implementation of the above approach ; Count leading zeros ; It removes i characters starting from index 0 ; Check each digit of the N ; If digit is K break it ; For odd numbers ; Add D1 to A and D2 to B ; If the digit is not K , no need to break String D in A and 0 in B ; Remove leading zeros ; Print the answer ; Driver code
def removeLeadingZeros ( Str ) : NEW_LINE INDENT i = 0 NEW_LINE n = len ( Str ) NEW_LINE while ( Str [ i ] == '0' and i < n ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT Str = Str [ i : ] NEW_LINE return Str NEW_LINE DEDENT def findPairs ( Sum , K ) : NEW_LINE INDENT A = " " NEW_LINE B = " " NEW_LINE N = str ( Sum ) NEW_LINE n = len ( N ) NEW_LINE for i in range ( n ) : NEW_LINE D = int ( N [ i ] ) - int ( '0' ) NEW_LINE if ( D == K ) : NEW_LINE INDENT D1 = D // 2 ; NEW_LINE D2 = ( D // 2 ) + ( D % 2 ) NEW_LINE A = A + ( str ) ( D1 + int ( '0' ) ) NEW_LINE B = B + ( str ) ( D2 + int ( '0' ) ) NEW_LINE DEDENT else : NEW_LINE INDENT A = A + ( str ) ( D + int ( '0' ) ) NEW_LINE B = B + '0' NEW_LINE DEDENT A = removeLeadingZeros ( A ) NEW_LINE B = removeLeadingZeros ( B ) NEW_LINE print ( A + " , ▁ " + B ) NEW_LINE DEDENT N = 33673 NEW_LINE K = 3 NEW_LINE findPairs ( N , K ) NEW_LINE