codeparrot/xlcost-text-to-code huggingface dataset:
Range Minimum Query ( Square Root Decomposition and Sparse Table ) | Python3 program to do range minimum query in O ( 1 ) time with O ( n * n ) extra space and O ( n * n ) preprocessing time . ; lookup [ i ] [ j ] is going to store index of minimum value in arr [ i . . j ] ; Structure to represent a query range ; Fills lookup array lookup [ n ] [ n ] for all possible values of query ranges ; Initialize lookup [ ] [ ] for the intervals with length 1 ; Fill rest of the entries in bottom up manner ; To find minimum of [ 0 , 4 ] , we compare minimum of arr [ lookup [ 0 ] [ 3 ] ] with arr [ 4 ] . ; Prints minimum of given m query ranges in arr [ 0. . n - 1 ] ; Fill lookup table for all possible input queries ; One by one compute sum of all queries ; Left and right boundaries of current range ; Print sum of current query range ; Driver code
MAX = 500 NEW_LINE lookup = [ [ 0 for j in range ( MAX ) ] for i in range ( MAX ) ] NEW_LINE class Query : NEW_LINE INDENT def __init__ ( self , L , R ) : NEW_LINE INDENT self . L = L NEW_LINE self . R = R NEW_LINE DEDENT DEDENT def preprocess ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT lookup [ i ] [ i ] = i ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ lookup [ i ] [ j - 1 ] ] < arr [ j ] ) : NEW_LINE INDENT lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT lookup [ i ] [ j ] = j ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def RMQ ( arr , n , q , m ) : NEW_LINE INDENT preprocess ( arr , n ) ; NEW_LINE for i in range ( m ) : NEW_LINE INDENT L = q [ i ] . L NEW_LINE R = q [ i ] . R ; NEW_LINE print ( " Minimum ▁ of ▁ [ " + str ( L ) + " , ▁ " + str ( R ) + " ] ▁ is ▁ " + str ( arr [ lookup [ L ] [ R ] ] ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 ] NEW_LINE n = len ( a ) NEW_LINE q = [ Query ( 0 , 4 ) , Query ( 4 , 7 ) , Query ( 7 , 8 ) ] NEW_LINE m = len ( q ) NEW_LINE RMQ ( a , n , q , m ) ; NEW_LINE DEDENT
Minimum salary hike for each employee such that no employee feels unfair | Python Program to find the minimum hike of each employee such that no adjacent employee feels unfair ; As hikes are positive integers , keeping minimum value ; Pass - 1 : compare with previous employee ; Pass - 2 : compare with Next employee ; Driver Code ; Function Call ; result -> [ 2 , 1 , 3 , 2 , 1 , 2 ] ; Function Call ; result -> [ 1 , 2 , 3 , 1 ] ; Function Call ; result -> [ 1 , 2 ]
def findMinimumHike ( ratings , employees ) : NEW_LINE INDENT hikes = [ 1 for i in range ( employees ) ] NEW_LINE for i in range ( 1 , employees ) : NEW_LINE INDENT if ( ratings [ i - 1 ] < ratings [ i ] and hikes [ i - 1 ] >= hikes [ i ] ) : NEW_LINE INDENT hikes [ i ] = hikes [ i - 1 ] + 1 ; NEW_LINE DEDENT DEDENT for i in range ( employees - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( ratings [ i ] > ratings [ i + 1 ] and hikes [ i + 1 ] >= hikes [ i ] ) : NEW_LINE INDENT hikes [ i ] = hikes [ i + 1 ] + 1 ; NEW_LINE DEDENT DEDENT return hikes NEW_LINE DEDENT data = [ 5 , 3 , 4 , 2 , 1 , 6 ] NEW_LINE result = findMinimumHike ( data , len ( data ) ) NEW_LINE print ( result ) NEW_LINE data = [ 1 , 3 , 5 , 4 ] NEW_LINE result = findMinimumHike ( data , len ( data ) ) NEW_LINE print ( result ) NEW_LINE data = [ 1 , 4 ] NEW_LINE result = findMinimumHike ( data , len ( data ) ) NEW_LINE print ( result ) NEW_LINE
Range Minimum Query ( Square Root Decomposition and Sparse Table ) | Python3 program to do range minimum query in O ( 1 ) time with O ( n Log n ) extra space and O ( n Log n ) preprocessing time ; lookup [ i ] [ j ] is going to store index of minimum value in arr [ i . . j ] . Ideally lookup table size should not be fixed and should be determined using n Log n . It is kept constant to keep code simple . ; Structure to represent a query range ; Fills lookup array lookup [ ] [ ] in bottom up manner . ; Initialize M for the intervals with length 1 ; Compute values from smaller to bigger intervals ; Compute minimum value for all intervals with size 2 ^ j ; For arr [ 2 ] [ 10 ] , we compare arr [ lookup [ 0 ] [ 3 ] ] and arr [ lookup [ 3 ] [ 3 ] ] ; Returns minimum of arr [ L . . R ] ; For [ 2 , 10 ] , j = 3 ; For [ 2 , 10 ] , we compare arr [ lookup [ 0 ] [ 3 ] ] and arr [ lookup [ 3 ] [ 3 ] ] , ; Prints minimum of given m query ranges in arr [ 0. . n - 1 ] ; Fills table lookup [ n ] [ Log n ] ; One by one compute sum of all queries ; Left and right boundaries of current range ; Print sum of current query range ; Driver Code
from math import log2 NEW_LINE MAX = 500 NEW_LINE lookup = [ [ 0 for i in range ( 500 ) ] for j in range ( 500 ) ] NEW_LINE class Query : NEW_LINE INDENT def __init__ ( self , l , r ) : NEW_LINE INDENT self . L = l NEW_LINE self . R = r NEW_LINE DEDENT DEDENT def preprocess ( arr : list , n : int ) : NEW_LINE INDENT global lookup NEW_LINE for i in range ( n ) : NEW_LINE INDENT lookup [ i ] [ 0 ] = i NEW_LINE DEDENT j = 1 NEW_LINE while ( 1 << j ) <= n : NEW_LINE INDENT i = 0 NEW_LINE while i + ( 1 << j ) - 1 < n : NEW_LINE INDENT if ( arr [ lookup [ i ] [ j - 1 ] ] < arr [ lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ] ) : NEW_LINE INDENT lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT lookup [ i ] [ j ] = lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT DEDENT def query ( arr : list , L : int , R : int ) -> int : NEW_LINE INDENT global lookup NEW_LINE j = int ( log2 ( R - L + 1 ) ) NEW_LINE if ( arr [ lookup [ L ] [ j ] ] <= arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] ) : NEW_LINE INDENT return arr [ lookup [ L ] [ j ] ] NEW_LINE DEDENT else : NEW_LINE INDENT return arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] NEW_LINE DEDENT DEDENT def RMQ ( arr : list , n : int , q : list , m : int ) : NEW_LINE INDENT preprocess ( arr , n ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT L = q [ i ] . L NEW_LINE R = q [ i ] . R NEW_LINE print ( " Minimum ▁ of ▁ [ % d , ▁ % d ] ▁ is ▁ % d " % ( L , R , query ( arr , L , R ) ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 ] NEW_LINE n = len ( a ) NEW_LINE q = [ Query ( 0 , 4 ) , Query ( 4 , 7 ) , Query ( 7 , 8 ) ] NEW_LINE m = len ( q ) NEW_LINE RMQ ( a , n , q , m ) 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 ; function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; sum of nodes at last level ; sum of all nodes ; Driver Code
import math NEW_LINE def sumNodes ( l ) : NEW_LINE INDENT leafNodeCount = math . pow ( 2 , l - 1 ) ; NEW_LINE sumLastLevel = 0 ; NEW_LINE sumLastLevel = ( ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ) ; NEW_LINE sum = sumLastLevel * l ; NEW_LINE return int ( sum ) ; NEW_LINE DEDENT l = 3 ; NEW_LINE print ( sumNodes ( l ) ) ; NEW_LINE
Longest subsequence with different adjacent characters | dp table ; A recursive function to find the update the dp table ; If we reach end of the String ; If subproblem has been computed ; Initialise variable to find the maximum length ; Choose the current character ; Omit the current character ; Return the store answer to the current subproblem ; Function to find the longest Subsequence with different adjacent character ; Length of the String s ; Return the final ans after every recursive call ; Driver Code ; Function call
dp = [ [ - 1 for i in range ( 27 ) ] for j in range ( 100005 ) ] ; NEW_LINE def calculate ( pos , prev , s ) : NEW_LINE INDENT if ( pos == len ( s ) ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ pos ] [ prev ] != - 1 ) : NEW_LINE INDENT return dp [ pos ] [ prev ] ; NEW_LINE DEDENT val = 0 ; NEW_LINE if ( ord ( s [ pos ] ) - ord ( ' a ' ) + 1 != prev ) : NEW_LINE INDENT val = max ( val , 1 + calculate ( pos + 1 , ord ( s [ pos ] ) - ord ( ' a ' ) + 1 , s ) ) ; NEW_LINE DEDENT val = max ( val , calculate ( pos + 1 , prev , s ) ) ; NEW_LINE dp [ pos ] [ prev ] = val ; NEW_LINE return dp [ pos ] [ prev ] ; NEW_LINE DEDENT def longestSubsequence ( s ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE return calculate ( 0 , 0 , s ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " ababa " ; NEW_LINE print ( longestSubsequence ( str ) ) ; NEW_LINE DEDENT
Constant time range add operation on an array | Utility method to add value val , to range [ lo , hi ] ; Utility method to get actual array from operation array ; convert array into prefix sum array ; method to print final updated array ; Driver code ; Range add Queries
def add ( arr , N , lo , hi , val ) : NEW_LINE INDENT arr [ lo ] += val NEW_LINE if ( hi != N - 1 ) : NEW_LINE INDENT arr [ hi + 1 ] -= val NEW_LINE DEDENT DEDENT def updateArray ( arr , N ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT arr [ i ] += arr [ i - 1 ] NEW_LINE DEDENT DEDENT def printArr ( arr , N ) : NEW_LINE INDENT updateArray ( arr , N ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT N = 6 NEW_LINE arr = [ 0 for i in range ( N ) ] NEW_LINE add ( arr , N , 0 , 2 , 100 ) NEW_LINE add ( arr , N , 1 , 5 , 100 ) NEW_LINE add ( arr , N , 2 , 3 , 100 ) NEW_LINE printArr ( arr , N ) NEW_LINE
Maximum profit by buying and selling a share at most K times | Greedy Approach | Function to return the maximum profit ; Find the farthest decreasing span of prices before prices rise ; Find the farthest increasing span of prices before prices fall again ; Check if the current buying price is greater than that of the previous transaction ; Store the profit ; Remove the previous transaction ; Check if the current selling price is less than that of the previous transactions ; Store the new profit ; Remove the previous transaction ; Add the new transactions ; Finally store the profits of all the transactions ; Add the highest K profits ; Return the maximum profit ; Driver code
def maxProfit ( n , k , prices ) : NEW_LINE INDENT ans = 0 NEW_LINE buy = 0 NEW_LINE sell = 0 NEW_LINE transaction = [ ] NEW_LINE profits = [ ] NEW_LINE while ( sell < n ) : NEW_LINE INDENT buy = sell NEW_LINE while ( buy < n - 1 and prices [ buy ] >= prices [ buy + 1 ] ) : NEW_LINE INDENT buy += 1 NEW_LINE DEDENT sell = buy + 1 NEW_LINE while ( sell < n and prices [ sell ] >= prices [ sell - 1 ] ) : NEW_LINE INDENT sell += 1 NEW_LINE DEDENT while ( len ( transaction ) != 0 and prices [ buy ] < prices [ transaction [ len ( transaction ) - 1 ] [ 0 ] ] ) : NEW_LINE INDENT p = transaction [ len ( transaction ) - 1 ] NEW_LINE profits . append ( prices [ p [ 1 ] - 1 ] - prices [ p [ 0 ] ] ) NEW_LINE transaction . remove ( transaction [ len ( transaction ) - 1 ] ) NEW_LINE DEDENT profits . sort ( reverse = True ) NEW_LINE while ( len ( transaction ) != 0 and prices [ sell - 1 ] > prices [ transaction [ len ( transaction ) - 1 ] [ 1 ] - 1 ] ) : NEW_LINE INDENT p = transaction [ len ( transaction ) - 1 ] NEW_LINE profits . append ( prices [ p [ 1 ] - 1 ] - prices [ buy ] ) NEW_LINE buy = p [ 0 ] NEW_LINE transaction . remove ( transaction [ len ( transaction ) - 1 ] ) NEW_LINE DEDENT transaction . append ( [ buy , sell ] ) NEW_LINE DEDENT profits . sort ( reverse = True ) NEW_LINE while ( len ( transaction ) != 0 ) : NEW_LINE INDENT profits . append ( prices [ transaction [ len ( transaction ) - 1 ] [ 1 ] - 1 ] - prices [ transaction [ len ( transaction ) - 1 ] [ 0 ] ] ) NEW_LINE transaction . remove ( transaction [ len ( transaction ) - 1 ] ) NEW_LINE DEDENT profits . sort ( reverse = True ) NEW_LINE while ( k != 0 and len ( profits ) != 0 ) : NEW_LINE INDENT ans += profits [ 0 ] NEW_LINE profits . remove ( profits [ 0 ] ) NEW_LINE k -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 3 NEW_LINE prices = [ 1 , 12 , 10 , 7 , 10 , 13 , 11 , 10 , 7 , 6 , 9 ] NEW_LINE n = len ( prices ) NEW_LINE print ( " Maximum ▁ profit ▁ is " , maxProfit ( n , k , prices ) ) NEW_LINE DEDENT
Find sum in range L to R in given sequence of integers | Function to find the sum within the given range ; generating array from given sequence ; calculate the desired sum ; return the sum ; initialise the range
def findSum ( L , R ) : NEW_LINE INDENT arr = [ ] NEW_LINE i = 0 NEW_LINE x = 2 NEW_LINE k = 0 NEW_LINE while ( i <= R ) : NEW_LINE INDENT arr . insert ( k , i + x ) NEW_LINE k += 1 NEW_LINE if ( i + 1 <= R ) : NEW_LINE INDENT arr . insert ( k , i + 1 + x ) NEW_LINE DEDENT k += 1 NEW_LINE x *= - 1 NEW_LINE i += 2 NEW_LINE DEDENT sum = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT L = 0 NEW_LINE R = 5 NEW_LINE print ( findSum ( L , R ) ) NEW_LINE
Queries for GCD of all numbers of an array except elements in a given range | Calculating GCD using euclid algorithm ; Filling the prefix and suffix array ; Filling the prefix array following relation prefix ( i ) = GCD ( prefix ( i - 1 ) , arr ( i ) ) ; Filling the suffix array following the relation suffix ( i ) = GCD ( suffix ( i + 1 ) , arr ( i ) ) ; To calculate gcd of the numbers outside range ; If l = 0 , we need to tell GCD of numbers from r + 1 to n ; If r = n - 1 we need to return the gcd of numbers from 1 to l ; Driver code
def GCD ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return GCD ( b , a % b ) NEW_LINE DEDENT def FillPrefixSuffix ( prefix , arr , suffix , n ) : NEW_LINE INDENT prefix [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefix [ i ] = GCD ( prefix [ i - 1 ] , arr [ i ] ) NEW_LINE DEDENT suffix [ n - 1 ] = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffix [ i ] = GCD ( suffix [ i + 1 ] , arr [ i ] ) NEW_LINE DEDENT DEDENT def GCDoutsideRange ( l , r , prefix , suffix , n ) : NEW_LINE INDENT if ( l == 0 ) : NEW_LINE INDENT return suffix [ r + 1 ] NEW_LINE DEDENT if ( r == n - 1 ) : NEW_LINE INDENT return prefix [ l - 1 ] NEW_LINE DEDENT return GCD ( prefix [ l - 1 ] , suffix [ r + 1 ] ) NEW_LINE DEDENT arr = [ 2 , 6 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE prefix = [ ] NEW_LINE suffix = [ ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT prefix . append ( 0 ) NEW_LINE suffix . append ( 0 ) NEW_LINE DEDENT FillPrefixSuffix ( prefix , arr , suffix , n ) NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE print ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) NEW_LINE l = 1 NEW_LINE r = 1 NEW_LINE print ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) NEW_LINE l = 1 NEW_LINE r = 2 NEW_LINE print ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) NEW_LINE
Count of pairs from arrays A and B such that element in A is greater than element in B at that index | Python3 program to find the maximum count of values that follow the given condition ; Function to find the maximum count of values that follow the given condition ; Initializing the max - heap for the array A [ ] ; Adding the values of A [ ] into max heap ; Adding the values of B [ ] into max heap ; Counter variable ; Loop to iterate through the heap ; Comparing the values at the top . If the value of heap A [ ] is greater , then counter is incremented ; Driver code
import heapq NEW_LINE def check ( A , B , N ) : NEW_LINE INDENT pq1 = [ ] NEW_LINE pq2 = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT heapq . heappush ( pq1 , - A [ i ] ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT heapq . heappush ( pq2 , - B [ i ] ) NEW_LINE DEDENT c = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if - pq1 [ 0 ] > - pq2 [ 0 ] : NEW_LINE INDENT c += 1 NEW_LINE heapq . heappop ( pq1 ) NEW_LINE heapq . heappop ( pq2 ) NEW_LINE DEDENT else : NEW_LINE INDENT if len ( pq2 ) == 0 : NEW_LINE INDENT break NEW_LINE DEDENT heapq . heappop ( pq2 ) NEW_LINE DEDENT DEDENT return ( c ) NEW_LINE DEDENT A = [ 10 , 3 , 7 , 5 , 8 ] NEW_LINE B = [ 8 , 6 , 2 , 5 , 9 ] NEW_LINE N = len ( A ) NEW_LINE print ( check ( A , B , N ) ) NEW_LINE
Number of elements less than or equal to a given number in a given subarray | Set 2 ( Including Updates ) | Number of elements less than or equal to a given number in a given subarray and allowing update operations . ; updating the bit array of a valid block ; answering the query ; traversing the first block in range ; Traversing completely overlapped blocks in range for such blocks bit array of that block is queried ; Traversing the last block ; Preprocessing the array ; updating the bit array at the original and new value of array ; Driver Code ; size of block size will be equal to square root of n ; initialising bit array of each block as elements of array cannot exceed 10 ^ 4 so size of bit array is accordingly
MAX = 10001 NEW_LINE def update ( idx , blk , val , bit ) : NEW_LINE INDENT while idx < MAX : NEW_LINE INDENT bit [ blk ] [ idx ] += val NEW_LINE idx += ( idx & - idx ) NEW_LINE DEDENT DEDENT def query ( l , r , k , arr , blk_sz , bit ) : NEW_LINE INDENT summ = 0 NEW_LINE while l < r and l % blk_sz != 0 and l != 0 : NEW_LINE INDENT if arr [ l ] <= k : NEW_LINE INDENT summ += 1 NEW_LINE DEDENT l += 1 NEW_LINE DEDENT while l + blk_sz <= r : NEW_LINE INDENT idx = k NEW_LINE while idx > 0 : NEW_LINE INDENT summ += bit [ l // blk_sz ] [ idx ] NEW_LINE idx -= ( idx & - idx ) NEW_LINE DEDENT l += blk_sz NEW_LINE DEDENT while l <= r : NEW_LINE INDENT if arr [ l ] <= k : NEW_LINE INDENT summ += 1 NEW_LINE DEDENT l += 1 NEW_LINE DEDENT return summ NEW_LINE DEDENT def preprocess ( arr , blk_sz , n , bit ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT update ( arr [ i ] , i // blk_sz , 1 , bit ) NEW_LINE DEDENT DEDENT def preprocessUpdate ( i , v , blk_sz , arr , bit ) : NEW_LINE INDENT update ( arr [ i ] , i // blk_sz , - 1 , bit ) NEW_LINE update ( v , i // blk_sz , 1 , bit ) NEW_LINE arr [ i ] = v NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE from math import sqrt NEW_LINE blk_sz = int ( sqrt ( n ) ) NEW_LINE bit = [ [ 0 for i in range ( MAX ) ] for j in range ( blk_sz + 1 ) ] NEW_LINE preprocess ( arr , blk_sz , n , bit ) NEW_LINE print ( query ( 1 , 3 , 1 , arr , blk_sz , bit ) ) NEW_LINE preprocessUpdate ( 3 , 10 , blk_sz , arr , bit ) NEW_LINE print ( query ( 3 , 3 , 4 , arr , blk_sz , bit ) ) NEW_LINE preprocessUpdate ( 2 , 1 , blk_sz , arr , bit ) NEW_LINE preprocessUpdate ( 0 , 2 , blk_sz , arr , bit ) NEW_LINE print ( query ( 0 , 4 , 5 , arr , blk_sz , bit ) ) NEW_LINE DEDENT
Queries for counts of array elements with values in given range | function to count elements within given range ; initialize result ; check if element is in range ; driver function ; Answer queries
def countInRange ( arr , n , x , y ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] >= x and arr [ i ] <= y ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 3 , 4 , 9 , 10 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE i = 1 NEW_LINE j = 4 NEW_LINE print ( countInRange ( arr , n , i , j ) ) NEW_LINE i = 9 NEW_LINE j = 12 NEW_LINE print ( countInRange ( arr , n , i , j ) ) NEW_LINE
Path with smallest product of edges with weight >= 1 | Function to return the smallest product of edges ; If the source is equal to the destination ; Initialise the priority queue ; Visited array ; While the priority - queue is not empty ; Current node ; Current product of distance ; Popping the top - most element ; If already visited continue ; Marking the node as visited ; If it is a destination node ; Traversing the current node ; If no path exists ; Driver code ; Graph as adjacency matrix ; Input edges ; print ( gr ) ; Source and destination ; Dijkstra
def dijkstra ( s , d , gr ) : NEW_LINE INDENT if ( s == d ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT pq = [ ] ; NEW_LINE pq . append ( ( 1 , s ) ) ; NEW_LINE v = [ 0 ] * ( len ( gr ) + 1 ) ; NEW_LINE while ( len ( pq ) != 0 ) : NEW_LINE INDENT curr = pq [ 0 ] [ 1 ] ; NEW_LINE dist = pq [ 0 ] [ 0 ] ; NEW_LINE pq . pop ( ) ; NEW_LINE if ( v [ curr ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT v [ curr ] = 1 ; NEW_LINE if ( curr == d ) : NEW_LINE INDENT return dist ; NEW_LINE DEDENT for it in gr [ curr ] : NEW_LINE INDENT if it not in pq : NEW_LINE INDENT pq . insert ( 0 , ( dist * it [ 1 ] , it [ 0 ] ) ) ; NEW_LINE DEDENT DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE gr = { } ; NEW_LINE gr [ 1 ] = [ ( 3 , 9 ) ] ; NEW_LINE gr [ 2 ] = [ ( 3 , 1 ) ] ; NEW_LINE gr [ 1 ] . append ( ( 2 , 5 ) ) ; NEW_LINE gr [ 3 ] = [ ] ; NEW_LINE s = 1 ; d = 3 ; NEW_LINE print ( dijkstra ( s , d , gr ) ) ; NEW_LINE DEDENT
Queries for counts of array elements with values in given range | function to find first index >= x ; function to find last index <= x ; function to count elements within given range ; initialize result ; driver function ; Preprocess array ; Answer queries
def lowerIndex ( arr , n , x ) : NEW_LINE INDENT l = 0 NEW_LINE h = n - 1 NEW_LINE while ( l <= h ) : NEW_LINE INDENT mid = int ( ( l + h ) / 2 ) NEW_LINE if ( arr [ mid ] >= x ) : NEW_LINE h = mid - 1 NEW_LINE else : NEW_LINE l = mid + 1 NEW_LINE DEDENT return l NEW_LINE DEDENT def upperIndex ( arr , n , x ) : NEW_LINE INDENT l = 0 NEW_LINE h = n - 1 NEW_LINE while ( l <= h ) : NEW_LINE INDENT mid = int ( ( l + h ) / 2 ) NEW_LINE if ( arr [ mid ] <= x ) : NEW_LINE l = mid + 1 NEW_LINE else : NEW_LINE h = mid - 1 NEW_LINE DEDENT return h NEW_LINE DEDENT def countInRange ( arr , n , x , y ) : NEW_LINE INDENT count = 0 ; NEW_LINE count = upperIndex ( arr , n , y ) - lowerIndex ( arr , n , x ) + 1 ; NEW_LINE return count NEW_LINE DEDENT arr = [ 1 , 3 , 4 , 9 , 10 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE arr . sort ( ) NEW_LINE i = 1 NEW_LINE j = 4 NEW_LINE print ( countInRange ( arr , n , i , j ) ) NEW_LINE i = 9 NEW_LINE j = 12 NEW_LINE print ( countInRange ( arr , n , i , j ) ) NEW_LINE
Queries for decimal values of subarrays of a binary array | implementation of finding number represented by binary subarray ; Fills pre [ ] ; returns the number represented by a binary subarray l to r ; if r is equal to n - 1 r + 1 does not exist ; Driver Code
from math import pow NEW_LINE def precompute ( arr , n , pre ) : NEW_LINE INDENT pre [ n - 1 ] = arr [ n - 1 ] * pow ( 2 , 0 ) NEW_LINE i = n - 2 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT pre [ i ] = ( pre [ i + 1 ] + arr [ i ] * ( 1 << ( n - 1 - i ) ) ) NEW_LINE i -= 1 NEW_LINE DEDENT DEDENT def decimalOfSubarr ( arr , l , r , n , pre ) : NEW_LINE INDENT if ( r != n - 1 ) : NEW_LINE INDENT return ( ( pre [ l ] - pre [ r + 1 ] ) / ( 1 << ( n - 1 - r ) ) ) NEW_LINE DEDENT return pre [ l ] / ( 1 << ( n - 1 - r ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 1 , 0 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE pre = [ 0 for i in range ( n ) ] NEW_LINE precompute ( arr , n , pre ) NEW_LINE print ( int ( decimalOfSubarr ( arr , 2 , 4 , n , pre ) ) ) NEW_LINE print ( int ( decimalOfSubarr ( arr , 4 , 5 , n , pre ) ) ) NEW_LINE DEDENT
Count elements which divide all numbers in range L | function to count element Time complexity O ( n ^ 2 ) worst case ; answer for query ; 0 based index ; iterate for all elements ; check if the element divides all numbers in range ; no of elements ; if all elements are divisible by a [ i ] ; answer for every query ; Driver Code
def answerQuery ( a , n , l , r ) : NEW_LINE INDENT count = 0 NEW_LINE l = l - 1 NEW_LINE for i in range ( l , r , 1 ) : NEW_LINE INDENT element = a [ i ] NEW_LINE divisors = 0 NEW_LINE for j in range ( l , r , 1 ) : NEW_LINE INDENT if ( a [ j ] % a [ i ] == 0 ) : NEW_LINE INDENT divisors += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( divisors == ( r - l ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 2 , 3 , 5 ] NEW_LINE n = len ( a ) NEW_LINE l = 1 NEW_LINE r = 4 NEW_LINE print ( answerQuery ( a , n , l , r ) ) NEW_LINE l = 2 NEW_LINE r = 4 NEW_LINE print ( answerQuery ( a , n , l , r ) ) NEW_LINE DEDENT