codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum of remaining elements after every removal of the array half with greater sum | Function to find the maximum sum ; Base Case ; Create a key to map the values ; Check if mapped key is found in the dictionary ; Traverse the array ; Store left prefix sum ; Store right prefix sum ; Compare the left and right values ; Store the value in dp array ; Return the final answer ; Function to print maximum sum ; Stores prefix sum ; Store results of subproblems ; Traversing the array ; Add prefix sum of array ; Print the answer ; Driver Code ; Function Call
def maxweight ( s , e , pre , dp ) : NEW_LINE INDENT if s == e : NEW_LINE INDENT return 0 NEW_LINE DEDENT key = ( s , e ) NEW_LINE if key in dp : NEW_LINE INDENT return dp [ key ] NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( s , e ) : NEW_LINE INDENT left = pre [ i ] - pre [ s - 1 ] NEW_LINE right = pre [ e ] - pre [ i ] NEW_LINE if left < right : NEW_LINE INDENT ans = max ( ans , left + maxweight ( s , i , pre , dp ) ) NEW_LINE DEDENT if left == right : NEW_LINE INDENT ans = max ( ans , left + maxweight ( s , i , pre , dp ) , right + maxweight ( i + 1 , e , pre , dp ) ) NEW_LINE DEDENT if left > right : NEW_LINE ans = max ( ans , right + maxweight ( i + 1 , e , pre , dp ) ) NEW_LINE dp [ key ] = ans NEW_LINE DEDENT return dp [ key ] NEW_LINE DEDENT def maxSum ( arr ) : NEW_LINE INDENT pre = { - 1 : 0 , 0 : arr [ 0 ] } NEW_LINE dp = { } NEW_LINE for i in range ( 1 , len ( arr ) ) : NEW_LINE INDENT pre [ i ] = pre [ i - 1 ] + arr [ i ] NEW_LINE DEDENT print ( maxweight ( 0 , len ( arr ) - 1 , pre , dp ) ) NEW_LINE DEDENT arr = [ 6 , 2 , 3 , 4 , 5 , 5 ] NEW_LINE maxSum ( arr ) NEW_LINE
Find K smallest leaf nodes from a given Binary Tree | Binary tree node ; Function to create new node ; Utility function which calculates smallest three nodes of all leaf nodes ; Check if current root is a leaf node ; Traverse the left and right subtree ; Function to find the K smallest nodes of the Binary Tree ; Sorting the Leaf nodes array ; Loop to print the K smallest Leaf nodes of the array ; Driver Code ; Construct binary 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 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 storeLeaf ( root : Node , arr : list ) -> None : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( not root . left and not root . right ) : NEW_LINE INDENT arr . append ( root . data ) NEW_LINE return NEW_LINE DEDENT storeLeaf ( root . left , arr ) NEW_LINE storeLeaf ( root . right , arr ) NEW_LINE DEDENT def KSmallest ( root : Node , k : int ) -> None : NEW_LINE INDENT arr = [ ] NEW_LINE storeLeaf ( root , arr ) NEW_LINE arr . sort ( ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT if ( i < len ( arr ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . left . left = Node ( 21 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . left . right . right = Node ( 8 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . right . right . right = Node ( 19 ) NEW_LINE KSmallest ( root , 3 ) NEW_LINE DEDENT
Check if all the pairs of an array are coprime with each other | Python3 implementation of the above approach ; Function to check if all the pairs of the array are coprime with each other or not ; Check if GCD of the pair is not equal to 1 ; All pairs are non - coprime Return false ;  ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( b , a % b ) NEW_LINE DEDENT DEDENT def allCoprime ( A , n ) : NEW_LINE INDENT all_coprime = True NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if gcd ( A [ i ] , A [ j ] ) != 1 : NEW_LINE INDENT all_coprime = False ; NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT return all_coprime NEW_LINE DEDENT / * Function return gcd of two number * / NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 5 , 11 , 7 , 19 ] NEW_LINE arr_size = len ( A ) NEW_LINE if ( allCoprime ( A , arr_size ) ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT
Smallest power of 2 consisting of N digits | Python3 program of the above approach ; Function to return smallest power of 2 with N digits ; Driver Code
from math import log2 , ceil NEW_LINE def smallestNum ( n ) : NEW_LINE INDENT power = log2 ( 10 ) NEW_LINE print ( power ) ; NEW_LINE return ceil ( ( n - 1 ) * power ) NEW_LINE DEDENT n = 4 NEW_LINE print ( smallestNum ( n ) ) NEW_LINE
Find a valid parenthesis sequence of length K from a given valid parenthesis sequence | Function to find the subsequence of length K forming valid sequence ; Stores the resultant string ; Check whether character at index i is visited or not ; Traverse the string ; Push index of open bracket ; Pop and mark visited ; Increment count by 2 ; Append the characters and create the resultant string ; Return the resultant string ; Driver Code ; Function call
def findString ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = " " NEW_LINE st = [ ] NEW_LINE vis = [ False ] * n NEW_LINE count = 0 NEW_LINE List < bool > vis ( n , false ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' ( ' ) : NEW_LINE INDENT st . append ( i ) NEW_LINE DEDENT if ( count < k and s [ i ] == ' ) ' ) : NEW_LINE INDENT vis [ st [ - 1 ] ] = 1 NEW_LINE del st [ - 1 ] NEW_LINE vis [ i ] = True NEW_LINE count += 2 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( vis [ i ] == True ) : NEW_LINE INDENT ans += s [ i ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " ( ) ( ) ( ) " NEW_LINE K = 2 NEW_LINE print ( findString ( s , K ) ) NEW_LINE DEDENT
Node having maximum number of nodes less than its value in its subtree | Stores the nodes to be deleted ; Structure of a Tree node ; Function to compare the current node key with keys received from it left & right tree by Post Order traversal ; Base Case ; Find nodes lesser than the current root in the left subtree ; Find nodes lesser than the current root in the right subtree ; Stores all the nodes less than the current node 's ; Add the nodes which are less than current node in left [ ] ; Add the nodes which are less than current node in right [ ] ; Create a combined vector for pass to it 's parent ; Stores key that has maximum nodes ; Return the vector of nodes ; Driver Code ; Given Tree ; Function Call ; Print the node value
max_v = 0 NEW_LINE rootIndex = 0 NEW_LINE mp = { } NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def findNodes ( root ) : NEW_LINE INDENT global max_v NEW_LINE global rootIndex NEW_LINE global mp NEW_LINE if ( root == None ) : NEW_LINE INDENT return [ ] NEW_LINE DEDENT left = findNodes ( root . left ) NEW_LINE right = findNodes ( root . right ) NEW_LINE combined = [ ] NEW_LINE count = 0 NEW_LINE for i in range ( len ( left ) ) : NEW_LINE INDENT if ( left [ i ] < root . key ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT combined . append ( left [ i ] ) NEW_LINE DEDENT for i in range ( len ( right ) ) : NEW_LINE INDENT if ( right [ i ] < root . key ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT combined . append ( right [ i ] ) NEW_LINE DEDENT combined . append ( root . key ) NEW_LINE if ( count > max_v ) : NEW_LINE INDENT rootIndex = root . key NEW_LINE max_v = count NEW_LINE DEDENT return combined NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = newNode ( 3 ) NEW_LINE root . left = newNode ( 4 ) NEW_LINE root . right = newNode ( 6 ) NEW_LINE root . right . left = newNode ( 4 ) NEW_LINE root . right . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 10 ) NEW_LINE root . left . right = newNode ( 2 ) NEW_LINE max_v = 0 NEW_LINE rootIndex = - 1 NEW_LINE findNodes ( root ) NEW_LINE print ( rootIndex ) NEW_LINE DEDENT
Maximum sum path in a matrix from top | Function to find the maximum sum path in the grid ; Dimensions of grid [ ] [ ] ; Stores maximum sum at each cell sum [ i ] [ j ] from cell sum [ 0 ] [ 0 ] ; Iterate to compute the maximum sum path in the grid ; Update the maximum path sum ; Return the maximum sum ; Driver Code
def MaximumPath ( grid ) : NEW_LINE INDENT N = len ( grid ) NEW_LINE M = len ( grid [ 0 ] ) NEW_LINE sum = [ [ 0 for i in range ( M + 1 ) ] for i in range ( N + 1 ) ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , M + 1 ) : NEW_LINE INDENT sum [ i ] [ j ] = ( max ( sum [ i - 1 ] [ j ] , sum [ i ] [ j - 1 ] ) + grid [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT return sum [ N ] [ M ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT grid = [ [ 1 , 2 ] , [ 3 , 5 ] ] NEW_LINE print ( MaximumPath ( grid ) ) NEW_LINE DEDENT
Maximize cost of deletions to obtain string having no pair of similar adjacent characters | Function to find maximum cost to remove consecutive characters ; Initialize the count ; Maximum cost ; Traverse from 0 to len ( s ) - 2 ; If characters are identical ; Add cost [ i ] if its maximum ; Add cost [ i + 1 ] if its maximum ; Increment i ; Return the final max count ; Given string s ; Given cost of removal ; Function Call
def Maxcost ( s , cost ) : NEW_LINE INDENT count = 0 NEW_LINE maxcost = 0 NEW_LINE i = 0 NEW_LINE while i < len ( s ) - 1 : NEW_LINE INDENT if s [ i ] == s [ i + 1 ] : NEW_LINE INDENT if cost [ i ] > cost [ i + 1 ] : NEW_LINE INDENT maxcost += cost [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT maxcost += cost [ i + 1 ] NEW_LINE cost [ i + 1 ] = cost [ i ] NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return maxcost NEW_LINE DEDENT s = " abaac " NEW_LINE cost = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE print ( Maxcost ( s , cost ) ) NEW_LINE
Maximum count of values of S modulo M lying in a range [ L , R ] after performing given operations on the array | Lookup table ; Function to count the value of S after adding arr [ i ] or arr [ i - 1 ] to the sum S at each time ; Base Case ; Store the mod value ; If the mod value lies in the range then return 1 ; Else return 0 ; Store the current state ; If already computed , return the computed value ; Recursively adding the elements to the sum adding ai value ; Adding arr [ i ] - 1 value ; Return the maximum count to check for root value as well ; Avoid counting idx = 0 as possible solution we are using idx != 0 ; Return the value of current state ; Driver Code
dp = { } NEW_LINE def countMagicNumbers ( idx , sum , a , n , m , l , r ) : NEW_LINE INDENT if ( idx == n ) : NEW_LINE INDENT temp = sum % m NEW_LINE if ( temp == l or temp == r or ( temp > l and temp < r ) ) : NEW_LINE INDENT dp [ ( idx , sum ) ] = 1 NEW_LINE return dp [ ( idx , sum ) ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ ( idx , sum ) ] = 0 NEW_LINE return dp [ ( idx , sum ) ] NEW_LINE DEDENT DEDENT curr = ( idx , sum ) NEW_LINE if ( curr in dp ) : NEW_LINE INDENT return dp [ curr ] NEW_LINE DEDENT ls = countMagicNumbers ( idx + 1 , sum + a [ idx ] , a , n , m , l , r ) NEW_LINE rs = countMagicNumbers ( idx + 1 , sum + ( a [ idx ] - 1 ) , a , n , m , l , r ) NEW_LINE temp1 = max ( ls , rs ) NEW_LINE temp = sum % m NEW_LINE if ( ( temp == l or temp == r or ( temp > l and temp < r ) ) and idx != 0 ) : NEW_LINE INDENT temp1 += 1 NEW_LINE DEDENT dp [ ( idx , sum ) ] = temp1 NEW_LINE return dp [ ( idx , sum ) ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE M = 22 NEW_LINE L = 14 NEW_LINE R = 16 NEW_LINE arr = [ 17 , 11 , 10 , 8 , 15 ] NEW_LINE print ( countMagicNumbers ( 0 , 0 , arr , N , M , L , R ) ) NEW_LINE DEDENT
Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2 | Python3 program for the above approach ; Function to check if cell with value 1 doesn 't reaches the bottom right cell or not ; Number of rows and columns ; Initialise the deque ; Traverse the matrix ; Push 1 to front of queue ; Push 2 to back of queue ; Store all the possible direction of the current cell ; Run multi - source BFS ; Get the front element ; If 1 reached corner first ; Insert new poin queue ; If 1 can 't reach the bottom  right then return false ; Driver Code ; Given matrix ; Function call
from collections import deque NEW_LINE def reachesBottom ( a ) : NEW_LINE INDENT n = len ( a ) NEW_LINE m = len ( a [ 0 ] ) NEW_LINE q = deque ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( a [ i ] [ j ] == 1 ) : NEW_LINE INDENT q . appendleft ( [ i , j , 1 ] ) NEW_LINE DEDENT elif ( a [ i ] [ j ] == 2 ) : NEW_LINE INDENT q . append ( [ i , j , 2 ] ) NEW_LINE DEDENT a [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT dx = [ - 1 , 0 , 1 , 0 ] NEW_LINE dy = [ 0 , 1 , 0 , - 1 ] NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT front = q . popleft ( ) NEW_LINE i = front [ 0 ] NEW_LINE j = front [ 1 ] NEW_LINE t = front [ 2 ] NEW_LINE if ( a [ i ] [ j ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT a [ i ] [ j ] = 1 NEW_LINE if ( t == 1 and ( i == n - 1 and j == m - 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT for d in range ( 4 ) : NEW_LINE INDENT ni = i + dx [ d ] NEW_LINE nj = j + dy [ d ] NEW_LINE if ( ni >= 0 and ni < n and nj >= 0 and nj < m ) : NEW_LINE INDENT q . append ( [ ni , nj , t ] ) NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 0 , 2 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 2 , 0 ] ] NEW_LINE if ( reachesBottom ( matrix ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Smallest element from all square submatrices of size K from a given Matrix | Python3 program for the above approach ; Function to returns a smallest elements of all KxK submatrices of a given NxM matrix ; Stores the dimensions of the given matrix ; Stores the required smallest elements ; Update the smallest elements row - wise ; Update the minimum column - wise ; Store the final submatrix with required minimum values ; Return the resultant matrix ; Function call ; Print resultant matrix with the minimum values of KxK sub - matrix ; Given matrix ; Given K
import sys NEW_LINE def matrixMinimum ( nums , K ) : NEW_LINE INDENT N = len ( nums ) NEW_LINE M = len ( nums [ 0 ] ) NEW_LINE res = [ [ 0 for x in range ( M - K + 1 ) ] for y in range ( N - K + 1 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M - K + 1 ) : NEW_LINE INDENT mn = sys . maxsize NEW_LINE for k in range ( j , j + K ) : NEW_LINE INDENT mn = min ( mn , nums [ i ] [ k ] ) NEW_LINE DEDENT nums [ i ] [ j ] = mn NEW_LINE DEDENT DEDENT for j in range ( M ) : NEW_LINE INDENT for i in range ( N - K + 1 ) : NEW_LINE INDENT mn = sys . maxsize NEW_LINE for k in range ( i , i + K ) : NEW_LINE INDENT mn = min ( mn , nums [ k ] [ j ] ) NEW_LINE DEDENT nums [ i ] [ j ] = mn NEW_LINE DEDENT DEDENT for i in range ( N - K + 1 ) : NEW_LINE INDENT for j in range ( M - K + 1 ) : NEW_LINE INDENT res [ i ] [ j ] = nums [ i ] [ j ] NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def smallestinKsubmatrices ( arr , K ) : NEW_LINE INDENT res = matrixMinimum ( arr , K ) NEW_LINE for i in range ( len ( res ) ) : NEW_LINE INDENT for j in range ( len ( res [ 0 ] ) ) : NEW_LINE INDENT print ( res [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT arr = [ [ - 1 , 5 , 4 , 1 , - 3 ] , [ 4 , 3 , 1 , 1 , 6 ] , [ 2 , - 2 , 5 , 3 , 1 ] , [ 8 , 5 , 1 , 9 , - 4 ] , [ 12 , 3 , 5 , 8 , 1 ] ] NEW_LINE K = 3 NEW_LINE smallestinKsubmatrices ( arr , K ) NEW_LINE
Queries to find Kth greatest character in a range [ L , R ] from a string with updates | Python3 Program to implement the above approach Function to find the kth greatest character from the string ; Sorting the in non - increasing Order ; Function to prthe K - th character from the subS [ l ] . . S [ r ] ; 0 - based indexing ; Subof strr from the indices l to r . ; Extract kth Largest character ; Function to replace character at pos of strr by the character s ; Index of S to be updated . ; Character to be replaced at index in S ; Driver Code ; Given string ; Count of queries ; Queries
def find_kth_largest ( strr , k ) : NEW_LINE INDENT strr = sorted ( strr ) NEW_LINE strr = strr [ : : - 1 ] NEW_LINE return strr [ k - 1 ] NEW_LINE DEDENT def printCharacter ( strr , l , r , k ) : NEW_LINE INDENT l = l - 1 NEW_LINE r = r - 1 NEW_LINE temp = strr [ l : r - l + 1 ] NEW_LINE ans = find_kth_largest ( temp , k ) NEW_LINE return ans NEW_LINE DEDENT def updateString ( strr , pos , s ) : NEW_LINE INDENT index = pos - 1 NEW_LINE c = s NEW_LINE strr [ index ] = c NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " abcddef " NEW_LINE strr = [ i for i in strr ] NEW_LINE Q = 3 NEW_LINE print ( printCharacter ( strr , 1 , 2 , 2 ) ) NEW_LINE updateString ( strr , 4 , ' g ' ) NEW_LINE print ( printCharacter ( strr , 1 , 5 , 4 ) ) NEW_LINE DEDENT
Split array into two subarrays such that difference of their sum is minimum | Python3 program for the above approach ; Function to return minimum difference between sum of two subarrays ; To store total sum of array ; Calculate the total sum of the array ; Stores the prefix sum ; Stores the minimum difference ; Traverse the given array ; To store minimum difference ; Update minDiff ; Return minDiff ; Given array ; Length of the array
import sys NEW_LINE def minDiffSubArray ( arr , n ) : NEW_LINE INDENT total_sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT total_sum += arr [ i ] NEW_LINE DEDENT prefix_sum = 0 NEW_LINE minDiff = sys . maxsize NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT prefix_sum += arr [ i ] NEW_LINE diff = abs ( ( total_sum - prefix_sum ) - prefix_sum ) NEW_LINE if ( diff < minDiff ) : NEW_LINE INDENT minDiff = diff NEW_LINE DEDENT DEDENT return minDiff NEW_LINE DEDENT arr = [ 7 , 9 , 5 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minDiffSubArray ( arr , n ) ) NEW_LINE
Maximize count of non | Function to count the maximum number of subarrays with sum K ; Stores all the distinct prefixSums obtained ; Stores the prefix sum of the current subarray ; Stores the count of subarrays with sum K ; If a subarray with sum K is already found ; Increase count ; Reset prefix sum ; Clear the set ; Insert the prefix sum ; Driver Code
def CtSubarr ( arr , N , K ) : NEW_LINE INDENT st = set ( ) NEW_LINE prefixSum = 0 NEW_LINE st . add ( prefixSum ) NEW_LINE res = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT prefixSum += arr [ i ] NEW_LINE if ( ( prefixSum - K ) in st ) : NEW_LINE INDENT res += 1 NEW_LINE prefixSum = 0 NEW_LINE st . clear ( ) NEW_LINE st . add ( 0 ) NEW_LINE DEDENT st . add ( prefixSum ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ - 2 , 6 , 6 , 3 , 5 , 4 , 1 , 2 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE K = 10 NEW_LINE print ( CtSubarr ( arr , N , K ) ) NEW_LINE
Count of subarrays consisting of only prime numbers | Function to check if a number is prime or not . ; If n has any factor other than 1 , then n is non - prime . ; Function to return the count of subarrays made up of prime numbers only ; Stores the answer ; Stores the count of continuous prime numbers in an array ; If the current array element is prime ; Increase the count ; Update count of subarrays ; If the array ended with a continuous prime sequence ; Driver Code
def is_prime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def count_prime_subarrays ( ar , n ) : NEW_LINE INDENT ans = 0 NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( is_prime ( ar [ i ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( count ) : NEW_LINE INDENT ans += count * ( count + 1 ) // 2 NEW_LINE count = 0 NEW_LINE DEDENT DEDENT DEDENT if ( count ) : NEW_LINE INDENT ans += count * ( count + 1 ) // 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 10 NEW_LINE ar = [ 2 , 3 , 5 , 6 , 7 , 11 , 3 , 5 , 9 , 3 ] NEW_LINE print ( count_prime_subarrays ( ar , N ) ) NEW_LINE
Minimum Subarray flips required to convert all elements of a Binary Array to K | Function to count the minimum number of subarray flips required ; Iterate the array ; If arr [ i ] and flag are equal ; Return the answer ; Driver Code
def minSteps ( arr , n , k ) : NEW_LINE INDENT cnt = 0 NEW_LINE if ( k == 1 ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT else : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == flag ) : NEW_LINE INDENT cnt += 1 NEW_LINE flag = ( flag + 1 ) % 2 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT arr = [ 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE k = 1 NEW_LINE print ( minSteps ( arr , n , k ) ) NEW_LINE
Minimize Sum of an Array by at most K reductions | Function to obtain the minimum possible sum from the array by K reductions ;  ; Insert elements into the MaxHeap ; Remove the maximum ; Insert maximum / 2 ; Stores the sum of remaining elements ; Driver code
def minSum ( a , n , k ) : NEW_LINE ' TABSYMBOL TABSYMBOL Implements ▁ the ▁ MaxHeap ' ' ' NEW_LINE INDENT q = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT q . append ( a [ i ] ) NEW_LINE DEDENT q = sorted ( q ) NEW_LINE while ( len ( q ) > 0 and k > 0 ) : NEW_LINE INDENT top = q [ - 1 ] // 2 NEW_LINE del q [ - 1 ] NEW_LINE q . append ( top ) NEW_LINE k -= 1 NEW_LINE q = sorted ( q ) NEW_LINE DEDENT sum = 0 NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT sum += q [ - 1 ] NEW_LINE del q [ - 1 ] NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE k = 3 NEW_LINE a = [ 20 , 7 , 5 , 4 ] NEW_LINE print ( minSum ( a , n , k ) ) NEW_LINE DEDENT