codeparrot/xlcost-text-to-code huggingface dataset:
Longest Increasing consecutive subsequence | python program to find length of the longest increasing subsequence whose adjacent element differ by 1 ; function that returns the length of the longest increasing subsequence whose adjacent element differ by 1 ; create hashmap to save latest consequent number as " key " and its length as " value " ; stores the length of the longest subsequence that ends with a [ i ] ; iterate for all element ; if a [ i ] - 1 is present before i - th index ; last index of a [ i ] - 1 ; Driver Code
from collections import defaultdict NEW_LINE import sys NEW_LINE def longestSubsequence ( a , n ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE dp = [ 0 for i in range ( n ) ] NEW_LINE maximum = - sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] - 1 in mp : NEW_LINE INDENT lastIndex = mp [ a [ i ] - 1 ] - 1 NEW_LINE dp [ i ] = 1 + dp [ lastIndex ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE DEDENT mp [ a [ i ] ] = i + 1 NEW_LINE maximum = max ( maximum , dp [ i ] ) NEW_LINE DEDENT return maximum NEW_LINE DEDENT a = [ 3 , 10 , 3 , 11 , 4 , 5 , 6 , 7 , 8 , 12 ] NEW_LINE n = len ( a ) NEW_LINE print ( longestSubsequence ( a , n ) ) NEW_LINE
Longest subsequence such that difference between adjacents is one | Set 2 | Python3 implementation to find longest subsequence such that difference between adjacents is one ; function to find longest subsequence such that difference between adjacents is one ; hash table to map the array element with the length of the longest subsequence of which it is a part of and is the last element of that subsequence ; to store the longest length subsequence ; traverse the array elements ; / initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' um ' and its length of subsequence is greater than 'len   ; f ' arr [ i ] + 1' is in ' um ' and its length of subsequence is greater than ' len ' ; update arr [ i ] subsequence length in ' um ' ; update longest length ; required longest length subsequence ; Driver code
from collections import defaultdict NEW_LINE def longLenSub ( arr , n ) : NEW_LINE INDENT um = defaultdict ( lambda : 0 ) NEW_LINE longLen = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT len1 = 0 NEW_LINE if ( arr [ i - 1 ] in um and len1 < um [ arr [ i ] - 1 ] ) : NEW_LINE INDENT len1 = um [ arr [ i ] - 1 ] NEW_LINE DEDENT if ( arr [ i ] + 1 in um and len1 < um [ arr [ i ] + 1 ] ) : NEW_LINE INDENT len1 = um [ arr [ i ] + 1 ] NEW_LINE DEDENT um [ arr [ i ] ] = len1 + 1 NEW_LINE if longLen < um [ arr [ i ] ] : NEW_LINE INDENT longLen = um [ arr [ i ] ] NEW_LINE DEDENT DEDENT return longLen NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Longest ▁ length ▁ subsequence ▁ = " , longLenSub ( arr , n ) ) NEW_LINE
Count nodes having highest value in the path from root to itself in a Binary Tree | ''Python 3 program for the above approach ; ''Stores the ct of nodes which are maximum in the path from root to the current node ; ''Binary Tree Node ; ''Function that performs Inorder Traversal on the Binary Tree ; '' If root does not exist ; '' Check if the node  satisfies the condition ; '' Update the maximum value  and recursively traverse  left and right subtree ; ''Function that counts the good nodes in the given Binary Tree ; '' Perform inorder  Traversal ; '' Return the final count ; ''Driver code ; ''   A Binary Tree  3  / /  2 5  / /  4 6     ; '' Function call ; '' Print the count of good nodes
import sys NEW_LINE ct = 0 NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . val = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def find ( root , mx ) : NEW_LINE INDENT global ct NEW_LINE if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . val >= mx ) : NEW_LINE INDENT ct += 1 NEW_LINE DEDENT find ( root . left , max ( mx , root . val ) ) NEW_LINE find ( root . right , max ( mx , root . val ) ) NEW_LINE DEDENT def NodesMaxInPath ( root ) : NEW_LINE INDENT global ct NEW_LINE find ( root , - sys . maxsize - 1 ) NEW_LINE return ct NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 3 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE answer = NodesMaxInPath ( root ) NEW_LINE print ( answer ) NEW_LINE DEDENT
Longest Consecutive Subsequence | Returns length of the longest contiguous subsequence ; Sort the array ; Insert repeated elements only once in the vector ; Find the maximum length by traversing the array ; Check if the current element is equal to previous element + 1 ; Update the maximum ; Driver code
def findLongestConseqSubseq ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE count = 0 NEW_LINE arr . sort ( ) NEW_LINE v = [ ] NEW_LINE v . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i - 1 ] ) : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT if ( i > 0 and v [ i ] == v [ i - 1 ] + 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count = 1 NEW_LINE DEDENT ans = max ( ans , count ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Length ▁ of ▁ the ▁ Longest ▁ contiguous ▁ subsequence ▁ is " , findLongestConseqSubseq ( arr , n ) ) NEW_LINE
Count smaller primes on the right of each array element | ''Python3 program for the above approach ; ''Function to check if a number is prime or not ; ''Function to update a Binary Tree ; ''Function to find the sum of all the elements which are less than or equal to index ; ''Function to find the number of smaller primes on the right for every array element ; '' Iterate the array in backwards ; '' Calculating the required  number of primes ; '' If current array  element is prime ; '' Update the Fenwick tree ; ''Driver Code ; '' Function call
maxn = int ( 1e6 ) + 5 NEW_LINE BITree = [ 0 ] * ( maxn ) NEW_LINE def is_prime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def update_bitree ( index , value ) : NEW_LINE INDENT while ( index <= maxn ) : NEW_LINE INDENT BITree [ index ] += value NEW_LINE index += ( index & ( - index ) ) NEW_LINE DEDENT DEDENT def sum_bitree ( index ) : NEW_LINE INDENT s = 0 NEW_LINE while ( index > 0 ) : NEW_LINE INDENT s += BITree [ index ] NEW_LINE index -= ( index & ( - index ) ) NEW_LINE DEDENT return s NEW_LINE DEDENT def countSmallerPrimes ( ar , N ) : NEW_LINE INDENT ans = [ 0 ] * ( N ) NEW_LINE global BITree NEW_LINE for i in range ( N - 1 , 0 , - 1 ) : NEW_LINE INDENT ans [ i ] = sum_bitree ( ar [ i ] ) NEW_LINE if ( is_prime ( ar [ i ] ) ) : NEW_LINE INDENT update_bitree ( ar [ i ] , 1 ) NEW_LINE DEDENT DEDENT ans [ 0 ] = 2 NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ar = [ 5 , 5 , 17 , 9 , 12 , 15 , 11 , 7 , 39 , 3 ] NEW_LINE N = len ( ar ) NEW_LINE countSmallerPrimes ( ar , N ) NEW_LINE DEDENT
Longest Consecutive Subsequence | Python program to find longest contiguous subsequence ; Hash all the array elements ; check each possible sequence from the start then update optimal length ; if current element is the starting element of a sequence ; Then check for next elements in the sequence ; update optimal length if this length is more ; Driver code
from sets import Set NEW_LINE def findLongestConseqSubseq ( arr , n ) : NEW_LINE INDENT s = Set ( ) NEW_LINE ans = 0 NEW_LINE for ele in arr : NEW_LINE INDENT s . add ( ele ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] - 1 ) not in s : NEW_LINE INDENT j = arr [ i ] NEW_LINE while ( j in s ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT ans = max ( ans , j - arr [ i ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 7 NEW_LINE arr = [ 1 , 9 , 3 , 10 , 4 , 20 , 2 ] NEW_LINE print " Length ▁ of ▁ the ▁ Longest ▁ contiguous ▁ subsequence ▁ is ▁ " , NEW_LINE print findLongestConseqSubseq ( arr , n ) NEW_LINE DEDENT
Largest increasing subsequence of consecutive integers | Python3 implementation of longest continuous increasing subsequence Function for LIS ; Initialize result ; iterate through array and find end index of LIS and its Size ; print LIS size ; print LIS after setting start element ; Driver Code
def findLIS ( A , n ) : NEW_LINE INDENT hash = dict ( ) NEW_LINE LIS_size , LIS_index = 1 , 0 NEW_LINE hash [ A [ 0 ] ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if A [ i ] - 1 not in hash : NEW_LINE INDENT hash [ A [ i ] - 1 ] = 0 NEW_LINE DEDENT hash [ A [ i ] ] = hash [ A [ i ] - 1 ] + 1 NEW_LINE if LIS_size < hash [ A [ i ] ] : NEW_LINE INDENT LIS_size = hash [ A [ i ] ] NEW_LINE LIS_index = A [ i ] NEW_LINE DEDENT DEDENT print ( " LIS _ size ▁ = " , LIS_size ) NEW_LINE print ( " LIS ▁ : ▁ " , end = " " ) NEW_LINE start = LIS_index - LIS_size + 1 NEW_LINE while start <= LIS_index : NEW_LINE INDENT print ( start , end = " ▁ " ) NEW_LINE start += 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 2 , 5 , 3 , 7 , 4 , 8 , 5 , 13 , 6 ] NEW_LINE n = len ( A ) NEW_LINE findLIS ( A , n ) NEW_LINE DEDENT
Count subsets having distinct even numbers | function to count the required subsets ; inserting even numbers in the set ' us ' single copy of each number is retained ; counting distinct even numbers ; total count of required subsets ; Driver program
def countSubSets ( arr , n ) : NEW_LINE INDENT us = set ( ) NEW_LINE even_count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] % 2 == 0 : NEW_LINE INDENT us . add ( arr [ i ] ) NEW_LINE DEDENT DEDENT even_count = len ( us ) NEW_LINE return pow ( 2 , even_count ) - 1 NEW_LINE DEDENT arr = [ 4 , 2 , 1 , 9 , 2 , 6 , 5 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Numbers ▁ of ▁ subset = " , countSubSets ( arr , n ) ) NEW_LINE
Count distinct elements in every window of size k | Simple Python3 program to count distinct elements in every window of size k ; Counts distinct elements in window of size k ; Traverse the window ; Check if element arr [ i ] exists in arr [ 0. . i - 1 ] ; Counts distinct elements in all windows of size k ; Traverse through every window ; Driver Code
import math as mt NEW_LINE def countWindowDistinct ( win , k ) : NEW_LINE INDENT dist_count = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT j = 0 NEW_LINE while j < i : NEW_LINE INDENT if ( win [ i ] == win [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT j += 1 NEW_LINE DEDENT DEDENT if ( j == i ) : NEW_LINE INDENT dist_count += 1 NEW_LINE DEDENT DEDENT return dist_count NEW_LINE DEDENT def countDistinct ( arr , n , k ) : NEW_LINE INDENT for i in range ( n - k + 1 ) : NEW_LINE INDENT print ( countWindowDistinct ( arr [ i : k + i ] , k ) ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 1 , 3 , 4 , 2 , 3 ] NEW_LINE k = 4 NEW_LINE n = len ( arr ) NEW_LINE countDistinct ( arr , n , k ) NEW_LINE
Iterative method to check if two trees are mirror of each other | Utility function to create and return a new node for a binary tree ; function to check whether the two binary trees are mirrors of each other or not ; iterative inorder traversal of 1 st tree and reverse inoder traversal of 2 nd tree ; if the corresponding nodes in the two traversal have different data values , then they are not mirrors of each other . ; if at any point one root becomes None and the other root is not None , then they are not mirrors . This condition verifies that structures of tree are mirrors of each other . ; we have visited the node and its left subtree . Now , it ' s ▁ right ▁ subtree ' s turn ; we have visited the node and its right subtree . Now , it ' s ▁ left ▁ subtree ' s turn ; both the trees have been completely traversed ; tress are mirrors of each other ; Driver Code ; 1 st binary tree formation 1 ; / \ ; 3 2 ; / \ ; 5 4 ; 2 nd binary tree formation 1 ; / \ ; 2 3 ; / \ ; 4 5 ; function cal
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def areMirrors ( root1 , root2 ) : NEW_LINE INDENT st1 = [ ] NEW_LINE st2 = [ ] NEW_LINE while ( 1 ) : NEW_LINE INDENT while ( root1 and root2 ) : NEW_LINE INDENT if ( root1 . data != root2 . data ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT st1 . append ( root1 ) NEW_LINE st2 . append ( root2 ) NEW_LINE root1 = root1 . left NEW_LINE root2 = root2 . right NEW_LINE DEDENT if ( not ( root1 == None and root2 == None ) ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT if ( not len ( st1 ) == 0 and not len ( st2 ) == 0 ) : NEW_LINE INDENT root1 = st1 [ - 1 ] NEW_LINE root2 = st2 [ - 1 ] NEW_LINE st1 . pop ( - 1 ) NEW_LINE st2 . pop ( - 1 ) NEW_LINE root1 = root1 . right NEW_LINE root2 = root2 . left NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return " Yes " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = newNode ( 1 ) NEW_LINE root1 . left = newNode ( 3 ) NEW_LINE root1 . right = newNode ( 2 ) NEW_LINE root1 . right . left = newNode ( 5 ) NEW_LINE root1 . right . right = newNode ( 4 ) NEW_LINE root2 = newNode ( 1 ) NEW_LINE root2 . left = newNode ( 2 ) NEW_LINE root2 . right = newNode ( 3 ) NEW_LINE root2 . left . left = newNode ( 4 ) NEW_LINE root2 . left . right = newNode ( 5 ) NEW_LINE print ( areMirrors ( root1 , root2 ) ) NEW_LINE DEDENT
Maximum score assigned to a subsequence of numerically consecutive and distinct array elements | ''Function to find the maximum score possible ; '' Base Case ; '' If previously occurred  subproblem occurred ; '' Check if lastpicked element differs  by 1 from the current element ; '' Calculate score by including  the current element ; '' Calculate score by excluding  the current element ; '' Return maximum score from  the two possibilities ; ''Function to print maximum score ; '' DP array to store results ; '' Function call ; ''Driver Code ; '' Given arrays ; '' Function Call
def maximumSum ( a , b , n , index , lastpicked , dp ) : NEW_LINE INDENT if ( index == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ index ] [ lastpicked + 1 ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ lastpicked + 1 ] NEW_LINE DEDENT option1 , option2 = 0 , 0 NEW_LINE if ( lastpicked == - 1 or a [ lastpicked ] != a [ index ] ) : NEW_LINE INDENT option1 = ( b [ index ] + maximumSum ( a , b , n , index + 1 , index , dp ) ) NEW_LINE DEDENT option2 = maximumSum ( a , b , n , index + 1 , lastpicked , dp ) NEW_LINE dp [ index ] [ lastpicked + 1 ] = max ( option1 , option2 ) NEW_LINE return dp [ index ] [ lastpicked + 1 ] NEW_LINE DEDENT def maximumPoints ( arr , brr , n ) : NEW_LINE INDENT index = 0 NEW_LINE lastPicked = - 1 NEW_LINE dp = [ [ - 1 for x in range ( n + 5 ) ] for y in range ( n + 5 ) ] NEW_LINE print ( maximumSum ( arr , brr , n , index , lastPicked , dp ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 3 , 3 , 1 ] NEW_LINE brr = [ - 1 , 2 , 10 , 20 , - 10 , - 9 ] NEW_LINE N = len ( arr ) NEW_LINE maximumPoints ( arr , brr , N ) NEW_LINE DEDENT
Maximum possible sum of a window in an array such that elements of same window in other array are unique | Function to return maximum sum of window in B [ ] according to given constraints . ; Map is used to store elements and their counts . ; Initialize result ; calculating the maximum possible sum for each subarray containing unique elements . ; Remove all duplicate instances of A [ i ] in current window . ; Add current instance of A [ i ] to map and to current sum . ; Update result if current sum is more . ; Driver code
def returnMaxSum ( A , B , n ) : NEW_LINE INDENT mp = set ( ) NEW_LINE result = 0 NEW_LINE curr_sum = curr_begin = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT while A [ i ] in mp : NEW_LINE INDENT mp . remove ( A [ curr_begin ] ) NEW_LINE curr_sum -= B [ curr_begin ] NEW_LINE curr_begin += 1 NEW_LINE DEDENT mp . add ( A [ i ] ) NEW_LINE curr_sum += B [ i ] NEW_LINE result = max ( result , curr_sum ) NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 0 , 1 , 2 , 3 , 0 , 1 , 4 ] NEW_LINE B = [ 9 , 8 , 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( A ) NEW_LINE print ( returnMaxSum ( A , B , n ) ) NEW_LINE DEDENT
Length of second longest sequence of consecutive 1 s in a binary array | ''Function to find maximum and second maximum length ; '' Initialise maximum length ; '' Initialise second maximum length ; '' Initialise count ; '' Iterate over the array ; '' If sequence ends ; '' Reset count ; '' Otherwise ; '' Increase length  of current sequence ; '' Update maximum ; '' Traverse the given array ; '' If sequence continues ; '' Increase length  of current sequence ; '' Update second max ; '' Reset count when 0 is found ; '' Print the result ; ''Driver Code ; '' Given array ; '' Function Call
def FindMax ( arr , N ) : NEW_LINE INDENT maxi = - 1 NEW_LINE maxi2 = - 1 NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT count = 0 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE maxi = max ( maxi , count ) NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE if ( count > maxi2 and count < maxi ) : NEW_LINE INDENT maxi2 = count NEW_LINE DEDENT DEDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT count = 0 NEW_LINE DEDENT DEDENT maxi = max ( maxi , 0 ) NEW_LINE maxi2 = max ( maxi2 , 0 ) NEW_LINE print ( maxi2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 0 , 0 , 1 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE FindMax ( arr , N ) NEW_LINE DEDENT
Minimum distance between any special pair in the given array | ''Python3 program for the above approach ; ''Function that finds the minimum difference between two vectors ; '' Find lower bound of the index ; '' Find two adjacent indices  to take difference ; '' Return the result ; ''Function to find the minimum distance between index of special pairs ; '' Stores the index of each element  in the array arr[] ; Store the indexes ; Get the unique values in list ; '' Take adjacent difference  of same values ; '' Left index array ; '' Right index array ; '' Find the minimum gap between  the two adjacent different  values ; ''Driver Code ; '' Given array  ; '' Function call
import sys NEW_LINE def mindist ( left , right ) : NEW_LINE INDENT res = sys . maxsize NEW_LINE for i in range ( len ( left ) ) : NEW_LINE INDENT num = left [ i ] NEW_LINE index = right . index ( min ( [ i for i in right if num >= i ] ) ) NEW_LINE if ( index == 0 ) : NEW_LINE INDENT res = min ( res , abs ( num - right [ index ] ) ) NEW_LINE DEDENT elif ( index == len ( right ) ) : NEW_LINE INDENT res = min ( res , min ( abs ( num - right [ index - 1 ] ) , abs ( num - right [ index ] ) ) ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def specialPairs ( nums ) : NEW_LINE INDENT m = { } NEW_LINE vals = [ ] NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT m [ nums [ i ] ] = i NEW_LINE DEDENT for p in m : NEW_LINE INDENT vals . append ( p ) NEW_LINE DEDENT res = sys . maxsize NEW_LINE for i in range ( 1 , len ( vals ) ) : NEW_LINE INDENT vec = [ m [ vals [ i ] ] ] NEW_LINE for i in range ( 1 , len ( vec ) ) : NEW_LINE INDENT res = min ( res , abs ( vec [ i ] - vec [ i - 1 ] ) ) NEW_LINE DEDENT if ( i ) : NEW_LINE INDENT a = vals [ i ] NEW_LINE left = [ m [ a ] ] NEW_LINE b = vals [ i - 1 ] NEW_LINE right = [ m [ b ] ] NEW_LINE res = min ( res , mindist ( left , right ) ) + 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , - 10 , 5 , - 5 , 1 ] NEW_LINE print ( specialPairs ( arr ) ) NEW_LINE DEDENT
Substring of length K having maximum frequency in the given string | ''Python3 program for the above approach ; ''Function that generates substring of length K that occurs maximum times ; '' Store the frequency of substrings ; '' Deque to maintain substrings  window size K ; '' Update the frequency of the  first subin the Map  E="".join(list(D ; '' Remove the first character of  the previous K length substring ; '' Traverse the string ; '' Insert the current character  as last character of  current substring ; '' Pop the first character of  previous K length substring ; '' Find the subthat occurs  maximum number of times  print(M) ; '' Print the substring ; ''Driver Code ; '' Given string ; '' Given K size of substring ; '' Function call
from collections import deque , Counter , defaultdict NEW_LINE import sys NEW_LINE def maximumOccurringString ( s , K ) : NEW_LINE INDENT M = { } NEW_LINE D = deque ( ) NEW_LINE for i in range ( K ) : NEW_LINE INDENT D . append ( s [ i ] ) NEW_LINE DEDENT M [ str ( " " . join ( list ( D ) ) ) ] = M . get ( str ( " " . join ( list ( D ) ) ) , 0 ) + 1 NEW_LINE D . popleft ( ) NEW_LINE for j in range ( i , len ( s ) ) : NEW_LINE INDENT D . append ( s [ j ] ) NEW_LINE M [ str ( " " . join ( list ( D ) ) ) ] = M . get ( str ( " " . join ( list ( D ) ) ) , 0 ) + 1 NEW_LINE D . popleft ( ) NEW_LINE DEDENT maxi = - sys . maxsize - 1 NEW_LINE ans = deque ( ) NEW_LINE for it in M : NEW_LINE INDENT if ( M [ it ] >= maxi ) : NEW_LINE INDENT maxi = M [ it ] NEW_LINE ans = it NEW_LINE DEDENT DEDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " bbbbbaaaaabbabababa " NEW_LINE K = 5 NEW_LINE maximumOccurringString ( s , K ) NEW_LINE DEDENT