codeparrot/xlcost-text-to-code huggingface dataset:
Count positions in Binary Matrix having equal count of set bits in corresponding row and column | Function to return the count of indices in from the given binary matrix having equal count of set bits in its row and column ; Stores count of set bits in corresponding column and row ; Traverse matrix ; Since 1 contains a set bit ; Update count of set bits for current row and col ; Stores the count of required indices ; Traverse matrix ; If current row and column has equal count of set bits ; Return count of required position ; Driver Code
def countPosition ( mat ) : NEW_LINE INDENT n = len ( mat ) NEW_LINE m = len ( mat [ 0 ] ) NEW_LINE row = [ 0 ] * n NEW_LINE col = [ 0 ] * m NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == 1 ) : NEW_LINE INDENT col [ j ] += 1 NEW_LINE row [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( row [ i ] == col [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT mat = [ [ 0 , 1 ] , [ 1 , 1 ] ] NEW_LINE print ( countPosition ( mat ) ) NEW_LINE
Maximum value at each level in an N | Function to find the maximum value at each level of N - ary tree ; Stores the adjacency list ; Create the adjacency list ; Perform level order traversal of nodes at each level ; Push the root node ; Iterate until queue is empty ; Get the size of queue ; Iterate for : all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue the children of dequeued node ; Print the result ; Driver Code ; Number of nodes ; Edges of the N - ary tree ; Given cost ; Function Call
def maxAtLevel ( N , M , Value , Edges ) : NEW_LINE INDENT adj = [ [ ] for i in range ( N ) ] NEW_LINE for i in range ( M ) : NEW_LINE INDENT u = Edges [ i ] [ 0 ] NEW_LINE v = Edges [ i ] [ 1 ] NEW_LINE adj [ u ] . append ( v ) NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( 0 ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT count = len ( q ) NEW_LINE maxVal = 0 NEW_LINE while ( count ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . remove ( q [ 0 ] ) NEW_LINE maxVal = max ( maxVal , Value [ temp ] ) NEW_LINE for i in range ( len ( adj [ temp ] ) ) : NEW_LINE INDENT q . append ( adj [ temp ] [ i ] ) NEW_LINE DEDENT count -= 1 NEW_LINE DEDENT print ( maxVal , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE Edges = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 0 , 3 ] , [ 1 , 4 ] , [ 1 , 5 ] , [ 3 , 6 ] , [ 6 , 7 ] , [ 6 , 8 ] , [ 6 , 9 ] ] NEW_LINE Value = [ 1 , 2 , - 1 , 3 , 4 , 5 , 8 , 6 , 12 , 7 ] NEW_LINE maxAtLevel ( N , N - 1 , Value , Edges ) NEW_LINE DEDENT
Rearrange array to maximize count of triplets ( i , j , k ) such that arr [ i ] > arr [ j ] < arr [ k ] and i < j < k | Function to rearrange the array to maximize the count of triplets ; Sort the given array ; Stores the permutations of the given array ; Stores the index of the given array ; Place the first half of the given array ; Place the last half of the given array ; Stores the count of triplets ; Check the given conditions ; Driver Code
def ctTriplets ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE temp = [ 0 ] * N NEW_LINE index = 0 NEW_LINE for i in range ( 1 , N , 2 ) : NEW_LINE INDENT temp [ i ] = arr [ index ] NEW_LINE index += 1 NEW_LINE DEDENT for i in range ( 0 , N , 2 ) : NEW_LINE INDENT temp [ i ] = arr [ index ] NEW_LINE index += 1 NEW_LINE DEDENT ct = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( i > 0 and i + 1 < N ) : NEW_LINE INDENT if ( temp [ i ] < temp [ i + 1 ] and temp [ i ] < temp [ i - 1 ] ) : NEW_LINE INDENT ct += 1 NEW_LINE DEDENT DEDENT DEDENT print ( " Count ▁ of ▁ triplets : " , ct ) NEW_LINE print ( " Array : " , end = " " ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( temp [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE ctTriplets ( arr , N ) NEW_LINE
Rearrange array to make it non | Python3 program for above approach ; Function to check if the array can be made non - decreasing ; Iterate till N ; Find the minimum element ; Sort the array ; Iterate till N ; Check if the element is at its correct position ; Return the answer ; Driver Code ; Print the answer
import sys NEW_LINE def check ( a , n ) : NEW_LINE INDENT b = [ None ] * n NEW_LINE minElement = sys . maxsize NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT b [ i ] = a [ i ] NEW_LINE minElement = min ( minElement , a [ i ] ) NEW_LINE DEDENT b . sort ( ) NEW_LINE k = 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( ( a [ i ] != b [ i ] ) and ( a [ i ] % minElement != 0 ) ) : NEW_LINE INDENT k = 0 NEW_LINE break NEW_LINE DEDENT DEDENT if k == 1 : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 4 , 3 , 6 , 6 , 2 , 9 ] NEW_LINE n = len ( a ) NEW_LINE if check ( a , n ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count 1 s in binary matrix having remaining indices of its row and column filled with 0 s | Function to count required 1 s from the given matrix ; Stores the dimensions of the mat ; Calculate sum of rows ; Calculate sum of columns ; Stores required count of 1 s ; If current cell is 1 and sum of row and column is 1 ; Increment count of 1 s ; Return the final count ; Driver Code ; Given matrix ; Function call
def numSpecial ( mat ) : NEW_LINE INDENT m = len ( mat ) NEW_LINE n = len ( mat [ 0 ] ) NEW_LINE rows = [ 0 ] * m NEW_LINE cols = [ 0 ] * n NEW_LINE i , j = 0 , 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT rows [ i ] = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT rows [ i ] += mat [ i ] [ j ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT cols [ i ] = 0 NEW_LINE for j in range ( m ) : NEW_LINE INDENT cols [ i ] += mat [ j ] [ i ] NEW_LINE DEDENT DEDENT cnt = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == 1 and rows [ i ] == 1 and cols [ j ] == 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 0 , 0 , 0 ] ] NEW_LINE print ( numSpecial ( mat ) ) NEW_LINE DEDENT
Find all matrix elements which are minimum in their row and maximum in their column | Python3 program for the above approach ; Functionto find all the matrix elements which are minimum in its row and maximum in its column ; Initialize unordered set ; Traverse the matrix ; Update the minimum element of current row ; Insert the minimum element of the row ; Update the maximum element of current column ; Checking if it is already present in the unordered_set or not ; Driver Code ; Function call ; If no such matrix element is found
import sys NEW_LINE def minmaxNumbers ( matrix , res ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( 0 , len ( matrix ) , 1 ) : NEW_LINE INDENT minr = sys . maxsize NEW_LINE for j in range ( 0 , len ( matrix [ i ] ) , 1 ) : NEW_LINE INDENT minr = min ( minr , matrix [ i ] [ j ] ) NEW_LINE DEDENT s . add ( minr ) NEW_LINE DEDENT for j in range ( 0 , len ( matrix [ 0 ] ) , 1 ) : NEW_LINE INDENT maxc = - sys . maxsize - 1 NEW_LINE for i in range ( 0 , len ( matrix ) , 1 ) : NEW_LINE INDENT maxc = max ( maxc , matrix [ i ] [ j ] ) NEW_LINE DEDENT if ( maxc in s ) : NEW_LINE INDENT res . append ( maxc ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 10 , 4 ] , [ 9 , 3 , 8 ] , [ 15 , 16 , 17 ] ] NEW_LINE ans = [ ] NEW_LINE minmaxNumbers ( mat , ans ) NEW_LINE if ( len ( ans ) == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] ) NEW_LINE DEDENT DEDENT
Node whose removal minimizes the maximum size forest from an N | Python3 program to implement the above approach ; Function to create the graph ; Function to traverse the graph and find the minimum of maximum size forest after removing a Node ; Traversing every child subtree except the parent Node ; Traverse all subtrees ; Update the maximum size of forests ; Update the minimum of maximum size of forests obtained ; Condition to find the minimum of maximum size forest ; Update and store the corresponding Node ; Driver Code
mini = 105 ; NEW_LINE ans = 0 ; NEW_LINE n = 0 ; NEW_LINE g = [ ] ; NEW_LINE size = [ 0 ] * 100 ; NEW_LINE def create_graph ( ) : NEW_LINE INDENT g [ 1 ] . append ( 2 ) ; NEW_LINE g [ 2 ] . append ( 1 ) ; NEW_LINE g [ 1 ] . append ( 3 ) ; NEW_LINE g [ 3 ] . append ( 1 ) ; NEW_LINE g [ 1 ] . append ( 4 ) ; NEW_LINE g [ 4 ] . append ( 1 ) ; NEW_LINE g [ 2 ] . append ( 5 ) ; NEW_LINE g [ 5 ] . append ( 2 ) ; NEW_LINE g [ 2 ] . append ( 6 ) ; NEW_LINE g [ 6 ] . append ( 2 ) ; NEW_LINE DEDENT def dfs ( Node , parent ) : NEW_LINE INDENT size [ Node ] = 1 ; NEW_LINE mx = 0 ; NEW_LINE global mini NEW_LINE global ans NEW_LINE for y in g [ Node ] : NEW_LINE INDENT if ( y == parent ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT dfs ( y , Node ) ; NEW_LINE size [ Node ] += size [ y ] ; NEW_LINE mx = max ( mx , size [ y ] ) ; NEW_LINE DEDENT mx = max ( mx , n - size [ Node ] ) ; NEW_LINE if ( mx < mini ) : NEW_LINE INDENT mini = mx ; NEW_LINE ans = Node ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 ; NEW_LINE for i in range ( 100 ) : NEW_LINE INDENT g . append ( [ ] ) NEW_LINE DEDENT create_graph ( ) ; NEW_LINE dfs ( 1 , - 1 ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT
Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array | Function to find the minimum length of prefix required to be deleted ; Stores index to be returned ; Iterate until previous element is <= current index ; Decrementing index ; Return index ; Driver code ; Given arr ; Function call
def findMinLength ( arr ) : NEW_LINE INDENT index = len ( arr ) - 1 ; NEW_LINE while ( index > 0 and arr [ index ] >= arr [ index - 1 ] ) : NEW_LINE INDENT index -= 1 ; NEW_LINE DEDENT return index ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 8 , 5 , 0 , - 1 , - 1 , 0 , 1 , 2 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findMinLength ( arr ) ) ; NEW_LINE DEDENT
Count array elements exceeding sum of preceding K elements | Function to count array elements exceeding sum of preceding K elements ; Iterate over the array ; Update prefix sum ; Check if arr [ K ] > arr [ 0 ] + . . + arr [ K - 1 ] ; Increment count ; Check if arr [ i ] > arr [ i - K - 1 ] + . . + arr [ i - 1 ] ; Increment count ; Driver Code ; Given array
def countPrecedingK ( a , n , K ) : NEW_LINE INDENT prefix = [ 0 ] * n NEW_LINE prefix [ 0 ] = a [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefix [ i ] = prefix [ i - 1 ] + a [ i ] NEW_LINE DEDENT ctr = 0 NEW_LINE if ( prefix [ K - 1 ] < a [ K ] ) : NEW_LINE INDENT ctr += 1 NEW_LINE DEDENT for i in range ( K + 1 , n ) : NEW_LINE INDENT if ( prefix [ i - 1 ] - prefix [ i - K - 1 ] < a [ i ] ) : NEW_LINE INDENT ctr += 1 NEW_LINE DEDENT DEDENT return ctr NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 8 , 10 , - 2 , 7 , 5 , 5 , 9 , 15 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE print ( countPrecedingK ( arr , N , K ) ) NEW_LINE DEDENT
Count minimum moves required to convert A to B | Function to find minimum number of moves to obtained B from A ; Stores the minimum number of moves ; Absolute difference ; K is in range [ 0 , 10 ] ; Print the required moves ; Driver Code
def convertBfromA ( a , b ) : NEW_LINE INDENT moves = 0 NEW_LINE x = abs ( a - b ) NEW_LINE for i in range ( 10 , 0 , - 1 ) : NEW_LINE INDENT moves += x // i NEW_LINE x = x % i NEW_LINE DEDENT print ( moves , end = " ▁ " ) NEW_LINE DEDENT A = 188 NEW_LINE B = 4 NEW_LINE convertBfromA ( A , B ) NEW_LINE
Count N digits numbers with sum divisible by K | Function to count the N digit numbers whose sum is divisible by K ; Base case ; If already computed subproblem occurred ; Store the count of N digit numbers whoe sum is divisible by K ; Check if the number does not contain any leading 0. ; Recurrence relation ; Driver code ; Stores the values of overlapping subproblems
def countNum ( N , sum , K , st , dp ) : NEW_LINE INDENT if ( N == 0 and sum == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( N < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ N ] [ sum ] [ st ] != - 1 ) : NEW_LINE INDENT return dp [ N ] [ sum ] [ st ] NEW_LINE DEDENT res = 0 NEW_LINE start = 1 NEW_LINE if ( st == 1 ) : NEW_LINE INDENT start = 0 NEW_LINE DEDENT else : NEW_LINE INDENT start = 1 NEW_LINE DEDENT for i in range ( start , 10 ) : NEW_LINE INDENT min = 0 NEW_LINE if ( ( st i ) > 0 ) : NEW_LINE INDENT min = 1 NEW_LINE DEDENT else : NEW_LINE INDENT min = 0 NEW_LINE DEDENT res += countNum ( N - 1 , ( sum + i ) % K , K , min , dp ) NEW_LINE dp [ N ] [ sum ] [ st ] = res NEW_LINE DEDENT return dp [ N ] [ sum ] [ st ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE K = 7 NEW_LINE M = 100 NEW_LINE dp = [ [ [ - 1 for i in range ( 2 ) ] for j in range ( M ) ] for j in range ( M ) ] NEW_LINE print ( countNum ( N , 0 , K , 0 , dp ) ) NEW_LINE DEDENT
Minimize Nth term of an Arithmetic progression ( AP ) | Python3 program to implement the above approach ; Function to find the smallest Nth term of an AP possible ; Stores the smallest Nth term ; Check if common difference of AP is an integer ; Store the common difference ; Store the First Term of that AP ; Store the Nth term of that AP ; Check if all elements of an AP are positive ; Return the least Nth term obtained ; Driver Code
import sys NEW_LINE def smallestNth ( A , B , N ) : NEW_LINE INDENT res = sys . maxsize NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( N , i , - 1 ) : NEW_LINE INDENT if ( ( B - A ) % ( j - i ) == 0 ) : NEW_LINE INDENT D = ( B - A ) // ( j - i ) NEW_LINE FirstTerm = A - ( i - 1 ) * D NEW_LINE NthTerm = FirstTerm + ( N - 1 ) * D NEW_LINE if ( FirstTerm > 0 ) : NEW_LINE INDENT res = min ( res , NthTerm ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE A = 1 NEW_LINE B = 6 NEW_LINE print ( smallestNth ( A , B , N ) ) NEW_LINE DEDENT
Subarray of size K with prime sum | Generate all prime numbers in the range [ 1 , 1000000 ] ; Set all numbers as prime initially ; Mark 0 and 1 as non - prime ; If current element is prime ; Mark all its multiples as non - prime ; Function to prthe subarray whose sum of elements is prime ; Store the current subarray of size K ; Calculate the sum of first K elements ; Check if currSum is prime ; Store the start and last index of subarray of size K ; Iterate over remaining array ; Check if currSum ; Driver Code
def sieve ( prime ) : NEW_LINE INDENT for i in range ( 1000000 ) : NEW_LINE INDENT prime [ i ] = True NEW_LINE DEDENT prime [ 0 ] = prime [ 1 ] = False NEW_LINE for i in range ( 2 , 1000 + 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , 1000000 , i ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT return prime NEW_LINE DEDENT def subPrimeSum ( N , K , arr , prime ) : NEW_LINE INDENT currSum = 0 NEW_LINE for i in range ( 0 , K ) : NEW_LINE INDENT currSum += arr [ i ] NEW_LINE DEDENT if ( prime [ currSum ] ) : NEW_LINE INDENT for i in range ( K ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT st = 1 NEW_LINE en = K NEW_LINE while ( en < N ) : NEW_LINE INDENT currSum += arr [ en ] - arr [ st - 1 ] NEW_LINE if ( prime [ currSum ] ) : NEW_LINE INDENT for i in range ( st , en + 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT en += 1 NEW_LINE st += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 20 , 7 , 5 , 4 , 3 , 11 , 99 , 87 , 23 , 45 ] NEW_LINE K = 4 NEW_LINE N = len ( arr ) NEW_LINE prime = [ False ] * 1000000 NEW_LINE prime = sieve ( prime ) NEW_LINE subPrimeSum ( N , K , arr , prime ) NEW_LINE DEDENT
Minimum characters required to be removed to sort binary string in ascending order | Function to find the minimum count of characters to be removed to make the string sorted in ascending order ; Length of given string ; Stores the first occurrence of '1 ; Stores the last occurrence of '0 ; Traverse the string to find the first occurrence of '1 ; Traverse the string to find the last occurrence of '0 ; Return 0 if the str have only one type of character ; Initialize count1 and count0 to count '1' s before lastIdx0 and '0' s after firstIdx1 ; Traverse the string to count0 ; Traverse the string to count1 ; Return the minimum of count0 and count1 ; Given string str ; Function call
def minDeletion ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE DEDENT ' NEW_LINE INDENT firstIdx1 = - 1 NEW_LINE DEDENT ' NEW_LINE INDENT lastIdx0 = - 1 NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( str [ i ] == '1' ) : NEW_LINE INDENT firstIdx1 = i NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT lastIdx0 = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( firstIdx1 == - 1 or lastIdx0 == - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT count1 = 0 NEW_LINE count0 = 0 NEW_LINE for i in range ( 0 , lastIdx0 ) : NEW_LINE INDENT if ( str [ i ] == '1' ) : NEW_LINE INDENT count1 += 1 NEW_LINE DEDENT DEDENT for i in range ( firstIdx1 + 1 , n ) : NEW_LINE INDENT if ( str [ i ] == '1' ) : NEW_LINE INDENT count0 += 1 NEW_LINE DEDENT DEDENT return min ( count0 , count1 ) NEW_LINE DEDENT str = "1000101" NEW_LINE print ( minDeletion ( str ) ) NEW_LINE
Maximum score assigned to a subsequence of numerically consecutive and distinct array elements | Function to find the maximum score with unique element in the subset ; Base Case ; Check if the previously picked element differs by 1 from the current element ; Calculate score by including the current element ; Calculate score by excluding the current element ; Return maximum of the two possibilities ; Driver Code ; Given arrays ; Function call
def maximumSum ( a , b , n , index , lastpicked ) : NEW_LINE INDENT if ( index == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT option1 = 0 NEW_LINE option2 = 0 NEW_LINE if ( lastpicked == - 1 or a [ lastpicked ] != a [ index ] ) : NEW_LINE INDENT option1 = b [ index ] + maximumSum ( a , b , n , index + 1 , index ) NEW_LINE DEDENT option2 = maximumSum ( a , b , n , index + 1 , lastpicked ) NEW_LINE return max ( option1 , option2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 3 , 3 , 1 ] NEW_LINE brr = [ - 1 , 2 , 10 , 20 , - 10 , - 9 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maximumSum ( arr , brr , N , 0 , - 1 ) ) NEW_LINE DEDENT
Replace every element in a circular array by sum of next K elements | Function to print required resultant array ; Reverse the array ; Traverse the range ; Store prefix sum ; Find the prefix sum ; Store the answer ; Calculate the answers ; Count of remaining elements ; Add the sum of all elements y times ; Add the remaining elements ; Update ans [ i ] ; If array is reversed print ans in reverse ; Driver Code ; Given array arr ; Given K ; Function
def sumOfKElements ( arr , n , k ) : NEW_LINE INDENT rev = False ; NEW_LINE if ( k < 0 ) : NEW_LINE INDENT rev = True ; NEW_LINE k *= - 1 ; NEW_LINE l = 0 ; NEW_LINE r = n - 1 ; NEW_LINE while ( l < r ) : NEW_LINE INDENT tmp = arr [ l ] ; NEW_LINE arr [ l ] = arr [ r ] ; NEW_LINE arr [ r ] = tmp ; NEW_LINE l += 1 ; NEW_LINE r -= 1 ; NEW_LINE DEDENT DEDENT dp = [ 0 ] * n ; NEW_LINE dp [ 0 ] = arr [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] += dp [ i - 1 ] + arr [ i ] ; NEW_LINE DEDENT ans = [ 0 ] * n ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i + k < n ) : NEW_LINE INDENT ans [ i ] = dp [ i + k ] - dp [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT x = k - ( n - 1 - i ) ; NEW_LINE y = x // n ; NEW_LINE rem = x % n ; NEW_LINE ans [ i ] = ( dp [ n - 1 ] - dp [ i ] + y * dp [ n - 1 ] + ( dp [ rem - 1 ] if rem - 1 >= 0 else 0 ) ) ; NEW_LINE DEDENT DEDENT if ( rev ) : NEW_LINE INDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 2 , - 5 , 11 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 3 ; NEW_LINE sumOfKElements ( arr , N , K ) ; NEW_LINE DEDENT
Count subarrays for every array element in which they are the minimum | Function to find the boundary of every element within which it is minimum ; Perform Binary Search ; Find mid m ; Update l ; Update r ; Inserting the index ; Function to required count subarrays ; Stores the indices of element ; Initialize the output array ; Left boundary , till the element is smallest ; Right boundary , till the element is smallest ; Calculate the number of subarrays based on its boundary ; Adding cnt to the ans ; Driver Code ; Given array arr [ ] ; Function Call
def binaryInsert ( boundary , i ) : NEW_LINE INDENT l = 0 NEW_LINE r = len ( boundary ) - 1 NEW_LINE while l <= r : NEW_LINE INDENT m = ( l + r ) // 2 NEW_LINE if boundary [ m ] < i : NEW_LINE INDENT l = m + 1 NEW_LINE DEDENT else : NEW_LINE INDENT r = m - 1 NEW_LINE DEDENT DEDENT boundary . insert ( l , i ) NEW_LINE return l NEW_LINE DEDENT def countingSubarray ( arr , n ) : NEW_LINE INDENT index = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT index [ arr [ i ] ] = i NEW_LINE DEDENT boundary = [ - 1 , n ] NEW_LINE arr . sort ( ) NEW_LINE ans = [ 0 for i in range ( n ) ] NEW_LINE for num in arr : NEW_LINE INDENT i = binaryInsert ( boundary , index [ num ] ) NEW_LINE l = boundary [ i ] - boundary [ i - 1 ] - 1 NEW_LINE r = boundary [ i + 1 ] - boundary [ i ] - 1 NEW_LINE cnt = l + r + l * r + 1 NEW_LINE ans [ index [ num ] ] += cnt NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 5 NEW_LINE arr = [ 3 , 2 , 4 , 1 , 5 ] NEW_LINE print ( countingSubarray ( arr , N ) ) NEW_LINE