codeparrot/xlcost-text-to-code huggingface dataset:
Queries to find the Lower Bound of K from Prefix Sum Array with updates using Fenwick Tree | Function to calculate and return the sum of arr [ 0. . index ] ; Traverse ancestors of BITree [ index ] ; Update the sum of current element of BIT to ans ; Update index to that of the parent node in getSum ( ) view by subtracting LSB ( Least Significant Bit ) ; Function to update the Binary Index Tree by replacing all ancestors of index by their respective sum with val ; Traverse all ancestors and sum with ' val ' . ; Add ' val ' to current node of BIT ; Update index to that of the parent node in updateBit ( ) view by adding LSB ( Least Significant Bit ) ; Function to construct the Binary Indexed Tree for the given array ; Initialize the Binary Indexed Tree ; Store the actual values in BITree [ ] using update ( ) ; Function to obtain and return the index of lower_bound of k ; Store the Binary Indexed Tree ; Solve each query in Q ; Update the values of all ancestors of idx ; Driver Code
def getSum ( BITree , index ) : NEW_LINE INDENT ans = 0 NEW_LINE index += 1 NEW_LINE while ( index > 0 ) : NEW_LINE INDENT ans += BITree [ index ] NEW_LINE index -= index & ( - index ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def updateBIT ( BITree , n , index , val ) : NEW_LINE INDENT index = index + 1 NEW_LINE while ( index <= n ) : NEW_LINE INDENT BITree [ index ] += val NEW_LINE index += index & ( - index ) NEW_LINE DEDENT DEDENT def constructBITree ( arr , n ) : NEW_LINE INDENT BITree = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT BITree [ i ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT updateBIT ( BITree , n , i , arr [ i ] ) NEW_LINE DEDENT return BITree NEW_LINE DEDENT def getLowerBound ( BITree , arr , n , k ) : NEW_LINE INDENT lb = - 1 NEW_LINE l = 0 NEW_LINE r = n - 1 NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = l + ( r - l ) // 2 NEW_LINE if ( getSum ( BITree , mid ) >= k ) : NEW_LINE INDENT r = mid - 1 NEW_LINE lb = mid NEW_LINE DEDENT else : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT DEDENT return lb NEW_LINE DEDENT def performQueries ( A , n , q ) : NEW_LINE INDENT BITree = constructBITree ( A , n ) NEW_LINE for i in range ( len ( q ) ) : NEW_LINE INDENT id = q [ i ] [ 0 ] NEW_LINE if ( id == 1 ) : NEW_LINE INDENT idx = q [ i ] [ 1 ] NEW_LINE val = q [ i ] [ 2 ] NEW_LINE A [ idx ] += val NEW_LINE updateBIT ( BITree , n , idx , val ) NEW_LINE DEDENT else : NEW_LINE INDENT k = q [ i ] [ 1 ] NEW_LINE lb = getLowerBound ( BITree , A , n , k ) NEW_LINE print ( lb ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 3 , 5 , 8 ] NEW_LINE n = len ( A ) NEW_LINE q = [ [ 1 , 0 , 2 ] , [ 2 , 5 , 0 ] , [ 1 , 3 , 5 ] ] NEW_LINE performQueries ( A , n , q ) NEW_LINE DEDENT
Minimum number of leaves required to be removed from a Tree to satisfy the given condition | Stores the count of safe nodes ; Function to perform DFS on the Tree to obtain the count of vertices that are not required to be deleted ; Update cost to reach the vertex ; If the vertex does not satisfy the condition ; Otherwise ; Traverse its subtree ; Driver Code ; Stores the Tree ; Perform DFS ; Print the number of nodes to be deleted
cnt = 0 NEW_LINE def dfs ( val , cost , tr , u , s ) : NEW_LINE INDENT global cnt NEW_LINE s = s + cost [ u ] NEW_LINE if ( s < 0 ) : NEW_LINE INDENT s = 0 NEW_LINE DEDENT if ( s > val [ u ] ) : NEW_LINE INDENT return NEW_LINE DEDENT cnt += 1 NEW_LINE for i in range ( 0 , len ( tr [ u ] ) ) : NEW_LINE INDENT dfs ( val , cost , tr , tr [ u ] [ i ] , s ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 9 NEW_LINE val = [ 88 , 22 , 83 , 14 , 95 , 91 , 98 , 53 , 11 ] NEW_LINE cost = [ - 1 , 24 , - 8 , 67 , 64 , 65 , 12 , - 80 , 8 ] NEW_LINE tr = [ [ ] for i in range ( n + 1 ) ] NEW_LINE tr [ 0 ] . append ( 3 ) NEW_LINE tr [ 0 ] . append ( 4 ) NEW_LINE tr [ 4 ] . append ( 6 ) NEW_LINE tr [ 6 ] . append ( 2 ) NEW_LINE tr [ 2 ] . append ( 1 ) NEW_LINE tr [ 2 ] . append ( 8 ) NEW_LINE tr [ 8 ] . append ( 5 ) NEW_LINE tr [ 5 ] . append ( 7 ) NEW_LINE dfs ( val , cost , tr , 0 , 0 ) NEW_LINE print ( n - cnt ) NEW_LINE DEDENT
Check if an Array is made up of Subarrays of continuous repetitions of every distinct element | Function to check if the array is made up of subarrays of repetitions ; Base Case ; Stores the size of current subarray ; If a different element is encountered ; If the previous subarray was a single element ; Reset to new subarray ; Increase size of subarray ; If last element differed from the second last element ; Driver code
def ContinuousElements ( a , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT curr = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] != a [ i - 1 ] ) : NEW_LINE if ( curr == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT curr = 0 NEW_LINE curr += 1 NEW_LINE if ( curr == 1 ) : NEW_LINE return False NEW_LINE return True NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 1 , 2 , 2 , 1 , 3 , 3 ] NEW_LINE n = len ( a ) NEW_LINE if ( ContinuousElements ( a , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Size of all connected non | Python3 program to implement the above approach ; Function to find size of all the islands from the given matrix ; Initialize a queue for the BFS traversal ; Iterate until the queue is empty ; Top element of queue ; Pop the element Q . pop ( ) ; ; Check for boundaries ; Check if current element is 0 ; Check if current element is 1 ; Mark the cell visited ; Incrementing the size ; Traverse all neighbors ; Return the answer ; Function to prsize of each connections ; Stores the size of each connected non - empty ; Check if the cell is non - empty ; Function call ; Print the answer ; Driver Code
from collections import deque NEW_LINE def BFS ( mat , row , col ) : NEW_LINE INDENT area = 0 NEW_LINE Q = deque ( ) NEW_LINE Q . append ( [ row , col ] ) NEW_LINE while ( len ( Q ) > 0 ) : NEW_LINE INDENT it = Q . popleft ( ) NEW_LINE r , c = it [ 0 ] , it [ 1 ] NEW_LINE if ( r < 0 or c < 0 or r > 4 or c > 4 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( mat [ r ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( mat [ r ] == 1 ) : NEW_LINE INDENT mat [ r ] = 0 NEW_LINE area += 1 NEW_LINE DEDENT Q . append ( [ r + 1 , c ] ) NEW_LINE Q . append ( [ r - 1 , c ] ) NEW_LINE Q . append ( [ r , c + 1 ] ) NEW_LINE Q . append ( [ r , c - 1 ] ) NEW_LINE DEDENT return area NEW_LINE DEDENT def sizeOfConnections ( mat ) : NEW_LINE INDENT result = [ ] NEW_LINE for row in range ( 5 ) : NEW_LINE INDENT for col in range ( 5 ) : NEW_LINE INDENT if ( mat [ row ] [ col ] == 1 ) : NEW_LINE INDENT area = BFS ( mat , row , col ) ; NEW_LINE result . append ( area ) NEW_LINE DEDENT DEDENT DEDENT for val in result : NEW_LINE INDENT print ( val , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 1 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 , 1 ] , [ 1 , 0 , 0 , 1 , 1 ] , [ 1 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 1 , 1 , 1 ] ] NEW_LINE sizeOfConnections ( mat ) NEW_LINE DEDENT
Find a pair in Array with second largest product | Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Sort the array ; Initialize smallest element of the array ; Initialize largest element of the array ; Prsecond largest product pair ; Driver Code ; Given array ; Function Call
def maxProduct ( arr , N ) : NEW_LINE INDENT if ( N < 3 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT arr . sort ( ) ; NEW_LINE smallest1 = arr [ 0 ] ; NEW_LINE smallest3 = arr [ 2 ] ; NEW_LINE largest1 = arr [ N - 1 ] ; NEW_LINE largest3 = arr [ N - 3 ] ; NEW_LINE if ( smallest1 * smallest3 >= largest1 * largest3 ) : NEW_LINE INDENT print ( smallest1 , " ▁ " , smallest3 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( largest1 , " ▁ " , largest3 ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 2 , 67 , 45 , 160 , 78 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE maxProduct ( arr , N ) ; NEW_LINE DEDENT
Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | Function to find the maximum subarray length of ones ; Stores the length , starting index and ending index of the subarrays ; S : starting index of the sub - array ; Traverse only continuous 1 s ; Calculate length of the sub - array ; v [ i ] [ 0 ] : Length of subarray v [ i ] [ 1 ] : Starting Index of subarray v [ i ] [ 2 ] : Ending Index of subarray ; If no such sub - array exists ; Traversing through the subarrays ; Update maximum length ; v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 : Count of zeros between the two sub - arrays ; Update length of both subarrays to the maximum ; Update length of both subarrays - 1 to the maximum ; Check if the last subarray has the maximum length ; Driver Code
def maxLen ( A , N ) : NEW_LINE INDENT v = [ ] NEW_LINE i = 0 NEW_LINE while i < N : NEW_LINE INDENT if ( A [ i ] == 1 ) : NEW_LINE INDENT s = i NEW_LINE while ( i < N and A [ i ] == 1 ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT le = i - s NEW_LINE v . append ( [ le , s , i - 1 ] ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( len ( v ) == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( len ( v ) - 1 ) : NEW_LINE INDENT ans = max ( ans , v [ i ] [ 0 ] ) NEW_LINE if ( v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 == 2 ) : NEW_LINE INDENT ans = max ( ans , v [ i ] [ 0 ] + v [ i + 1 ] [ 0 ] ) NEW_LINE DEDENT if ( v [ i + 1 ] [ 1 ] - v [ i ] [ 2 ] - 1 == 1 ) : NEW_LINE INDENT ans = max ( ans , v [ i ] [ 0 ] + v [ i + 1 ] [ 0 ] - 1 ) NEW_LINE DEDENT DEDENT ans = max ( v [ len ( v ) - 1 ] [ 0 ] , ans ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 0 , 1 , 0 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxLen ( arr , N ) ) NEW_LINE DEDENT
Detect cycle in Directed Graph using Topological Sort | Python3 program to implement the above approach ; Stack to store the visited vertices in the Topological Sort ; Store Topological Order ; Adjacency list to store edges ; To ensure visited vertex ; Function to perform DFS ; Set the vertex as visited ; Visit connected vertices ; Push into the stack on complete visit of vertex ; Function to check and return if a cycle exists or not ; Stores the position of vertex in topological order ; Pop all elements from stack ; Push element to get Topological Order ; Pop from the stack ; If parent vertex does not appear first ; Cycle exists ; Return false if cycle does not exist ; Function to add edges from u to v ; Driver Code ; Insert edges ; If cycle exist
t = 0 NEW_LINE n = 0 NEW_LINE m = 0 NEW_LINE a = 0 NEW_LINE s = [ ] NEW_LINE tsort = [ ] NEW_LINE adj = [ [ ] for i in range ( 100001 ) ] NEW_LINE visited = [ False for i in range ( 100001 ) ] NEW_LINE def dfs ( u ) : NEW_LINE INDENT visited [ u ] = 1 NEW_LINE for it in adj [ u ] : NEW_LINE INDENT if ( visited [ it ] == 0 ) : NEW_LINE INDENT dfs ( it ) NEW_LINE DEDENT DEDENT s . append ( u ) NEW_LINE DEDENT def check_cycle ( ) : NEW_LINE INDENT pos = dict ( ) NEW_LINE ind = 0 NEW_LINE while ( len ( s ) != 0 ) : NEW_LINE INDENT pos [ s [ - 1 ] ] = ind NEW_LINE tsort . append ( s [ - 1 ] ) NEW_LINE ind += 1 NEW_LINE s . pop ( ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for it in adj [ i ] : NEW_LINE INDENT first = 0 if i not in pos else pos [ i ] NEW_LINE second = 0 if it not in pos else pos [ it ] NEW_LINE if ( first > second ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT def addEdge ( u , v ) : NEW_LINE INDENT adj [ u ] . append ( v ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE m = 5 NEW_LINE addEdge ( 0 , 1 ) NEW_LINE addEdge ( 0 , 2 ) NEW_LINE addEdge ( 1 , 2 ) NEW_LINE addEdge ( 2 , 0 ) NEW_LINE addEdge ( 2 , 3 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( visited [ i ] == False ) : NEW_LINE INDENT dfs ( i ) NEW_LINE DEDENT DEDENT if ( check_cycle ( ) ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT
Rearrange an Array such that Sum of same | Function to rearrange the array such that no same - indexed subset have sum equal to that in the original array ; Initialize a vector ; Iterate the array ; Sort the vector ; Shift of elements to the index of its next cyclic element ; Print the answer ; Driver Code
def printNewArray ( a , n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT v . append ( ( a [ i ] , i ) ) NEW_LINE DEDENT v . sort ( ) NEW_LINE ans = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans [ v [ ( i + 1 ) % n ] [ 1 ] ] = v [ i ] [ 0 ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 4 , 1 , 2 , 5 , 3 ] NEW_LINE n = len ( a ) NEW_LINE printNewArray ( a , n ) NEW_LINE DEDENT
Minimize count of array elements to be removed to maximize difference between any pair up to K | Python3 program to implement the above approach ; Function to count the number of elements to be removed from the array based on the given condition ; Sort the array ; Initialize the variable ; Iterate for all possible pairs ; Check the difference between the numbers ; Update the minimum removals ; Return the answer ; Driver Code
import sys NEW_LINE def min_remove ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE ans = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT if ( arr [ j ] - arr [ i ] <= k ) : NEW_LINE INDENT ans = min ( ans , n - j + i - 1 ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k = 3 NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( min_remove ( arr , n , k ) ) NEW_LINE DEDENT
Reduce a given Binary Array to a single element by removal of Triplets | Function to check if it is possible to reduce the array to a single element ; Stores frequency of 0 's ; Stores frequency of 1 's ; Condition for array to be reduced ; Otherwise ; Driver Code
def solve ( arr , n ) : NEW_LINE INDENT countzeroes = 0 ; NEW_LINE countones = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT countzeroes += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT countones += 1 ; NEW_LINE DEDENT DEDENT if ( abs ( countzeroes - countones ) == 1 ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 1 , 0 , 0 , 1 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE solve ( arr , n ) ; NEW_LINE DEDENT
Nearest smaller character to a character K from a Sorted Array | Function to return the nearest smaller character ; Stores the nearest smaller character ; Iterate till starts cross end ; Find the mid element ; Check if K is found ; Check if current character is less than K ; Increment the start ; Otherwise ; Increment end ; Return the character ; Driver code
def bs ( a , n , ele ) : NEW_LINE INDENT start = 0 NEW_LINE end = n - 1 NEW_LINE ch = ' @ ' NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = start + ( end - start ) // 2 ; NEW_LINE if ( ar [ mid ] == ele ) : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT elif ( ar [ mid ] < ele ) : NEW_LINE INDENT ch = ar [ mid ] NEW_LINE start = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 ; NEW_LINE DEDENT DEDENT return ch NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ar = [ ' e ' , ' g ' , ' t ' , ' y ' ] NEW_LINE n = len ( ar ) NEW_LINE K = ' u ' ; NEW_LINE ch = bs ( ar , n , K ) ; NEW_LINE if ( ch == ' @ ' ) : NEW_LINE INDENT print ( ' - 1' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ch ) NEW_LINE DEDENT DEDENT
Largest possible Subset from an Array such that no element is K times any other element in the Subset | Function to find the maximum size of the required subset ; Size of the array ; Sort the array ; Stores which index is included or excluded ; Stores the indices of array elements ; Count of pairs ; Iterate through all the element ; If element is included ; Check if a [ i ] * k is present in the array or not ; Increase count of pair ; Exclude the pair ; Driver Code
def findMaxLen ( a , k ) : NEW_LINE INDENT n = len ( a ) NEW_LINE a . sort ( ) NEW_LINE vis = [ 0 ] * n NEW_LINE mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ a [ i ] ] = i NEW_LINE DEDENT c = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( vis [ i ] == False ) : NEW_LINE INDENT check = a [ i ] * k NEW_LINE if ( check in mp . keys ( ) ) : NEW_LINE INDENT c += 1 NEW_LINE vis [ mp [ check ] ] = True NEW_LINE DEDENT DEDENT DEDENT return n - c NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 3 NEW_LINE arr = [ 1 , 4 , 3 , 2 ] NEW_LINE print ( findMaxLen ( arr , K ) ) NEW_LINE DEDENT
Minimize length of prefix of string S containing all characters of another string T | Python3 program for the above approach ; Base Case - if T is empty , it matches 0 length prefix ; Convert strings to lower case for uniformity ; Update dictCount to the letter count of T ; If new character is found , initialize its entry , and increase nUnique ; Increase count of ch ; Iterate from 0 to N ; i - th character ; Skip if ch not in targetStr ; Decrease Count ; If the count of ch reaches 0 , we do not need more ch , and can decrease nUnique ; If nUnique reaches 0 , we have found required prefix ; Otherwise ; Driver Code
def getPrefixLength ( srcStr , targetStr ) : NEW_LINE INDENT if ( len ( targetStr ) == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT srcStr = srcStr . lower ( ) NEW_LINE targetStr = targetStr . lower ( ) NEW_LINE dictCount = dict ( [ ] ) NEW_LINE nUnique = 0 NEW_LINE for ch in targetStr : NEW_LINE INDENT if ( ch not in dictCount ) : NEW_LINE INDENT nUnique += 1 NEW_LINE dictCount [ ch ] = 0 NEW_LINE DEDENT dictCount [ ch ] += 1 NEW_LINE DEDENT for i in range ( len ( srcStr ) ) : NEW_LINE INDENT ch = srcStr [ i ] NEW_LINE if ( ch not in dictCount ) : NEW_LINE INDENT continue NEW_LINE DEDENT dictCount [ ch ] -= 1 NEW_LINE if ( dictCount [ ch ] == 0 ) : NEW_LINE INDENT nUnique -= 1 NEW_LINE DEDENT if ( nUnique == 0 ) : NEW_LINE INDENT return ( i + 1 ) NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " MarvoloGaunt " NEW_LINE T = " Tom " NEW_LINE print ( getPrefixLength ( S , T ) ) NEW_LINE DEDENT
Minimize length of Substrings containing at least one common Character | Function to check and return if substrings of length mid has a common character a ; Length of the string ; Initialise the first occurrence of character a ; Check that distance b / w the current and previous occurrence of character a is less than or equal to mid ; If distance exceeds mid ; Function to check for all the alphabets , if substrings of length mid have a character common ; Check for all 26 alphabets ; Check that char i + a is common in all the substrings of length mid ; If no characters is common ; Function to calculate and return the minm length of substrings ; Initialise low and high ; Perform binary search ; Update mid ; Check if one common character is present in the length of the mid ; Returns the minimum length that contain one common character ; Function to check if all characters are distinct ; Driver Code
def check ( st , mid , a ) : NEW_LINE INDENT n = len ( st ) NEW_LINE previous = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( st [ i ] == chr ( a ) ) : NEW_LINE INDENT if ( i - previous > mid ) : NEW_LINE INDENT return False NEW_LINE DEDENT previous = i NEW_LINE DEDENT DEDENT if ( i - previous > mid ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT def possible ( st , mid ) : NEW_LINE INDENT for i in range ( 26 ) : NEW_LINE INDENT if ( check ( st , mid , i + ord ( ' a ' ) ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def findMinLength ( st ) : NEW_LINE INDENT low = 1 NEW_LINE high = len ( st ) NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( possible ( st , mid ) ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return high + 1 NEW_LINE DEDENT def ifAllDistinct ( st ) : NEW_LINE INDENT s = [ ] NEW_LINE for c in st : NEW_LINE INDENT s . append ( c ) NEW_LINE DEDENT return len ( set ( s ) ) == len ( st ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " geeksforgeeks " NEW_LINE if ( ifAllDistinct ( st ) ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( findMinLength ( st ) ) NEW_LINE DEDENT DEDENT
Maximize product of absolute index difference with K | Function returns maximum possible value of k ; Pointer i make sure that A [ i ] will result in max k ; Stores maximum possible k ; Possible value of k for current pair ( A [ i ] and A [ j ] ) ; If current value exceeds k ; Update the value of k ; Update pointer i ; Return the maxm possible k ; Driver Code
def solve ( A , N ) : NEW_LINE INDENT i = 0 NEW_LINE k = 0 NEW_LINE for j in range ( 1 , N ) : NEW_LINE INDENT tempK = ( min ( A [ i ] , A [ j ] ) // ( j - i ) ) NEW_LINE if ( tempK > k ) : NEW_LINE INDENT k = tempK NEW_LINE DEDENT if ( A [ j ] >= A [ i ] // ( j - i ) ) : NEW_LINE INDENT i = j NEW_LINE DEDENT DEDENT return k NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 10 , 5 , 12 , 15 , 8 ] NEW_LINE N = len ( A ) ; NEW_LINE print ( solve ( A , N ) ) NEW_LINE DEDENT
Split Array into min number of subsets with difference between each pair greater than 1 | Function to Split the array into minimum number of subsets with difference strictly > 1 ; Sort the array ; Traverse through the sorted array ; Check the pairs of elements with difference 1 ; If we find even a single pair with difference equal to 1 , then 2 partitions else only 1 partition ; Print the count of partitions ; Driver Code ; Given array ; Size of the array ; Function call
def split ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE count = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] - arr [ i - 1 ] == 1 ) : NEW_LINE INDENT count = 2 NEW_LINE break NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE split ( arr , n ) NEW_LINE DEDENT
Check if a Palindromic String can be formed by concatenating Substrings of two given Strings | Function to check if a palindromic string can be formed from the substring of given strings ; Boolean array to mark presence of characters ; Check if any of the character of str2 is already marked ; If a common character is found ; If no common character is found ; Driver code
def check ( str1 , str2 ) : NEW_LINE INDENT mark = [ False for i in range ( 26 ) ] NEW_LINE n = len ( str1 ) NEW_LINE m = len ( str2 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mark [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] = True NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT if ( mark [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " abca " NEW_LINE str2 = " efad " NEW_LINE if ( check ( str1 , str2 ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT