codeparrot/xlcost-text-to-code huggingface dataset:
Sum of indices of Characters removed to obtain an Empty String based on given conditions | Python3 program to implement the above approach ; Function to add index of the deleted character ; If index is beyond the range ; Insert the index of the deleted characeter ; Search over the subtrees to find the desired index ; Function to return count of deleted indices which are to the left of the current index ; Function to generate the sum of indices ; Stores the original index of the characters in sorted order of key ; Traverse the map ; Extract smallest index of smallest character ; Delete the character from the map if it has no remaining occurrence ; Stores the original index ; Count of elements removed to the left of current character ; Current index of the current character ; For 1 - based indexing ; Insert the deleted index in the segment tree ; Final answer ; Driver Code
import math , collections NEW_LINE def add_seg ( seg , start , end , current , index ) : NEW_LINE INDENT if ( index > end or index < start ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( start == end ) : NEW_LINE INDENT seg [ current ] = 1 NEW_LINE return NEW_LINE DEDENT mid = int ( ( start + end ) / 2 ) NEW_LINE add_seg ( seg , start , mid , 2 * current + 1 , index ) NEW_LINE add_seg ( seg , mid + 1 , end , 2 * current + 2 , index ) NEW_LINE seg [ current ] = seg [ 2 * current + 1 ] + seg [ 2 * current + 2 ] NEW_LINE DEDENT def deleted ( seg , l , r , start , end , current ) : NEW_LINE INDENT if ( end < l or start > r ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( start >= l and end <= r ) : NEW_LINE INDENT return seg [ current ] NEW_LINE DEDENT mid = int ( ( start + end ) / 2 ) NEW_LINE return deleted ( seg , l , r , start , mid , 2 * current + 1 ) + deleted ( seg , l , r , mid + 1 , end , 2 * current + 2 ) NEW_LINE DEDENT def sumOfIndices ( s ) : NEW_LINE INDENT N = len ( s ) NEW_LINE x = ( int ) ( math . ceil ( math . log ( N ) / math . log ( 2 ) ) ) NEW_LINE seg_size = 2 * pow ( 2 , x ) - 1 NEW_LINE segment = [ 0 ] * ( seg_size ) NEW_LINE count = 4 NEW_LINE fre = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT key = ( ord ) ( s [ i ] ) NEW_LINE if key in fre : NEW_LINE INDENT que = fre [ key ] NEW_LINE DEDENT else : NEW_LINE INDENT que = collections . deque ( [ ] ) NEW_LINE DEDENT que . append ( i ) NEW_LINE fre [ key ] = que NEW_LINE DEDENT while len ( fre ) > 0 : NEW_LINE INDENT it = list ( fre . keys ( ) ) [ 0 ] NEW_LINE if len ( fre [ it ] ) == 0 : NEW_LINE INDENT del fre [ it ] NEW_LINE DEDENT else : NEW_LINE INDENT que = fre [ it ] NEW_LINE original_index = que [ 0 ] NEW_LINE curr_index = deleted ( segment , 0 , original_index - 1 , 0 , N - 1 , 0 ) NEW_LINE new_index = original_index - curr_index NEW_LINE count += new_index + 1 NEW_LINE add_seg ( segment , 0 , N - 1 , 0 , original_index ) NEW_LINE que . popleft ( ) NEW_LINE fre [ it ] = que NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE sumOfIndices ( s ) NEW_LINE
Maximum Length of Sequence of Sums of prime factors generated by the given operations | Python3 program to implement the above approach ; Smallest prime factor array ; Stores if a number is prime or not ; Function to compute all primes using Sieve of Eratosthenes ; Function for finding smallest prime factors for every integer ; Function to find the sum of prime factors of number ; Add smallest prime factor to the sum ; Reduce N ; Return the answer ; Function to return the length of sequence of for the given number ; If the number is prime ; If a previously computed subproblem occurred ; Calculate the sum of prime factors ; Function to return the maximum length of sequence for the given range ; Pre - calculate primes ; Precalculate smallest prime factors ; Iterate over the range ; Update maximum length ; Driver Code
import sys NEW_LINE spf = [ 0 ] * 100005 NEW_LINE prime = [ False ] * 100005 NEW_LINE dp = [ 0 ] * 100005 NEW_LINE def sieve ( ) : NEW_LINE INDENT for i in range ( 2 , 100005 ) : NEW_LINE INDENT prime [ i ] = True NEW_LINE DEDENT i = 2 NEW_LINE while i * i < 100005 : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , 100005 , i ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def smallestPrimeFactors ( ) : NEW_LINE INDENT for i in range ( 10005 ) : NEW_LINE INDENT spf [ i ] = - 1 NEW_LINE DEDENT i = 2 NEW_LINE while i * i < 100005 : NEW_LINE INDENT for j in range ( i , 100005 , i ) : NEW_LINE INDENT if ( spf [ j ] == - 1 ) : NEW_LINE INDENT spf [ j ] = i NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def sumOfPrimeFactors ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( n > 1 ) : NEW_LINE INDENT ans += spf [ n ] NEW_LINE n //= spf [ n ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def findLength ( n ) : NEW_LINE INDENT if ( prime [ n ] ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ n ] ) : NEW_LINE INDENT return dp [ n ] NEW_LINE DEDENT sum = sumOfPrimeFactors ( n ) NEW_LINE dp [ n ] = 1 + findLength ( sum ) NEW_LINE return dp [ n ] NEW_LINE DEDENT def maxLength ( n , m ) : NEW_LINE INDENT sieve ( ) NEW_LINE smallestPrimeFactors ( ) NEW_LINE ans = - sys . maxsize - 1 NEW_LINE for i in range ( n , m + 1 ) : NEW_LINE INDENT if ( i == 4 ) : NEW_LINE INDENT continue NEW_LINE DEDENT ans = max ( ans , findLength ( i ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 2 NEW_LINE m = 14 NEW_LINE print ( maxLength ( n , m ) ) NEW_LINE
Longest Subarray consisting of unique elements from an Array | Python3 program to implement the above approach ; Function to find largest subarray with no duplicates ; Stores index of array elements ; Update j based on previous occurrence of a [ i ] ; Update ans to store maximum length of subarray ; Store the index of current occurrence of a [ i ] ; Return final ans ; Driver Code
from collections import defaultdict NEW_LINE def largest_subarray ( a , n ) : NEW_LINE INDENT index = defaultdict ( lambda : 0 ) NEW_LINE ans = 0 NEW_LINE j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT j = max ( index [ a [ i ] ] , j ) NEW_LINE ans = max ( ans , i - j + 1 ) NEW_LINE index [ a [ i ] ] = i + 1 NEW_LINE i += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 1 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( largest_subarray ( arr , n ) ) NEW_LINE
Count of Ways to obtain given Sum from the given Array elements | Function to count the number of ways ; Base Case : Reached the end of the array ; Sum is equal to the required sum ; Recursively check if required sum can be obtained by adding current element or by subtracting the current index element ; Function to call dfs ( ) to calculate the number of ways ; Driver Code
def dfs ( nums , S , curr_sum , index ) : NEW_LINE INDENT if ( index == len ( nums ) ) : NEW_LINE INDENT if ( S == curr_sum ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT return ( dfs ( nums , S , curr_sum + nums [ index ] , index + 1 ) + dfs ( nums , S , curr_sum - nums [ index ] , index + 1 ) ) ; NEW_LINE DEDENT def findWays ( nums , S ) : NEW_LINE INDENT return dfs ( nums , S , 0 , 0 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = 3 ; NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE answer = findWays ( arr , S ) ; NEW_LINE print ( answer ) ; NEW_LINE DEDENT
Check if all the Nodes in a Binary Tree having common values are at least D distance apart | Function to create a new node ; Function to count the frequency of node value present in the tree ; Function that returns the max distance between the nodes that have the same key ; If right and left subtree did not have node whose key is value ; Check if the current node is equal to value ; If the left subtree has no node whose key is equal to value ; If the right subtree has no node whose key is equal to value ; Function that finds if the distance between any same nodes is at most K ; Create the mp to look for same value of nodes ; Counting the frequency of nodes ; If the returned value of distance is exceeds dist ; Print the result ; Driver Code
mp = { } NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def frequencyCounts ( root ) : NEW_LINE INDENT global mp NEW_LINE if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT mp [ root . key ] = mp . get ( root . key , 0 ) + 1 NEW_LINE frequencyCounts ( root . left ) NEW_LINE frequencyCounts ( root . right ) NEW_LINE DEDENT def computeDistance ( root , value ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT left = computeDistance ( root . left , value ) NEW_LINE right = computeDistance ( root . right , value ) NEW_LINE if ( left == - 1 and right == - 1 ) : NEW_LINE INDENT if ( root . key == value ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if ( left == - 1 ) : NEW_LINE INDENT return right + 1 NEW_LINE DEDENT if ( right == - 1 ) : NEW_LINE INDENT return left + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 1 + max ( left , right ) NEW_LINE DEDENT return - 1 NEW_LINE DEDENT def solve ( root , dist ) : NEW_LINE INDENT global mp NEW_LINE frequencyCounts ( root ) NEW_LINE flag = 0 NEW_LINE for key , value in mp . items ( ) : NEW_LINE INDENT if ( value > 1 ) : NEW_LINE INDENT result = computeDistance ( root , key ) NEW_LINE if ( result > dist or result == - 1 ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if flag == 0 : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 3 ) NEW_LINE root . right . right = newNode ( 4 ) NEW_LINE root . right . left = newNode ( 4 ) NEW_LINE dist = 7 NEW_LINE solve ( root , dist ) NEW_LINE DEDENT
Minimum Sum of a pair at least K distance apart from an Array | Python3 Program to implement the above approach ; Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Iterate over the array ; Initialize the min value ; Iterate from i + k to N ; Find the minimum ; Update the minimum sum ; Prthe answer ; Driver Code
import sys NEW_LINE def findMinSum ( A , K ) : NEW_LINE INDENT n = len ( A ) ; NEW_LINE minimum_sum = sys . maxsize ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT minimum = sys . maxsize ; NEW_LINE for j in range ( i + K , n , 1 ) : NEW_LINE INDENT minimum = min ( minimum , A [ j ] ) ; NEW_LINE DEDENT if ( minimum == sys . maxsize ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT minimum_sum = min ( minimum_sum , A [ i ] + minimum ) ; NEW_LINE DEDENT print ( minimum_sum ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 2 , 5 , 4 , 3 , 2 , 5 ] ; NEW_LINE K = 3 ; NEW_LINE findMinSum ( A , K ) ; NEW_LINE DEDENT
Absolute distinct count in a Linked List | Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Allocate node ; Insert data ; Point to the head ; Make the new Node the new head ; Function to return the count of distinct absolute values in the linked list ; Create the Head ; Insert nodes
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( 0 ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = ( head_ref ) NEW_LINE ( head_ref ) = new_node NEW_LINE return head_ref NEW_LINE DEDENT def distinctCount ( head_ref1 ) : NEW_LINE INDENT s = set ( ) NEW_LINE ptr1 = head_ref1 NEW_LINE while ( ptr1 != None ) : NEW_LINE INDENT s . add ( abs ( ptr1 . data ) ) NEW_LINE ptr1 = ptr1 . next NEW_LINE DEDENT return len ( s ) NEW_LINE DEDENT head1 = None NEW_LINE head1 = push ( head1 , - 1 ) NEW_LINE head1 = push ( head1 , - 2 ) NEW_LINE head1 = push ( head1 , 0 ) NEW_LINE head1 = push ( head1 , 4 ) NEW_LINE head1 = push ( head1 , 5 ) NEW_LINE head1 = push ( head1 , 8 ) NEW_LINE ans = distinctCount ( head1 ) NEW_LINE print ( ans ) NEW_LINE
Count of all possible pairs having sum of LCM and GCD equal to N | Recursive function to return gcd of a and b ; Function to calculate and return LCM of two numbers ; Function to count pairs whose sum of GCD and LCM is equal to N ; Driver code
def __gcd ( a , b ) : NEW_LINE INDENT if b == 0 : NEW_LINE INDENT return a NEW_LINE DEDENT else : NEW_LINE INDENT return __gcd ( b , a % b ) NEW_LINE DEDENT DEDENT def lcm ( a , b ) : NEW_LINE INDENT return ( a * b ) // __gcd ( a , b ) NEW_LINE DEDENT def countPair ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( __gcd ( i , j ) + lcm ( i , j ) == N ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 14 NEW_LINE print ( countPair ( N ) ) NEW_LINE DEDENT
Minimize cost to color all the vertices of an Undirected Graph using given operation | Python3 program to find the minimum cost to color all vertices of an Undirected Graph ; Function to add edge in the given graph ; Function to perform DFS traversal and find the node with minimum cost ; Update the minimum cost ; Recur for all connected nodes ; Function to calculate and return the minimum cost of coloring all vertices of the Undirected Graph ; Marks if a vertex is visited or not ; Perform DFS traversal ; If vertex is not visited ; Update minimum cost ; Return the final cost ; Driver Code
import sys NEW_LINE MAX = 10 NEW_LINE adj = [ [ ] for i in range ( MAX ) ] NEW_LINE def addEdge ( u , v ) : NEW_LINE INDENT adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE DEDENT def dfs ( v , cost , vis , min_cost_node ) : NEW_LINE INDENT vis [ v ] = True NEW_LINE min_cost_node = min ( min_cost_node , cost [ v - 1 ] ) NEW_LINE for i in range ( len ( adj [ v ] ) ) : NEW_LINE INDENT if ( vis [ adj [ v ] [ i ] ] == False ) : NEW_LINE INDENT min_cost_node = dfs ( adj [ v ] [ i ] , cost , vis , min_cost_node ) NEW_LINE DEDENT DEDENT return min_cost_node NEW_LINE DEDENT def minimumCost ( V , cost ) : NEW_LINE INDENT vis = [ False for i in range ( V + 1 ) ] NEW_LINE min_cost = 0 NEW_LINE for i in range ( 1 , V + 1 ) : NEW_LINE INDENT if ( not vis [ i ] ) : NEW_LINE INDENT min_cost_node = sys . maxsize NEW_LINE min_cost_node = dfs ( i , cost , vis , min_cost_node ) NEW_LINE min_cost += min_cost_node NEW_LINE DEDENT DEDENT return min_cost NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT V = 6 NEW_LINE E = 5 NEW_LINE cost = [ 12 , 25 , 8 , 11 , 10 , 7 ] NEW_LINE addEdge ( 1 , 2 ) NEW_LINE addEdge ( 1 , 3 ) NEW_LINE addEdge ( 3 , 2 ) NEW_LINE addEdge ( 2 , 5 ) NEW_LINE addEdge ( 4 , 6 ) NEW_LINE min_cost = minimumCost ( V , cost ) NEW_LINE print ( min_cost ) NEW_LINE DEDENT
Maximize count of set bits in a root to leaf path in a binary tree | Python3 program to implement the above approach ; Node class ; Initialise constructor ; Function to count the number of 1 in number ; Function to find the maximum count of setbits in a root to leaf ; Check if root is null ; Update the maximum count of setbits ; Traverse left of binary tree ; Traverse right of the binary tree ; Driver code
maxm = 0 NEW_LINE class Node : 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 count_1 ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT count += n & 1 NEW_LINE n >>= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def maxm_setbits ( root , ans ) : NEW_LINE INDENT global maxm NEW_LINE if not root : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . left == None and root . right == None ) : NEW_LINE INDENT ans += count_1 ( root . val ) NEW_LINE maxm = max ( ans , maxm ) NEW_LINE return NEW_LINE DEDENT maxm_setbits ( root . left , ans + count_1 ( root . val ) ) NEW_LINE maxm_setbits ( root . right , ans + count_1 ( root . val ) ) NEW_LINE DEDENT root = Node ( 15 ) NEW_LINE root . left = Node ( 3 ) NEW_LINE root . right = Node ( 7 ) NEW_LINE root . left . left = Node ( 5 ) NEW_LINE root . left . right = Node ( 1 ) NEW_LINE root . right . left = Node ( 31 ) NEW_LINE root . right . right = Node ( 9 ) NEW_LINE maxm_setbits ( root , 0 ) NEW_LINE print ( maxm ) NEW_LINE
Minimize count of divisions by D to obtain at least K equal array elements | Function to return minimum number of moves required ; Stores the number of moves required to obtain respective values from the given array ; Traverse the array ; Insert 0 into V [ a [ i ] ] as it is the initial state ; Insert the moves required to obtain current a [ i ] ; Traverse v [ ] to obtain minimum count of moves ; Check if there are at least K equal elements for v [ i ] ; Add the sum of minimum K moves ; Update answer ; Return the final answer ; Driver Code
def getMinimumMoves ( n , k , d , a ) : NEW_LINE INDENT MAX = 100000 NEW_LINE v = [ ] NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT v . append ( [ ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE v [ a [ i ] ] += [ 0 ] NEW_LINE while ( a [ i ] > 0 ) : NEW_LINE INDENT a [ i ] //= d NEW_LINE cnt += 1 NEW_LINE v [ a [ i ] ] += [ cnt ] NEW_LINE DEDENT DEDENT ans = float ( ' inf ' ) NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT if ( len ( v [ i ] ) >= k ) : NEW_LINE INDENT move = 0 NEW_LINE v [ i ] . sort ( ) NEW_LINE for j in range ( k ) : NEW_LINE INDENT move += v [ i ] [ j ] NEW_LINE DEDENT ans = min ( ans , move ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE K = 3 NEW_LINE D = 2 NEW_LINE A = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE print ( getMinimumMoves ( N , K , D , A ) ) NEW_LINE DEDENT
Longest subarray with odd product | Function to return length of longest subarray with odd product ; If even element is encountered ; Update maximum ; Driver code
def Maxlen ( a , n ) : NEW_LINE INDENT ans = 0 NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] % 2 == 0 : NEW_LINE INDENT count = 0 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 , 7 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( Maxlen ( arr , n ) ) NEW_LINE
Min difference between maximum and minimum element in all Y size subarrays | Function to get the maximum of all the subarrays of size Y ; ith index of maxarr array will be the index upto which Arr [ i ] is maximum ; Stack is used to find the next larger element and keeps track of index of current iteration ; Loop for remaining indexes ; j < i used to keep track whether jth element is inside or outside the window ; Return submax ; Function to get the minimum for all subarrays of size Y ; ith index of minarr array will be the index upto which Arr [ i ] is minimum ; Stack is used to find the next smaller element and keeping track of index of current iteration ; Loop for remaining indexes ; j < i used to keep track whether jth element is inside or outside the window ; Return submin ; Function to get minimum difference ; Create submin and submax arrays ; Store initial difference ; Calculate temporary difference ; Final minimum difference ; Given array arr [ ] ; Given subarray size ; Function call
def get_submaxarr ( arr , n , y ) : NEW_LINE INDENT j = 0 NEW_LINE stk = [ ] NEW_LINE maxarr = [ 0 ] * n NEW_LINE stk . append ( 0 ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT while ( len ( stk ) > 0 and arr [ i ] > arr [ stk [ - 1 ] ] ) : NEW_LINE INDENT maxarr [ stk [ - 1 ] ] = i - 1 NEW_LINE stk . pop ( ) NEW_LINE DEDENT stk . append ( i ) NEW_LINE DEDENT while ( stk ) : NEW_LINE INDENT maxarr [ stk [ - 1 ] ] = n - 1 NEW_LINE stk . pop ( ) NEW_LINE DEDENT submax = [ ] NEW_LINE for i in range ( n - y + 1 ) : NEW_LINE INDENT while ( maxarr [ j ] < i + y - 1 or j < i ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT submax . append ( arr [ j ] ) NEW_LINE DEDENT return submax NEW_LINE DEDENT def get_subminarr ( arr , n , y ) : NEW_LINE INDENT j = 0 NEW_LINE stk = [ ] NEW_LINE minarr = [ 0 ] * n NEW_LINE stk . append ( 0 ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT while ( stk and arr [ i ] < arr [ stk [ - 1 ] ] ) : NEW_LINE INDENT minarr [ stk [ - 1 ] ] = i NEW_LINE stk . pop ( ) NEW_LINE DEDENT stk . append ( i ) NEW_LINE DEDENT while ( stk ) : NEW_LINE INDENT minarr [ stk [ - 1 ] ] = n NEW_LINE stk . pop ( ) NEW_LINE DEDENT submin = [ ] NEW_LINE for i in range ( n - y + 1 ) : NEW_LINE INDENT while ( minarr [ j ] <= i + y - 1 or j < i ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT submin . append ( arr [ j ] ) NEW_LINE DEDENT return submin NEW_LINE DEDENT def getMinDifference ( Arr , N , Y ) : NEW_LINE INDENT submin = get_subminarr ( Arr , N , Y ) NEW_LINE submax = get_submaxarr ( Arr , N , Y ) NEW_LINE minn = submax [ 0 ] - submin [ 0 ] NEW_LINE b = len ( submax ) NEW_LINE for i in range ( 1 , b ) : NEW_LINE INDENT diff = submax [ i ] - submin [ i ] NEW_LINE minn = min ( minn , diff ) NEW_LINE DEDENT print ( minn ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 3 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE Y = 4 NEW_LINE getMinDifference ( arr , N , Y ) NEW_LINE
Least root of given quadratic equation for value greater than equal to K | Python3 program for the above approach ; Function to calculate value of quadratic equation for some x ; Function to calculate the minimum value of x such that F ( x ) >= K using binary search ; Start and end value for binary search ; Binary Search ; Computing F ( mid ) and F ( mid - 1 ) ; If F ( mid ) >= K and F ( mid - 1 ) < K return mid ; If F ( mid ) < K go to mid + 1 to end ; If F ( mid ) > K go to start to mid - 1 ; If no such value exist ; Given coefficients of Equations ; Find minimum value of x
import math NEW_LINE def func ( A , B , C , x ) : NEW_LINE INDENT return ( A * x * x + B * x + C ) NEW_LINE DEDENT def findMinx ( A , B , C , K ) : NEW_LINE INDENT start = 1 NEW_LINE end = math . ceil ( math . sqrt ( K ) ) NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = start + ( end - start ) // 2 NEW_LINE x = func ( A , B , C , mid ) NEW_LINE Y = func ( A , B , C , mid - 1 ) NEW_LINE if ( x >= K and Y < K ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( x < K ) : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT A = 3 NEW_LINE B = 4 NEW_LINE C = 5 NEW_LINE K = 6 NEW_LINE print ( findMinx ( A , B , C , K ) ) NEW_LINE