codeparrot/xlcost-text-to-code huggingface dataset:
Maximum items that can be filled in K Knapsacks of given Capacity | 2 - d array to store states of DP ; 2 - d array to store if a state has been solved ; Vector to store power of variable ' C ' . ; function to compute the states ; Base case ; Checking if a state has been solved ; Setting a state as solved ; Recurrence relation ; Returning the solved state ; Function to initialize global variables and find the initial value of 'R ; Resizing the variables ; Variable to store the initial value of R ; Input array ; number of knapsacks and capacity ; Performing required pre - computation ; finding the required answer
x = 100 NEW_LINE dp = [ [ 0 for i in range ( x ) ] for i in range ( x ) ] NEW_LINE v = [ [ 0 for i in range ( x ) ] for i in range ( x ) ] NEW_LINE exp_c = [ ] NEW_LINE def FindMax ( i , r , w , n , c , k ) : NEW_LINE INDENT if ( i >= n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( v [ i ] [ r ] ) : NEW_LINE INDENT return dp [ i ] [ r ] NEW_LINE DEDENT v [ i ] [ r ] = 1 NEW_LINE dp [ i ] [ r ] = FindMax ( i + 1 , r , w , n , c , k ) NEW_LINE for j in range ( k ) : NEW_LINE INDENT x = ( r // exp_c [ j ] ) % ( c + 1 ) NEW_LINE if ( x - w [ i ] >= 0 ) : NEW_LINE INDENT dp [ i ] [ r ] = max ( dp [ i ] [ r ] , w [ i ] + FindMax ( i + 1 , r - w [ i ] * exp_c [ j ] , w , n , c , k ) ) NEW_LINE DEDENT DEDENT return dp [ i ] [ r ] NEW_LINE DEDENT ' NEW_LINE def PreCompute ( n , c , k ) : NEW_LINE INDENT exp_c . append ( 1 ) NEW_LINE for i in range ( 1 , k ) : NEW_LINE INDENT exp_c [ i ] = ( exp_c [ i - 1 ] * ( c + 1 ) ) NEW_LINE DEDENT R = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT R += exp_c [ i ] * c NEW_LINE DEDENT return R NEW_LINE DEDENT w = [ 3 , 8 , 9 ] NEW_LINE k = 1 NEW_LINE c = 11 NEW_LINE n = len ( w ) NEW_LINE r = PreCompute ( n , c , k ) NEW_LINE print ( FindMax ( 0 , r , w , n , c , k ) ) NEW_LINE
Find number of pairs ( x , y ) in an array such that x ^ y > y ^ x | Python3 program to find the number of pairs ( x , y ) in an array such that x ^ y > y ^ x ; Function to return count of pairs with x as one element of the pair . It mainly looks for all values in Y where x ^ Y [ i ] > Y [ i ] ^ x ; If x is 0 , then there cannot be any value in Y such that x ^ Y [ i ] > Y [ i ] ^ x ; If x is 1 , then the number of pairs is equal to number of zeroes in Y ; Find number of elements in Y [ ] with values greater than x , bisect . bisect_right gets address of first greater element in Y [ 0. . n - 1 ] ; If we have reached here , then x must be greater than 1 , increase number of pairs for y = 0 and y = 1 ; Decrease number of pairs for x = 2 and ( y = 4 or y = 3 ) ; Increase number of pairs for x = 3 and y = 2 ; Function to return count of pairs ( x , y ) such that x belongs to X , y belongs to Y and x ^ y > y ^ x ; To store counts of 0 , 1 , 2 , 3 , and 4 in array Y ; Sort Y so that we can do binary search in it ; Initialize result ; Take every element of X and count pairs with it ; Driver Code
import bisect NEW_LINE def count ( x , Y , n , NoOfY ) : NEW_LINE INDENT if x == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if x == 1 : NEW_LINE INDENT return NoOfY [ 0 ] NEW_LINE DEDENT idx = bisect . bisect_right ( Y , x ) NEW_LINE ans = n - idx NEW_LINE ans += NoOfY [ 0 ] + NoOfY [ 1 ] NEW_LINE if x == 2 : NEW_LINE INDENT ans -= NoOfY [ 3 ] + NoOfY [ 4 ] NEW_LINE DEDENT if x == 3 : NEW_LINE INDENT ans += NoOfY [ 2 ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def count_pairs ( X , Y , m , n ) : NEW_LINE INDENT NoOfY = [ 0 ] * 5 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if Y [ i ] < 5 : NEW_LINE INDENT NoOfY [ Y [ i ] ] += 1 NEW_LINE DEDENT DEDENT Y . sort ( ) NEW_LINE total_pairs = 0 NEW_LINE for x in X : NEW_LINE INDENT total_pairs += count ( x , Y , n , NoOfY ) NEW_LINE DEDENT return total_pairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = [ 2 , 1 , 6 ] NEW_LINE Y = [ 1 , 5 ] NEW_LINE print ( " Total ▁ pairs ▁ = ▁ " , count_pairs ( X , Y , len ( X ) , len ( Y ) ) ) NEW_LINE DEDENT
Count all distinct pairs with difference equal to k | A simple program to count pairs with difference k ; Pick all elements one by one ; See if there is a pair of this picked element ; Driver program
def countPairsWithDiffK ( arr , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if arr [ i ] - arr [ j ] == k or arr [ j ] - arr [ i ] == k : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( arr , n , k ) ) NEW_LINE
Count all distinct pairs with difference equal to k | Standard binary search function ; Returns count of pairs with difference k in arr [ ] of size n . ; Sort array elements ; Pick a first element point ; Driver Code
def binarySearch ( arr , low , high , x ) : NEW_LINE INDENT if ( high >= low ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if x == arr [ mid ] : NEW_LINE INDENT return ( mid ) NEW_LINE DEDENT elif ( x > arr [ mid ] ) : NEW_LINE INDENT return binarySearch ( arr , ( mid + 1 ) , high , x ) NEW_LINE DEDENT else : NEW_LINE INDENT return binarySearch ( arr , low , ( mid - 1 ) , x ) NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def countPairsWithDiffK ( arr , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT if ( binarySearch ( arr , i + 1 , n - 1 , arr [ i ] + k ) != - 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( arr , n , k ) ) NEW_LINE
Count all distinct pairs with difference equal to k | Returns count of pairs with difference k in arr [ ] of size n . ; Sort array elements ; arr [ r ] - arr [ l ] < sum ; Driver code
def countPairsWithDiffK ( arr , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE arr . sort ( ) NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE while r < n : NEW_LINE INDENT if arr [ r ] - arr [ l ] == k : NEW_LINE INDENT count += 1 NEW_LINE l += 1 NEW_LINE r += 1 NEW_LINE DEDENT elif arr [ r ] - arr [ l ] > k : NEW_LINE INDENT l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT r += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 3 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( arr , n , k ) ) NEW_LINE DEDENT
Count of sub | Python3 implementation of the approach ; Function to return the total number of required sub - sets ; Variable to store total elements which on dividing by 3 give remainder 0 , 1 and 2 respectively ; Create a dp table ; Process for n states and store the sum ( mod 3 ) for 0 , 1 and 2 ; Use of MOD for large numbers ; Final answer store at dp [ n - 1 ] [ 0 ] ; Driver Code
import math NEW_LINE def totalSubSets ( n , l , r ) : NEW_LINE INDENT MOD = 1000000007 ; NEW_LINE zero = ( math . floor ( r / 3 ) - math . ceil ( l / 3 ) + 1 ) ; NEW_LINE one = ( math . floor ( ( r - 1 ) / 3 ) - math . ceil ( ( l - 1 ) / 3 ) + 1 ) ; NEW_LINE two = ( math . floor ( ( r - 2 ) / 3 ) - math . ceil ( ( l - 2 ) / 3 ) + 1 ) ; NEW_LINE dp = [ [ 0 for x in range ( 3 ) ] for y in range ( n ) ] NEW_LINE dp [ 0 ] [ 0 ] = zero ; NEW_LINE dp [ 0 ] [ 1 ] = one ; NEW_LINE dp [ 0 ] [ 2 ] = two ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] [ 0 ] = ( ( dp [ i - 1 ] [ 0 ] * zero ) + ( dp [ i - 1 ] [ 1 ] * two ) + ( dp [ i - 1 ] [ 2 ] * one ) ) % MOD ; NEW_LINE dp [ i ] [ 1 ] = ( ( dp [ i - 1 ] [ 0 ] * one ) + ( dp [ i - 1 ] [ 1 ] * zero ) + ( dp [ i - 1 ] [ 2 ] * two ) ) % MOD ; NEW_LINE dp [ i ] [ 2 ] = ( ( dp [ i - 1 ] [ 0 ] * two ) + ( dp [ i - 1 ] [ 1 ] * one ) + ( dp [ i - 1 ] [ 2 ] * zero ) ) % MOD ; NEW_LINE DEDENT return dp [ n - 1 ] [ 0 ] ; NEW_LINE DEDENT n = 5 ; NEW_LINE l = 10 ; NEW_LINE r = 100 ; NEW_LINE print ( totalSubSets ( n , l , r ) ) ; NEW_LINE
Check if a word exists in a grid or not | Python3 program to check if the word exists in the grid or not ; Function to check if a word exists in a grid starting from the first match in the grid level : index till which pattern is matched x , y : current position in 2D array ; Pattern matched ; Out of Boundary ; If grid matches with a letter while recursion ; Marking this cell as visited ; finding subpattern in 4 directions ; marking this cell as unvisited again ; else : Not matching then false ; Function to check if the word exists in the grid or not ; if total characters in matrix is less then pattern lenghth ; Traverse in the grid ; If first letter matches , then recur and check ; Driver Code ; Function to check if word exists or not
r = 4 NEW_LINE c = 4 NEW_LINE def findmatch ( mat , pat , x , y , nrow , ncol , level ) : NEW_LINE INDENT l = len ( pat ) NEW_LINE if ( level == l ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( x < 0 or y < 0 or x >= nrow or y >= ncol ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( mat [ x ] [ y ] == pat [ level ] ) : NEW_LINE INDENT temp = mat [ x ] [ y ] NEW_LINE mat [ x ] . replace ( mat [ x ] [ y ] , ' # ' ) NEW_LINE res = ( findmatch ( mat , pat , x - 1 , y , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x + 1 , y , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x , y - 1 , nrow , ncol , level + 1 ) | findmatch ( mat , pat , x , y + 1 , nrow , ncol , level + 1 ) ) NEW_LINE mat [ x ] . replace ( mat [ x ] [ y ] , temp ) NEW_LINE return res NEW_LINE return False NEW_LINE DEDENT DEDENT def checkMatch ( mat , pat , nrow , ncol ) : NEW_LINE INDENT l = len ( pat ) NEW_LINE if ( l > nrow * ncol ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( nrow ) : NEW_LINE INDENT for j in range ( ncol ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == pat [ 0 ] ) : NEW_LINE INDENT if ( findmatch ( mat , pat , i , j , nrow , ncol , 0 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT grid = [ " axmy " , " bgdf " , " xeet " , " raks " ] NEW_LINE if ( checkMatch ( grid , " geeks " , r , c ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Construct an array from its pair | Fills element in arr [ ] from its pair sum array pair [ ] . n is size of arr [ ] ; Driver code
def constructArr ( arr , pair , n ) : NEW_LINE INDENT arr [ 0 ] = ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) // 2 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT arr [ i ] = pair [ i - 1 ] - arr [ 0 ] NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT pair = [ 15 , 13 , 11 , 10 , 12 , 10 , 9 , 8 , 7 , 5 ] NEW_LINE n = 5 NEW_LINE arr = [ 0 ] * n NEW_LINE constructArr ( arr , pair , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Merge two sorted arrays with O ( 1 ) extra space | Merge ar1 [ ] and ar2 [ ] with O ( 1 ) extra space ; Iterate through all elements of ar2 [ ] starting from the last element ; Find the smallest element greater than ar2 [ i ] . Move all elements one position ahead till the smallest greater element is not found ; If there was a greater element ; Driver program
def merge ( ar1 , ar2 , m , n ) : NEW_LINE INDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT last = ar1 [ m - 1 ] NEW_LINE j = m - 2 NEW_LINE while ( j >= 0 and ar1 [ j ] > ar2 [ i ] ) : NEW_LINE INDENT ar1 [ j + 1 ] = ar1 [ j ] NEW_LINE j -= 1 NEW_LINE DEDENT if ( j != m - 2 or last > ar2 [ i ] ) : NEW_LINE INDENT ar1 [ j + 1 ] = ar2 [ i ] NEW_LINE ar2 [ i ] = last NEW_LINE DEDENT DEDENT DEDENT ar1 = [ 1 , 5 , 9 , 10 , 15 , 20 ] NEW_LINE ar2 = [ 2 , 3 , 8 , 13 ] NEW_LINE m = len ( ar1 ) NEW_LINE n = len ( ar2 ) NEW_LINE merge ( ar1 , ar2 , m , n ) NEW_LINE print ( " After Merging First Array : " , ▁ end = " " ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT print ( ar1 [ i ] , " ▁ " , end = " " ) NEW_LINE DEDENT print ( " Second Array : " , ▁ end = " " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( ar2 [ i ] , " ▁ " , end = " " ) NEW_LINE DEDENT
Gould 's Sequence | Function to generate gould 's Sequence ; loop to generate each row of pascal 's Triangle up to nth row ; Loop to generate each element of ith row ; if c is odd increment count ; print count of odd elements ; Get n ; Function call
def gouldSequence ( n ) : NEW_LINE INDENT for row_num in range ( 1 , n ) : NEW_LINE INDENT count = 1 NEW_LINE c = 1 NEW_LINE for i in range ( 1 , row_num ) : NEW_LINE INDENT c = c * ( row_num - i ) / i NEW_LINE if ( c % 2 == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 16 ; NEW_LINE gouldSequence ( n ) NEW_LINE
Product of maximum in first array and minimum in second | Function to calculate the product ; Sort the arrays to find the maximum and minimum elements in given arrays ; Return product of maximum and minimum . ; Driver Program
def minmaxProduct ( arr1 , arr2 , n1 , n2 ) : NEW_LINE INDENT arr1 . sort ( ) NEW_LINE arr2 . sort ( ) NEW_LINE return arr1 [ n1 - 1 ] * arr2 [ 0 ] NEW_LINE DEDENT arr1 = [ 10 , 2 , 3 , 6 , 4 , 1 ] NEW_LINE arr2 = [ 5 , 1 , 4 , 2 , 6 , 9 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE n2 = len ( arr2 ) NEW_LINE print ( minmaxProduct ( arr1 , arr2 , n1 , n2 ) ) NEW_LINE
Minimum odd cost path in a matrix | Python3 program to find Minimum odd cost path in a matrix ; Function to find the minimum cost ; leftmost element ; rightmost element ; Any element except leftmost and rightmost element of a row is reachable from direct upper or left upper or right upper row 's block ; Counting the minimum cost ; Find the minimum cost ; Driver code
M = 100 NEW_LINE N = 100 NEW_LINE def find_min_odd_cost ( given , m , n ) : NEW_LINE INDENT floor = [ [ 0 for i in range ( M ) ] for i in range ( N ) ] NEW_LINE min_odd_cost = 0 NEW_LINE i , j , temp = 0 , 0 , 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT floor [ 0 ] [ j ] = given [ 0 ] [ j ] NEW_LINE DEDENT for i in range ( 1 , m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT floor [ i ] [ j ] = given [ i ] [ j ] ; NEW_LINE floor [ i ] [ j ] += min ( floor [ i - 1 ] [ j ] , floor [ i - 1 ] [ j + 1 ] ) NEW_LINE DEDENT elif ( j == n - 1 ) : NEW_LINE INDENT floor [ i ] [ j ] = given [ i ] [ j ] ; NEW_LINE floor [ i ] [ j ] += min ( floor [ i - 1 ] [ j ] , floor [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT temp = min ( floor [ i - 1 ] [ j ] , floor [ i - 1 ] [ j - 1 ] ) NEW_LINE temp = min ( temp , floor [ i - 1 ] [ j + 1 ] ) NEW_LINE floor [ i ] [ j ] = given [ i ] [ j ] + temp NEW_LINE DEDENT DEDENT DEDENT min_odd_cost = 10 ** 9 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( floor [ n - 1 ] [ j ] % 2 == 1 ) : NEW_LINE INDENT if ( min_odd_cost > floor [ n - 1 ] [ j ] ) : NEW_LINE INDENT min_odd_cost = floor [ n - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT if ( min_odd_cost == - 10 ** 9 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT return min_odd_cost NEW_LINE DEDENT m , n = 5 , 5 NEW_LINE given = [ [ 1 , 2 , 3 , 4 , 6 ] , [ 1 , 2 , 3 , 4 , 5 ] , [ 1 , 2 , 3 , 4 , 5 ] , [ 1 , 2 , 3 , 4 , 5 ] , [ 100 , 2 , 3 , 4 , 5 ] ] NEW_LINE print ( " Minimum ▁ odd ▁ cost ▁ is " , find_min_odd_cost ( given , m , n ) ) NEW_LINE
Product of maximum in first array and minimum in second | Function to calculate the product ; Initialize max of first array ; initialize min of second array ; To find the maximum element in first array ; To find the minimum element in second array ; Process remaining elements ; Driver code
def minMaxProduct ( arr1 , arr2 , n1 , n2 ) : NEW_LINE INDENT max = arr1 [ 0 ] NEW_LINE min = arr2 [ 0 ] NEW_LINE i = 1 NEW_LINE while ( i < n1 and i < n2 ) : NEW_LINE INDENT if ( arr1 [ i ] > max ) : NEW_LINE INDENT max = arr1 [ i ] NEW_LINE DEDENT if ( arr2 [ i ] < min ) : NEW_LINE INDENT min = arr2 [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT while ( i < n1 ) : NEW_LINE INDENT if ( arr1 [ i ] > max ) : NEW_LINE INDENT max = arr1 [ i ] NEW_LINE i += 1 NEW_LINE DEDENT DEDENT while ( i < n2 ) : NEW_LINE INDENT if ( arr2 [ i ] < min ) : NEW_LINE INDENT min = arr2 [ i ] NEW_LINE i += 1 NEW_LINE DEDENT DEDENT return max * min NEW_LINE DEDENT arr1 = [ 10 , 2 , 3 , 6 , 4 , 1 ] NEW_LINE arr2 = [ 5 , 1 , 4 , 2 , 6 , 9 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE n2 = len ( arr1 ) NEW_LINE print ( minMaxProduct ( arr1 , arr2 , n1 , n2 ) ) NEW_LINE
Search , insert and delete in an unsorted array | Function to implement search operation ; Driver Code ; Using a last element as search element
def findElement ( arr , n , key ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == key ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 12 , 34 , 10 , 6 , 40 ] NEW_LINE n = len ( arr ) NEW_LINE key = 40 NEW_LINE index = findElement ( arr , n , key ) NEW_LINE if index != - 1 : NEW_LINE INDENT print ( " element ▁ found ▁ at ▁ position : ▁ " + str ( index + 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " element ▁ not ▁ found " ) NEW_LINE DEDENT
Search , insert and delete in a sorted array | function to implement binary search ; low + ( high - low ) / 2 ; Driver program to check above functions Let us search 3 in below array
def binarySearch ( arr , low , high , key ) : NEW_LINE INDENT mid = ( low + high ) / 2 NEW_LINE if ( key == arr [ int ( mid ) ] ) : NEW_LINE INDENT return mid NEW_LINE DEDENT if ( key > arr [ int ( mid ) ] ) : NEW_LINE INDENT return binarySearch ( arr , ( mid + 1 ) , high , key ) NEW_LINE DEDENT if ( key < arr [ int ( mid ) ] ) : NEW_LINE INDENT return binarySearch ( arr , low , ( mid - 1 ) , key ) NEW_LINE DEDENT return 0 NEW_LINE DEDENT arr = [ 5 , 6 , 7 , 8 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE key = 10 NEW_LINE print ( " Index : " , int ( binarySearch ( arr , 0 , n - 1 , key ) ) ) NEW_LINE
Search , insert and delete in a sorted array | Inserts a key in arr [ ] of given capacity . n is current size of arr [ ] . This function returns n + 1 if insertion is successful , else n . ; Cannot insert more elements if n is already more than or equal to capcity ; Driver Code ; Inserting key
def insertSorted ( arr , n , key , capacity ) : NEW_LINE INDENT if ( n >= capacity ) : NEW_LINE INDENT return n NEW_LINE DEDENT i = n - 1 NEW_LINE while i >= 0 and arr [ i ] > key : NEW_LINE INDENT arr [ i + 1 ] = arr [ i ] NEW_LINE i -= 1 NEW_LINE DEDENT arr [ i + 1 ] = key NEW_LINE return ( n + 1 ) NEW_LINE DEDENT arr = [ 12 , 16 , 20 , 40 , 50 , 70 ] NEW_LINE for i in range ( 20 ) : NEW_LINE INDENT arr . append ( 0 ) NEW_LINE DEDENT capacity = len ( arr ) NEW_LINE n = 6 NEW_LINE key = 26 NEW_LINE print ( " Before ▁ Insertion : ▁ " , end = " ▁ " ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT n = insertSorted ( arr , n , key , capacity ) NEW_LINE print ( " After Insertion : " , ▁ end ▁ = ▁ " " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Search , insert and delete in a sorted array | To search a ley to be deleted ; Function to delete an element ; Find position of element to be deleted ; Deleting element ; Driver code
def binarySearch ( arr , low , high , key ) : NEW_LINE INDENT if ( high < low ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT mid = ( low + high ) // 2 NEW_LINE if ( key == arr [ mid ] ) : NEW_LINE INDENT return mid NEW_LINE DEDENT if ( key > arr [ mid ] ) : NEW_LINE INDENT return binarySearch ( arr , ( mid + 1 ) , high , key ) NEW_LINE DEDENT return binarySearch ( arr , low , ( mid - 1 ) , key ) NEW_LINE DEDENT def deleteElement ( arr , n , key ) : NEW_LINE INDENT pos = binarySearch ( arr , 0 , n - 1 , key ) NEW_LINE if ( pos == - 1 ) : NEW_LINE INDENT print ( " Element ▁ not ▁ found " ) NEW_LINE return n NEW_LINE DEDENT for i in range ( pos , n - 1 ) : NEW_LINE INDENT arr [ i ] = arr [ i + 1 ] NEW_LINE DEDENT return n - 1 NEW_LINE DEDENT arr = [ 10 , 20 , 30 , 40 , 50 ] NEW_LINE n = len ( arr ) NEW_LINE key = 30 NEW_LINE print ( " Array ▁ before ▁ deletion " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT n = deleteElement ( arr , n , key ) NEW_LINE print ( " Array after deletion " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Queries on number of Binary sub | Python 3 Program to answer queries on number of submatrix of given size ; Solve each query on matrix ; For each of the cell . ; finding submatrix size of oth row and column . ; intermediate cells . ; Find frequency of each distinct size for 0 s and 1 s . ; Find the Cumulative Sum . ; Output the answer for each query ; Driver Program
MAX = 100 NEW_LINE N = 5 NEW_LINE M = 4 NEW_LINE def solveQuery ( n , m , mat , q , a , binary ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( m ) ] for y in range ( n ) ] NEW_LINE max = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( i == 0 or j == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 NEW_LINE DEDENT elif ( ( mat [ i ] [ j ] == mat [ i - 1 ] [ j ] ) and ( mat [ i ] [ j ] == mat [ i ] [ j - 1 ] ) and ( mat [ i ] [ j ] == mat [ i - 1 ] [ j - 1 ] ) ) : NEW_LINE INDENT dp [ i ] [ j ] = ( min ( dp [ i - 1 ] [ j ] , min ( dp [ i - 1 ] [ j - 1 ] , dp [ i ] [ j - 1 ] ) ) + 1 ) NEW_LINE if ( max < dp [ i ] [ j ] ) : NEW_LINE INDENT max = dp [ i ] [ j ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT freq0 = [ 0 ] * MAX NEW_LINE freq1 = [ 0 ] * MAX NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == 0 ) : NEW_LINE INDENT freq0 [ dp [ i ] [ j ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq1 [ dp [ i ] [ j ] ] += 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( max - 1 , - 1 , - 1 ) : NEW_LINE INDENT freq0 [ i ] += freq0 [ i + 1 ] NEW_LINE freq1 [ i ] += freq1 [ i + 1 ] NEW_LINE DEDENT for i in range ( q ) : NEW_LINE INDENT if ( binary [ i ] == 0 ) : NEW_LINE INDENT print ( freq0 [ a [ i ] ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( freq1 [ a [ i ] ] ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE m = 4 NEW_LINE mat = [ [ 0 , 0 , 1 , 1 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 1 ] , [ 0 , 1 , 1 , 1 ] ] NEW_LINE q = 2 NEW_LINE a = [ 2 , 2 ] NEW_LINE binary = [ 1 , 0 ] NEW_LINE solveQuery ( n , m , mat , q , a , binary ) NEW_LINE DEDENT