codeparrot/xlcost-text-to-code huggingface dataset:
Check if both halves of a string are Palindrome or not | Function to check if both halves of a string are palindrome or not ; Length of string ; Initialize both part as true ; If first half is not palindrome ; If second half is not palindrome ; If both halves are palindrome ; Driver code
def checkPalindrome ( S ) : NEW_LINE INDENT n = len ( S ) NEW_LINE first_half = True NEW_LINE second_half = True NEW_LINE cnt = ( n // 2 ) - 1 NEW_LINE for i in range ( 0 , int ( ( n / 2 ) / 2 ) ) : NEW_LINE INDENT if ( S [ i ] != S [ cnt ] ) : NEW_LINE INDENT first_half = False NEW_LINE break NEW_LINE DEDENT if ( S [ n // 2 + i ] != S [ n // 2 + cnt ] ) : NEW_LINE INDENT second_half = False NEW_LINE break NEW_LINE DEDENT cnt -= 1 NEW_LINE DEDENT if ( first_half and second_half ) : NEW_LINE INDENT print ( ' Yes ' , end = ' ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' , end = ' ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = ' momdad ' NEW_LINE checkPalindrome ( S ) NEW_LINE DEDENT
Count of pairs of strings whose concatenation forms a palindromic string | Python3 program to find palindromic string ; Stores frequency array and its count ; Total number of pairs ; Initializing array of size 26 to store count of character ; Counting occurrence of each character of current string ; Convert each count to parity ( 0 or 1 ) on the basis of its frequency ; Adding to answer ; Frequency of single character can be possibly changed , so change its parity ; Driver code
from collections import defaultdict NEW_LINE def getCount ( N , s ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT a = [ 0 ] * 26 NEW_LINE for j in range ( len ( s [ i ] ) ) : NEW_LINE INDENT a [ ord ( s [ i ] [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for j in range ( 26 ) : NEW_LINE INDENT a [ j ] = a [ j ] % 2 NEW_LINE DEDENT ans += mp [ tuple ( a ) ] NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT changedCount = a [ : ] NEW_LINE if ( a [ j ] == 0 ) : NEW_LINE INDENT changedCount [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT changedCount [ j ] = 0 NEW_LINE DEDENT ans += mp [ tuple ( changedCount ) ] NEW_LINE DEDENT mp [ tuple ( a ) ] += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE A = [ " aab " , " abcac " , " dffe " , " ed " , " aa " , " aade " ] NEW_LINE print ( getCount ( N , A ) ) NEW_LINE DEDENT
Kth Smallest element in a Perfect Binary Search Tree | Python3 program to find K - th smallest element in a perfect BST ; A BST node ; A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; If the tree is empty ; Recur down the left subtree for smaller values ; Recur down the right subtree for smaller values ; Return the ( unchanged ) node pointer ; FUnction to find Kth Smallest element in a perfect BST ; Find the median ( division operation is floored ) ; If the element is at the median ; Calculate the number of nodes in the right and left sub - trees ( division operation is floored ) ; If median is located higher ; If median is located lower ; Driver Code ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 / \ / \ / \ / \ 14 25 35 45 55 65 75 85 ; Function call
kth_smallest = 0 NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , item ) : NEW_LINE INDENT self . key = item NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def insert ( node , key ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return newNode ( key ) NEW_LINE DEDENT if ( key < node . key ) : NEW_LINE INDENT node . left = insert ( node . left , key ) NEW_LINE DEDENT elif ( key > node . key ) : NEW_LINE INDENT node . right = insert ( node . right , key ) NEW_LINE DEDENT return node NEW_LINE DEDENT def KSmallestPerfectBST ( root , k , treeSize ) : NEW_LINE INDENT global kth_smallest NEW_LINE if ( root == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT median_loc = ( treeSize // 2 ) + 1 NEW_LINE if ( k == median_loc ) : NEW_LINE INDENT kth_smallest = root . key NEW_LINE return True NEW_LINE DEDENT newTreeSize = treeSize // 2 NEW_LINE if ( k < median_loc ) : NEW_LINE INDENT return KSmallestPerfectBST ( root . left , k , newTreeSize ) NEW_LINE DEDENT newK = k - median_loc NEW_LINE return KSmallestPerfectBST ( root . right , newK , newTreeSize ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 50 ) NEW_LINE insert ( root , 30 ) NEW_LINE insert ( root , 20 ) NEW_LINE insert ( root , 40 ) NEW_LINE insert ( root , 70 ) NEW_LINE insert ( root , 60 ) NEW_LINE insert ( root , 80 ) NEW_LINE insert ( root , 14 ) NEW_LINE insert ( root , 25 ) NEW_LINE insert ( root , 35 ) NEW_LINE insert ( root , 45 ) NEW_LINE insert ( root , 55 ) NEW_LINE insert ( root , 65 ) NEW_LINE insert ( root , 75 ) NEW_LINE insert ( root , 85 ) NEW_LINE n = 15 NEW_LINE k = 5 NEW_LINE if ( KSmallestPerfectBST ( root , k , n ) ) : NEW_LINE INDENT print ( kth_smallest , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Check if given Binary string follows then given condition or not | Function to check if the string follows rules or not ; Check for the first condition ; Check for the third condition ; Check for the second condition ; Driver code ; Given string ; Function call
def checkrules ( s ) : NEW_LINE INDENT if len ( s ) == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT if s [ 0 ] != '1' : NEW_LINE INDENT return False NEW_LINE DEDENT if len ( s ) > 2 : NEW_LINE INDENT if s [ 1 ] == '0' and s [ 2 ] == '0' : NEW_LINE INDENT return checkrules ( s [ 3 : ] ) NEW_LINE DEDENT DEDENT return checkrules ( s [ 1 : ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = '1111' NEW_LINE if checkrules ( s ) : NEW_LINE INDENT print ( ' valid ▁ string ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' invalid ▁ string ' ) NEW_LINE DEDENT DEDENT
Length of longest subsequence in an Array having all elements as Nude Numbers | Function to check if the number is a Nude number ; Variable initialization ; Integer ' copy ' is converted to a string ; Total digits in the number ; Loop through all digits and check if every digit divides n or not ; flag is used to keep check ; Return true or false as per the condition ; Function to find the longest subsequence which contain all Nude numbers ; Find the length of longest Nude number subsequence ; Given array arr [ ] ; Function call
def isNudeNum ( n ) : NEW_LINE INDENT flag = 0 NEW_LINE copy = n NEW_LINE temp = str ( copy ) NEW_LINE length = len ( temp ) NEW_LINE for i in range ( length ) : NEW_LINE INDENT num = ord ( temp [ i ] ) - ord ( '0' ) NEW_LINE if ( ( num == 0 ) or ( n % num != 0 ) ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT def longestNudeSubseq ( arr , n ) : NEW_LINE INDENT answer = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isNudeNum ( arr [ i ] ) ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT arr = [ 34 , 34 , 2 , 2 , 3 , 333 , 221 , 32 ] NEW_LINE n = len ( arr ) NEW_LINE print ( longestNudeSubseq ( arr , n ) ) NEW_LINE
Longest subarray with difference exactly K between any two distinct values | Function to return the length of the longest sub - array ; Initialize set ; Store 1 st element of sub - array into set ; Check absolute difference between two elements ; If the new element is not present in the set ; If the set contains two elements ; Otherwise ; Update the maximum length ; Remove the set elements ; Driver Code
def longestSubarray ( arr , n , k ) : NEW_LINE INDENT Max = 1 NEW_LINE s = set ( ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( abs ( arr [ i ] - arr [ j ] ) == 0 or abs ( arr [ i ] - arr [ j ] ) == k ) : NEW_LINE INDENT if ( not arr [ j ] in s ) : NEW_LINE INDENT if ( len ( s ) == 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT s . add ( arr [ j ] ) NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( len ( s ) == 2 ) : NEW_LINE INDENT Max = max ( Max , j - i ) NEW_LINE s . clear ( ) NEW_LINE DEDENT else : NEW_LINE INDENT s . clear ( ) NEW_LINE DEDENT DEDENT return Max NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 2 , 2 , 5 , 5 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE K = 1 NEW_LINE length = longestSubarray ( arr , N , K ) NEW_LINE if ( length == 1 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( length ) NEW_LINE DEDENT DEDENT
Longest subarray of non | Function to find the maximum length of a subarray of 1 s after removing at most one 0 ; Stores the count of consecutive 1 's from left ; Stores the count of consecutive 1 's from right ; Traverse left to right ; If cell is non - empty ; Increase count ; If cell is empty ; Store the count of consecutive 1 's  till (i - 1)-th index ; Traverse from right to left ; Store the count of consecutive 1 s till ( i + 1 ) - th index ; Stores the length of longest subarray ; Store the maximum ; If array a contains only 1 s return n else return ans ; Driver code
def longestSubarray ( a , n ) : NEW_LINE INDENT l = [ 0 ] * ( n ) NEW_LINE r = [ 0 ] * ( n ) NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT l [ i ] = count NEW_LINE count = 0 NEW_LINE DEDENT DEDENT count = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT r [ i ] = count NEW_LINE count = 0 NEW_LINE DEDENT DEDENT ans = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT ans = max ( ans , l [ i ] + r [ i ] ) NEW_LINE DEDENT DEDENT return ans < 0 and n or ans NEW_LINE DEDENT arr = [ 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( longestSubarray ( arr , n ) ) NEW_LINE
Find largest factor of N such that N / F is less than K | Function to find the value of X ; Loop to check all the numbers divisible by N that yield minimum N / i value ; Prthe value of packages ; Driver code ; Given N and K ; Function call
def findMaxValue ( N , K ) : NEW_LINE INDENT packages = 0 ; NEW_LINE maxi = 1 ; NEW_LINE for i in range ( 1 , K + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT maxi = max ( maxi , i ) ; NEW_LINE DEDENT DEDENT packages = N // maxi ; NEW_LINE print ( packages ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 ; NEW_LINE K = 7 ; NEW_LINE findMaxValue ( N , K ) ; NEW_LINE DEDENT
Largest square which can be formed using given rectangular blocks | Function to find maximum side length of square ; Sort array in asc order ; Traverse array in desc order ; Driver code ; Given array arr [ ] ; Function Call
def maxSide ( a , n ) : NEW_LINE INDENT sideLength = 0 NEW_LINE a . sort NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] > sideLength ) : NEW_LINE INDENT sideLength += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( sideLength ) NEW_LINE DEDENT N = 6 NEW_LINE arr = [ 3 , 2 , 1 , 5 , 2 , 4 ] NEW_LINE maxSide ( arr , N ) NEW_LINE
Minimize difference after changing all odd elements to even | Function to minimize the difference between two elements of array ; Find all the element which are possible by multiplying 2 to odd numbers ; Sort the array ; Find the minimum difference Iterate and find which adjacent elements have the minimum difference ; Print the minimum difference ; Driver Code ; Given array ; Function Call
def minDiff ( a , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 1 ) : NEW_LINE INDENT a . append ( a [ i ] * 2 ) NEW_LINE DEDENT DEDENT a = sorted ( a ) NEW_LINE mindifference = a [ 1 ] - a [ 0 ] NEW_LINE for i in range ( 1 , len ( a ) ) : NEW_LINE INDENT mindifference = min ( mindifference , a [ i ] - a [ i - 1 ] ) NEW_LINE DEDENT print ( mindifference ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 8 , 13 , 30 , 50 ] NEW_LINE n = len ( arr ) NEW_LINE minDiff ( arr , n ) NEW_LINE DEDENT
Longest subarray having sum K | Set 2 | To store the prefix sum1 array ; Function for searching the lower bound of the subarray ; Iterate until low less than equal to high ; For each mid finding sum1 of sub array less than or equal to k ; Return the final answer ; Function to find the length of subarray with sum1 K ; Initialize sum1 to 0 ; Push the prefix sum1 of the array arr [ ] in prefix [ ] ; Search r for each i ; Update ans ; Print the length of subarray found in the array ; Driver Code ; Given array arr [ ] ; Given sum1 K ; Function Call
v = [ ] NEW_LINE def bin1 ( val , k , n ) : NEW_LINE INDENT global v NEW_LINE lo = 0 NEW_LINE hi = n NEW_LINE mid = 0 NEW_LINE ans = - 1 NEW_LINE while ( lo <= hi ) : NEW_LINE INDENT mid = lo + ( ( hi - lo ) // 2 ) NEW_LINE if ( v [ mid ] - val <= k ) : NEW_LINE INDENT lo = mid + 1 NEW_LINE ans = mid NEW_LINE DEDENT else : NEW_LINE INDENT hi = mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def findSubarraysum1K ( arr , N , K ) : NEW_LINE INDENT global v NEW_LINE sum1 = 0 NEW_LINE v . append ( 0 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum1 += arr [ i ] NEW_LINE v . append ( sum1 ) NEW_LINE DEDENT l = 0 NEW_LINE ans = 0 NEW_LINE r = 0 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT r = bin1 ( v [ i ] , K , N ) NEW_LINE ans = max ( ans , r - i ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 6 , 8 , 14 , 9 , 4 , 11 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE K = 13 NEW_LINE findSubarraysum1K ( arr , N , K ) NEW_LINE DEDENT
Minimum number of reversals to reach node 0 from every other node | Function to find minimum reversals ; Add all adjacent nodes to the node in the graph ; Insert edges in the graph ; Insert edges in the reversed graph ; Create array visited to mark all the visited nodes ; Stores the number of edges to be reversed ; BFS Traversal ; Pop the current node from the queue ; mark the current node visited ; Intitialize count of edges need to be reversed to 0 ; Push adjacent nodes in the reversed graph to the queue , if not visited ; Push adjacent nodes in graph to the queue , if not visited count the number of nodes added to the queue ; Update the reverse edge to the final count ; Return the result ; Driver Code ; Given edges to the graph ; Number of nodes ; Function Call
def minRev ( edges , n ) : NEW_LINE INDENT graph = dict ( ) NEW_LINE graph_rev = dict ( ) NEW_LINE for i in range ( len ( edges ) ) : NEW_LINE INDENT x = edges [ i ] [ 0 ] ; NEW_LINE y = edges [ i ] [ 1 ] ; NEW_LINE if x not in graph : NEW_LINE INDENT graph [ x ] = [ ] NEW_LINE DEDENT graph [ x ] . append ( y ) ; NEW_LINE if y not in graph_rev : NEW_LINE INDENT graph_rev [ y ] = [ ] NEW_LINE DEDENT graph_rev [ y ] . append ( x ) ; NEW_LINE DEDENT q = [ ] NEW_LINE visited = [ 0 for i in range ( n ) ] NEW_LINE q . append ( 0 ) ; NEW_LINE ans = 0 ; NEW_LINE while ( len ( q ) != 0 ) : NEW_LINE INDENT curr = q [ 0 ] NEW_LINE visited [ curr ] = 1 ; NEW_LINE count = 0 ; NEW_LINE q . pop ( 0 ) ; NEW_LINE if curr in graph_rev : NEW_LINE INDENT for i in range ( len ( graph_rev [ curr ] ) ) : NEW_LINE INDENT if ( not visited [ graph_rev [ curr ] [ i ] ] ) : NEW_LINE INDENT q . append ( graph_rev [ curr ] [ i ] ) ; NEW_LINE DEDENT DEDENT DEDENT if curr in graph : NEW_LINE INDENT for i in range ( len ( graph [ curr ] ) ) : NEW_LINE INDENT if ( not visited [ graph [ curr ] [ i ] ] ) : NEW_LINE INDENT q . append ( graph [ curr ] [ i ] ) ; NEW_LINE count += 1 NEW_LINE DEDENT DEDENT DEDENT ans += count ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT edges = [ ] NEW_LINE edges = [ [ 0 , 1 ] , [ 1 , 3 ] , [ 2 , 3 ] , [ 4 , 0 ] , [ 4 , 5 ] ] ; NEW_LINE n = 6 ; NEW_LINE print ( minRev ( edges , n ) ) NEW_LINE DEDENT
Partition a set into two subsets such that difference between max of one and min of other is minimized | Function to split the array ; Sort the array in increasing order ; Calculating the max difference between consecutive elements ; Return the final minimum difference ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
def splitArray ( arr , N ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE result = 10 ** 9 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT result = min ( result , arr [ i ] - arr [ i - 1 ] ) NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 1 , 2 , 6 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( splitArray ( arr , N ) ) NEW_LINE DEDENT
Find the element at R ' th ▁ row ▁ and ▁ C ' th column in given a 2D pattern | Function to find the R ' th ▁ row ▁ and ▁ C ' th column value in the given pattern ; First element of a given row ; Element in the given column ; Driver Code ; Function call
def findValue ( R , C ) : NEW_LINE INDENT k = ( R * ( R - 1 ) ) // 2 + 1 NEW_LINE diff = R + 1 NEW_LINE for i in range ( 1 , C ) : NEW_LINE INDENT k = ( k + diff ) NEW_LINE diff += 1 NEW_LINE DEDENT return k NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT R = 4 NEW_LINE C = 4 NEW_LINE k = findValue ( R , C ) NEW_LINE print ( k ) NEW_LINE DEDENT
Maximize number of groups formed with size not smaller than its largest element | Function that prints the number of maximum groups ; Store the number of occurrence of elements ; Make all groups of similar elements and store the left numbers ; Condition for finding first leftover element ; Condition for current leftover element ; Condition if group size is equal to or more than current element ; Printing maximum number of groups ; Driver Code
def makeGroups ( a , n ) : NEW_LINE INDENT v = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT v [ a [ i ] ] += 1 NEW_LINE DEDENT no_of_groups = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT no_of_groups += v [ i ] // i NEW_LINE v [ i ] = v [ i ] % i NEW_LINE DEDENT i = 1 NEW_LINE total = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( v [ i ] != 0 ) : NEW_LINE INDENT total = v [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT i += 1 NEW_LINE while ( i <= n ) : NEW_LINE INDENT if ( v [ i ] != 0 ) : NEW_LINE INDENT total += v [ i ] NEW_LINE if ( total >= i ) : NEW_LINE INDENT rem = total - i NEW_LINE no_of_groups += 1 NEW_LINE total = rem NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT print ( no_of_groups ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 1 , 2 , 2 ] NEW_LINE size = len ( arr ) NEW_LINE makeGroups ( arr , size ) NEW_LINE DEDENT
Leftmost Column with atleast one 1 in a row | Python3 program to calculate leftmost column having at least a 1 ; Function return leftmost column having at least a 1 ; If current element is 1 decrement column by 1 ; If current element is 0 increment row by 1 ; Driver Code
N = 3 NEW_LINE M = 4 NEW_LINE def findColumn ( mat : list ) -> int : NEW_LINE INDENT row = 0 NEW_LINE col = M - 1 NEW_LINE while row < N and col >= 0 : NEW_LINE INDENT if mat [ row ] [ col ] == 1 : NEW_LINE INDENT col -= 1 NEW_LINE flag = 1 NEW_LINE DEDENT else : NEW_LINE INDENT row += 1 NEW_LINE DEDENT DEDENT col += 1 NEW_LINE if flag : NEW_LINE INDENT return col + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 0 , 0 , 0 , 1 ] , [ 0 , 1 , 1 , 1 ] , [ 0 , 0 , 1 , 1 ] ] NEW_LINE print ( findColumn ( mat ) ) NEW_LINE DEDENT
Find two numbers made up of a given digit such that their difference is divisible by N | Function to implement the above approach ; Hashmap to store remainder - length of the number as key - value pairs ; Iterate till N + 1 length ; Search remainder in the map ; If remainder is not already present insert the length for the corresponding remainder ; Keep increasing M ; To keep M in range of integer ; Length of one number is the current Length ; Length of the other number is the length paired with current remainder in map ; Driver code
def findNumbers ( N , M ) : NEW_LINE INDENT m = M NEW_LINE remLen = { } NEW_LINE for len1 in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT remainder = M % N NEW_LINE if ( remLen . get ( remainder ) == None ) : NEW_LINE INDENT remLen [ remainder ] = len1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT M = M * 10 + m NEW_LINE M = M % N NEW_LINE DEDENT LenA = len1 NEW_LINE LenB = remLen [ remainder ] NEW_LINE for i in range ( LenB ) : NEW_LINE INDENT print ( m , end = " " ) NEW_LINE DEDENT print ( " ▁ " , end = " " ) NEW_LINE for i in range ( LenA ) : NEW_LINE INDENT print ( m , end = " " ) NEW_LINE DEDENT return NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE M = 2 NEW_LINE findNumbers ( N , M ) NEW_LINE DEDENT
Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements | 2D prefix sum for submatrix sum query for matrix ; Function to find the prefix sum of the matrix from i and j ; Function to count the number of ways to select equal sized subarrays such that they have atleast K common elements ; Combining the two arrays ; Calculating the 2D prefix sum ; iterating through all the elements of matrix and considering them to be the bottom right ; applying binary search over side length ; if sum of this submatrix >= k then new search space will be [ low , mid ] ; else new search space will be [ mid + 1 , high ] ; Adding the total submatrices ; Driver Code
prefix_2D = [ [ 0 for x in range ( 2005 ) ] for y in range ( 2005 ) ] NEW_LINE def subMatrixSum ( i , j , length ) : NEW_LINE INDENT return ( prefix_2D [ i ] [ j ] - prefix_2D [ i ] [ j - length ] - prefix_2D [ i - length ] [ j ] + prefix_2D [ i - length ] [ j - length ] ) NEW_LINE DEDENT def numberOfWays ( a , b , n , m , k ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( a [ i - 1 ] == b [ j - 1 ] ) : NEW_LINE INDENT prefix_2D [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT prefix_2D [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT prefix_2D [ i ] [ j ] += prefix_2D [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT prefix_2D [ i ] [ j ] += prefix_2D [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT answer = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT low = 1 NEW_LINE high = min ( i , j ) NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = ( low + high ) >> 1 NEW_LINE if ( subMatrixSum ( i , j , mid ) >= k ) : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT if ( subMatrixSum ( i , j , low ) >= k ) : NEW_LINE INDENT answer += ( min ( i , j ) - low + 1 ) NEW_LINE DEDENT DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE M = 3 NEW_LINE A = [ 1 , 2 ] NEW_LINE B = [ 1 , 2 , 3 ] NEW_LINE K = 1 NEW_LINE print ( numberOfWays ( A , B , N , M , K ) ) NEW_LINE DEDENT