codeparrot/xlcost-text-to-code huggingface dataset:
Largest subset having with sum less than equal to sum of respective indices | Function to find the length of the longest subset ; Stores the sum of differences between elements and their respective index ; Stores the size of the subset ; Iterate over the array ; If an element which is smaller than or equal to its index is encountered ; Increase count and sum ; Store the difference with index of the remaining elements ; Sort the differences in increasing order ; Include the differences while sum remains positive or ; Return the size ; Driver code ; Function calling
def findSubset ( a , n ) : NEW_LINE INDENT sum = 0 NEW_LINE cnt = 0 NEW_LINE v = [ ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( a [ i - 1 ] - i <= 0 ) : NEW_LINE INDENT sum += a [ i - 1 ] - i NEW_LINE cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( a [ i - 1 ] - i ) NEW_LINE DEDENT DEDENT v . sort ( ) NEW_LINE ptr = 0 NEW_LINE while ( ptr < len ( v ) and sum + v [ ptr ] <= 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE ptr += 1 NEW_LINE sum += v [ ptr ] NEW_LINE DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 1 , 6 , 7 , 8 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findSubset ( arr , n ) ) NEW_LINE DEDENT
Queries for count of array elements with values in given range with updates | Python3 code for queries for number of elements that lie in range [ l , r ] ( with updates ) ; Function to set arr [ index ] = x ; Function to get count of elements that lie in range [ l , r ] ; Traverse array ; If element lies in the range [ L , R ] ; Increase count ; Function to solve each query ; Driver Code
from typing import Generic , List , TypeVar NEW_LINE T = TypeVar ( ' T ' ) NEW_LINE V = TypeVar ( ' V ' ) NEW_LINE class Pair ( Generic [ V , T ] ) : NEW_LINE INDENT def __init__ ( self , first : V , second : T ) -> None : NEW_LINE INDENT self . first = first NEW_LINE self . second = second NEW_LINE DEDENT DEDENT def setElement ( arr : List [ int ] , n : int , index : int , x : int ) -> None : NEW_LINE INDENT arr [ index ] = x NEW_LINE DEDENT def getCount ( arr : List [ int ] , n : int , l : int , r : int ) -> int : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] >= l and arr [ i ] <= r ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def SolveQuery ( arr : List [ int ] , n : int , Q : List [ Pair [ int , Pair [ int , int ] ] ] ) : NEW_LINE INDENT x = 0 NEW_LINE for i in range ( len ( Q ) ) : NEW_LINE INDENT if ( Q [ i ] . first == 1 ) : NEW_LINE INDENT x = getCount ( arr , n , Q [ i ] . second . first , Q [ i ] . second . second ) NEW_LINE print ( x , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT setElement ( arr , n , Q [ i ] . second . first , Q [ i ] . second . second ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE Q = [ Pair ( 1 , Pair ( 3 , 5 ) ) , Pair ( 1 , Pair ( 2 , 4 ) ) , Pair ( 1 , Pair ( 1 , 2 ) ) , Pair ( 2 , Pair ( 1 , 7 ) ) , Pair ( 1 , Pair ( 1 , 2 ) ) ] NEW_LINE SolveQuery ( arr , n , Q ) NEW_LINE DEDENT
Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference | Function generates the sequence ; Map to store the frequency of numbers ; Sum of all frequencies ; Try all possibilities for the starting element ; If the frequency of current element is non - zero ; vector to store the answer ; Copy of the map for every possible starting element ; Decrement the frequency ; Push the starting element to the vector ; The last element inserted is i ; Try to fill the rest of the positions if possible ; If the frequency of last - 1 is non - zero ; Decrement the frequency of last - 1 ; Insert it into the sequence ; Update last number added to sequence ; Break from the inner loop ; If the size of the sequence vector is equal to sum of total frequqncies ; Return sequence ; If no such sequence if found return empty sequence ; Function Call to print the sequence ; The required sequence ; If the size of sequence if zero it means no such sequence was found ; Otherwise print the sequence ; Driver Code ; Frequency of all elements from 0 to n - 1 ; Number of elements whose frequencies are given ; Function Call
def generateSequence ( freq , n ) : NEW_LINE INDENT m = { } NEW_LINE total = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ i ] = freq [ i ] NEW_LINE total += freq [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( m [ i ] ) : NEW_LINE INDENT sequence = [ ] NEW_LINE mcopy = { } NEW_LINE for j in m : NEW_LINE INDENT mcopy [ j ] = m [ j ] NEW_LINE DEDENT mcopy [ i ] -= 1 NEW_LINE sequence . append ( i ) NEW_LINE last = i NEW_LINE for j in range ( total - 1 ) : NEW_LINE INDENT if ( ( last - 1 ) in mcopy and mcopy [ last - 1 ] > 0 ) : NEW_LINE INDENT mcopy [ last - 1 ] -= 1 NEW_LINE sequence . append ( last - 1 ) NEW_LINE last -= 1 NEW_LINE DEDENT elif ( mcopy [ last + 1 ] ) : NEW_LINE INDENT mcopy [ last + 1 ] -= 1 NEW_LINE sequence . append ( last + 1 ) NEW_LINE last += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( len ( sequence ) == total ) : NEW_LINE INDENT return sequence NEW_LINE DEDENT DEDENT DEDENT return [ ] NEW_LINE DEDENT def PrintSequence ( freq , n ) : NEW_LINE INDENT sequence = generateSequence ( freq , n ) NEW_LINE if ( len ( sequence ) == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( len ( sequence ) ) : NEW_LINE INDENT print ( sequence [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT freq = [ 2 , 2 , 2 , 3 , 1 ] NEW_LINE N = 5 NEW_LINE PrintSequence ( freq , N ) NEW_LINE DEDENT
Minimum distance between any most frequent and least frequent element of an array | Python3 implementation of the approach ; Function to find the minimum distance between any two most and least frequent element ; Initialize sets to store the least and the most frequent elements ; Initialize variables to store max and min frequency ; Initialize HashMap to store frequency of each element ; Loop through the array ; Store the count of each element ; Store the least and most frequent elements in the respective sets ; Store count of current element ; If count is equal to max count ; Store in max set ; If count is greater then max count ; Empty max set ; Update max count ; Store in max set ; If count is equal to min count ; Store in min set ; If count is less then max count ; Empty min set ; Update min count ; Store in min set ; Initialize a variable to store the minimum distance ; Initialize a variable to store the last index of least frequent element ; Traverse array ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Traverse array from the end ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Print the minimum distance ; Driver Code ; Given array ; Function Call
import sys NEW_LINE def getMinimumDistance ( a , n ) : NEW_LINE INDENT min_set = { } NEW_LINE max_set = { } NEW_LINE max , min = 0 , sys . maxsize + 1 NEW_LINE frequency = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT frequency [ a [ i ] ] = frequency . get ( a [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT count = frequency [ a [ i ] ] NEW_LINE if ( count == max ) : NEW_LINE INDENT max_set [ a [ i ] ] = 1 NEW_LINE DEDENT elif ( count > max ) : NEW_LINE INDENT max_set . clear ( ) NEW_LINE max = count NEW_LINE max_set [ a [ i ] ] = 1 NEW_LINE DEDENT if ( count == min ) : NEW_LINE INDENT min_set [ a [ i ] ] = 1 NEW_LINE DEDENT elif ( count < min ) : NEW_LINE INDENT min_set . clear ( ) NEW_LINE min = count NEW_LINE min_set [ a [ i ] ] = 1 NEW_LINE DEDENT DEDENT min_dist = sys . maxsize + 1 NEW_LINE last_min_found = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] in min_set ) : NEW_LINE INDENT last_min_found = i NEW_LINE DEDENT if ( ( a [ i ] in max_set ) and last_min_found != - 1 ) : NEW_LINE INDENT if i - last_min_found < min_dist : NEW_LINE INDENT min_dist = i - last_min_found NEW_LINE DEDENT DEDENT DEDENT last_min_found = - 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] in min_set ) : NEW_LINE INDENT last_min_found = i ; NEW_LINE DEDENT if ( ( a [ i ] in max_set ) and last_min_found != - 1 ) : NEW_LINE INDENT if min_dist > last_min_found - i : NEW_LINE INDENT min_dist = last_min_found - i NEW_LINE DEDENT DEDENT DEDENT print ( min_dist ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 3 , 2 , 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE getMinimumDistance ( arr , N ) NEW_LINE DEDENT
Construct a string that has exactly K subsequences from given string | Python3 program for the above approach ; Function that computes the string s ; Length of the given string str ; List that stores all the prime factors of given k ; Find the prime factors ; Initialize the count of each character position as 1 ; Loop until the list becomes empty ; Increase the character count by multiplying it with the prime factor ; If we reach end then again start from beginning ; store output ; Print the string ; Driver code ; Given String ; Function Call
import math NEW_LINE def printSubsequenceString ( st , k ) : NEW_LINE INDENT n = len ( st ) NEW_LINE factors = [ ] NEW_LINE sqt = ( int ( math . sqrt ( k ) ) ) NEW_LINE for i in range ( 2 , sqt + 1 ) : NEW_LINE INDENT while ( k % i == 0 ) : NEW_LINE INDENT factors . append ( i ) NEW_LINE k //= i NEW_LINE DEDENT DEDENT if ( k > 1 ) : NEW_LINE INDENT factors . append ( k ) NEW_LINE DEDENT count = [ 1 ] * n NEW_LINE index = 0 NEW_LINE while ( len ( factors ) > 0 ) : NEW_LINE INDENT count [ index ] *= factors [ - 1 ] NEW_LINE factors . pop ( ) NEW_LINE index += 1 NEW_LINE if ( index == n ) : NEW_LINE INDENT index = 0 NEW_LINE DEDENT DEDENT s = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( count [ i ] > 0 ) : NEW_LINE INDENT s += st [ i ] NEW_LINE count [ i ] -= 1 NEW_LINE DEDENT DEDENT print ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " code " NEW_LINE k = 20 NEW_LINE printSubsequenceString ( st , k ) NEW_LINE DEDENT
Replace each element of Array with it 's corresponding rank | Function to assign rank to array elements ; Copy input array into newArray ; Sort newArray [ ] in ascending order ; Dictionary to store the rank of the array element ; Update rank of element ; Assign ranks to elements ; Driver Code ; Given array arr [ ] ; Function call ; Print the array elements
def changeArr ( input1 ) : NEW_LINE INDENT newArray = input1 . copy ( ) NEW_LINE newArray . sort ( ) NEW_LINE ranks = { } NEW_LINE rank = 1 NEW_LINE for index in range ( len ( newArray ) ) : NEW_LINE INDENT element = newArray [ index ] ; NEW_LINE if element not in ranks : NEW_LINE INDENT ranks [ element ] = rank NEW_LINE rank += 1 NEW_LINE DEDENT DEDENT for index in range ( len ( input1 ) ) : NEW_LINE INDENT element = input1 [ index ] NEW_LINE input1 [ index ] = ranks [ input1 [ index ] ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 100 , 2 , 70 , 2 ] NEW_LINE changeArr ( arr ) NEW_LINE print ( arr ) NEW_LINE DEDENT
Find pairs in array whose sum does not exist in Array | Function to print all pairs with sum not present in the array ; Corner Case ; Stores the distinct array elements ; Generate all possible pairs ; Calculate sum of current pair ; Check if the sum exists in the HashSet or not ; Driver Code
def findPair ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT hashMap = [ ] NEW_LINE for k in arr : NEW_LINE INDENT hashMap . append ( k ) NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT sum = arr [ i ] + arr [ j ] NEW_LINE if sum not in hashMap : NEW_LINE INDENT print ( " ( " , arr [ i ] , " , ▁ " , arr [ j ] , " ) " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 2 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE findPair ( arr , n ) NEW_LINE DEDENT
Largest subset with M as smallest missing number | Function to find and return the length of the longest subset whose smallest missing value is M ; Initialize a set ; If array element is not equal to M ; Insert into set ; Increment frequency ; Stores minimum missing number ; Iterate to find the minimum missing integer ; If minimum obtained is less than M ; Update answer ; Return answer ; Driver Code
def findLengthOfMaxSubset ( arr , n , m ) : NEW_LINE INDENT s = [ ] ; NEW_LINE answer = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT tmp = arr [ i ] ; NEW_LINE if ( tmp != m ) : NEW_LINE INDENT s . append ( tmp ) ; NEW_LINE answer += 1 ; NEW_LINE DEDENT DEDENT min = 1 ; NEW_LINE while ( s . count ( min ) ) : NEW_LINE INDENT min += 1 ; NEW_LINE DEDENT if ( min != m ) : NEW_LINE INDENT answer = - 1 ; NEW_LINE DEDENT return answer ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE M = 3 ; NEW_LINE print ( findLengthOfMaxSubset ( arr , N , M ) ) ; NEW_LINE DEDENT
Count of submatrix with sum X in a given Matrix | Size of a column ; Function to find the count of submatrix whose sum is X ; Copying arr to dp and making it indexed 1 ; Precalculate and store the sum of all rectangles with upper left corner at ( 0 , 0 ) ; ; Calculating sum in a 2d grid ; Stores the answer ; Minimum length of square ; Maximum length of square ; Flag to set if sub - square with sum X is found ; Calculate lower right index if upper right corner is at { i , j } ; Calculate the sum of elements in the submatrix with upper left column { i , j } and lower right column at { ni , nj } ; ; If sum X is found ; If sum > X , then size of the square with sum X must be less than mid ; If sum < X , then size of the square with sum X must be greater than mid ; If found , increment count by 1 ; ; Driver Code ; Given matrix arr [ ] [ ] ; Function call
m = 5 NEW_LINE def countSubsquare ( arr , n , X ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT dp [ i + 1 ] [ j + 1 ] = arr [ i ] [ j ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] += ( dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT cnt = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT lo = 1 NEW_LINE hi = min ( n - i , m - j ) + 1 NEW_LINE found = False NEW_LINE while ( lo <= hi ) : NEW_LINE INDENT mid = ( lo + hi ) // 2 NEW_LINE ni = i + mid - 1 NEW_LINE nj = j + mid - 1 NEW_LINE sum = ( dp [ ni ] [ nj ] - dp [ ni ] [ j - 1 ] - dp [ i - 1 ] [ nj ] + dp [ i - 1 ] [ j - 1 ] ) NEW_LINE if ( sum >= X ) : NEW_LINE INDENT if ( sum == X ) : NEW_LINE INDENT found = True NEW_LINE DEDENT hi = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT lo = mid + 1 NEW_LINE DEDENT DEDENT if ( found == True ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , X = 4 , 10 NEW_LINE arr = [ [ 2 , 4 , 3 , 2 , 10 ] , [ 3 , 1 , 1 , 1 , 5 ] , [ 1 , 1 , 2 , 1 , 4 ] , [ 2 , 1 , 1 , 1 , 3 ] ] NEW_LINE print ( countSubsquare ( arr , N , X ) ) NEW_LINE DEDENT
Check if two arrays can be made equal by reversing any subarray once | Function to check whether two arrays can be made equal by reversing a sub - array only once ; Integer variable for storing the required starting and ending indices in the array ; Finding the smallest index for which A [ i ] != B [ i ] i . e the starting index of the unequal sub - array ; Finding the largest index for which A [ i ] != B [ i ] i . e the ending index of the unequal sub - array ; Reversing the sub - array A [ start ] , A [ start + 1 ] . . A [ end ] ; Checking whether on reversing the sub - array A [ start ] ... A [ end ] makes the arrays equal ; If any element of the two arrays is unequal print No and return ; Print Yes if arrays are equal after reversing the sub - array ; Driver code
def checkArray ( A , B , N ) : NEW_LINE INDENT start = 0 NEW_LINE end = N - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] != B [ i ] ) : NEW_LINE INDENT start = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( A [ i ] != B [ i ] ) : NEW_LINE INDENT end = i NEW_LINE break NEW_LINE DEDENT DEDENT A [ start : end + 1 ] = reversed ( A [ start : end + 1 ] ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] != B [ i ] ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Yes " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 3 , 2 , 4 ] NEW_LINE B = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( A ) NEW_LINE checkArray ( A , B , N ) NEW_LINE DEDENT
Count of substrings having all distinct characters | Function to count total number of valid substrings ; Stores the count of substrings ; Stores the frequency of characters ; Initialised both pointers to beginning of the string ; If all characters in substring from index i to j are distinct ; Increment count of j - th character ; Add all substring ending at j and starting at any index between i and j to the answer ; Increment 2 nd pointer ; If some characters are repeated or j pointer has reached to end ; Decrement count of j - th character ; Increment first pointer ; Return the final count of substrings ; Driver code
def countSub ( Str ) : NEW_LINE INDENT n = len ( Str ) NEW_LINE ans = 0 NEW_LINE cnt = 26 * [ 0 ] NEW_LINE i , j = 0 , 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( j < n and ( cnt [ ord ( Str [ j ] ) - ord ( ' a ' ) ] == 0 ) ) : NEW_LINE INDENT cnt [ ord ( Str [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE ans += ( j - i + 1 ) NEW_LINE j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt [ ord ( Str [ i ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT Str = " gffg " NEW_LINE print ( countSub ( Str ) ) NEW_LINE
Find largest factor of N such that N / F is less than K | Function to find the largest factor of N which is less than or equal to K ; Initialise the variable to store the largest factor of N <= K ; Loop to find all factors of N ; Check if j is a factor of N or not ; Check if j <= K If yes , then store the larger value between ans and j in ans ; Check if N / j <= K If yes , then store the larger value between ans and j in ans ; Since max value is always stored in ans , the maximum value divisible by N less than or equal to K will be returned . ; Driver Code ; Given N and K ; Function call
def solve ( n , k ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( j * j > n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT if ( n % j == 0 ) : NEW_LINE INDENT if ( j <= k ) : NEW_LINE INDENT ans = max ( ans , j ) ; NEW_LINE DEDENT if ( n // j <= k ) : NEW_LINE INDENT ans = max ( ans , n // j ) ; NEW_LINE DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 ; K = 7 ; NEW_LINE print ( ( N // solve ( N , K ) ) ) ; NEW_LINE DEDENT
Maximum sum subarray of size range [ L , R ] | Python3 program to find maximum sum subarray of size between L and R . ; Function to find maximum sum subarray of size between L and R ; Calculating prefix sum ; Maintain 0 for initial values of i upto R Once i = R , then we need to erase that 0 from our multiset as our first index of subarray cannot be 0 anymore . ; We maintain flag to counter if that initial 0 was erased from set or not . ; Erase 0 from multiset once i = b ; Insert pre [ i - L ] ; Find minimum value in multiset . ; Erase pre [ i - R ] ; Driver code
import sys NEW_LINE def max_sum_subarray ( arr , L , R ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE pre = n * [ 0 ] NEW_LINE pre [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT pre [ i ] = pre [ i - 1 ] + arr [ i ] NEW_LINE DEDENT s1 = [ ] NEW_LINE s1 . append ( 0 ) NEW_LINE ans = - sys . maxsize - 1 NEW_LINE ans = max ( ans , pre [ L - 1 ] ) NEW_LINE flag = 0 NEW_LINE for i in range ( L , n ) : NEW_LINE INDENT if ( i - R >= 0 ) : NEW_LINE INDENT if ( flag == 0 ) : NEW_LINE INDENT s1 . remove ( 0 ) NEW_LINE flag = 1 NEW_LINE DEDENT DEDENT if ( i - L >= 0 ) : NEW_LINE INDENT s1 . append ( pre [ i - L ] ) NEW_LINE DEDENT ans = max ( ans , pre [ i ] - s1 [ 0 ] ) NEW_LINE if ( i - R >= 0 ) : NEW_LINE INDENT s1 . remove ( pre [ i - R ] ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 1 NEW_LINE R = 3 NEW_LINE arr = [ 1 , 2 , 2 , 1 ] NEW_LINE max_sum_subarray ( arr , L , R ) NEW_LINE DEDENT
Count of ways to select K consecutive empty cells from a given Matrix | Function to Traverse the matrix row wise ; Initialize ans ; Traverse row wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , then increment countcons ; Check if number of empty consecutive cells is greater or equal to K , increment the ans ; Return the count ; Function to Traverse the matrix column wise ; Initialize ans ; Traverse column wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , increment countcons ; Check if number of empty consecutive cells is greater than or equal to K , increment the ans ; Return the count ; Driver Code ; If k = 1 only traverse row wise ; Traverse both row and column wise
def rowWise ( v , n , m , k ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT countcons = 0 NEW_LINE for j in range ( m ) : NEW_LINE INDENT if ( v [ i ] [ j ] == '1' ) : NEW_LINE INDENT countcons = 0 NEW_LINE DEDENT else : NEW_LINE INDENT countcons += 1 NEW_LINE DEDENT if ( countcons >= k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT def colWise ( v , n , m , k ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT countcons = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( v [ j ] [ i ] == '1' ) : NEW_LINE INDENT countcons = 0 NEW_LINE DEDENT else : NEW_LINE INDENT countcons += 1 NEW_LINE DEDENT if ( countcons >= k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE m = 3 NEW_LINE k = 1 NEW_LINE v = [ [ '0' , '0' , '0' ] , [ '0' , '0' , '0' ] , [ '0' , '0' , '0' ] ] NEW_LINE if ( k == 1 ) : NEW_LINE INDENT print ( rowWise ( v , n , m , k ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( colWise ( v , n , m , k ) + rowWise ( v , n , m , k ) ) NEW_LINE DEDENT DEDENT
Check if the count of inversions of two given types on an Array are equal or not | Python3 program to implement the above approach ; Function to check if the count of inversion of two types are same or not ; If maximum value is found to be greater than a [ j ] , then that pair of indices ( i , j ) will add extra value to inversion of Type 1 ; Update max ; Driver code
import sys NEW_LINE def solve ( a , n ) : NEW_LINE INDENT mx = - sys . maxsize - 1 NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT if ( mx > a [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT mx = max ( mx , a [ j - 1 ] ) NEW_LINE DEDENT return True NEW_LINE DEDENT a = [ 1 , 0 , 2 ] NEW_LINE n = len ( a ) NEW_LINE possible = solve ( a , n ) NEW_LINE if ( possible != 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT