codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs from a given array whose sum lies from a given range | Python3 program for the above approach ; Function to count pairs whose sum lies over the range [ L , R ] ; Sort the given array ; Iterate until right > 0 ; Starting index of element whose sum with arr [ right ] >= L ; Ending index of element whose sum with arr [ right ] <= R ; Update the value of end ; Add the count of elements to the variable count ; Return the value of count ; Driver Code
from bisect import bisect_left , bisect_right NEW_LINE def countPairSum ( arr , L , R , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE right = N - 1 NEW_LINE count = 0 NEW_LINE while ( right > 0 ) : NEW_LINE INDENT it1 = bisect_left ( arr , L - arr [ right ] ) NEW_LINE start = it1 NEW_LINE it2 = bisect_right ( arr , R - arr [ right ] ) NEW_LINE it2 -= 1 NEW_LINE end = it2 NEW_LINE end = min ( end , right - 1 ) NEW_LINE if ( end - start >= 0 ) : NEW_LINE INDENT count += ( end - start + 1 ) NEW_LINE DEDENT right -= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 1 , 2 , 4 , 3 ] NEW_LINE L = 5 NEW_LINE R = 8 NEW_LINE N = len ( arr ) NEW_LINE print ( countPairSum ( arr , L , R , N ) ) NEW_LINE DEDENT
Maximize median of a KxK sub | Function to determine if a given value can be median ; Stores the prefix sum array ; Traverse the matrix arr [ ] [ ] ; Update Pre [ i + 1 ] [ j + 1 ] ; If arr [ i ] [ j ] is less than or equal to mid ; Stores the count of elements should be less than mid ; Stores if the median mid can be possible or not ; Iterate over the range [ K , N ] ; Iterate over the range [ K , N ] ; Stores count of elements less than or equal to the value mid in submatrix with bottom right vertices at ( i , j ) ; If X is less than or equal to required ; Return flag ; Function to find the maximum median of a subsquare of the given size ; Stores the range of the search space ; Iterate until low is less than high ; Stores the mid value of the range [ low , high ] ; If the current median can be possible ; Update the value of low ; Update the value of high ; Return value stored in low as answer ; Driver Code
def isMaximumMedian ( arr , N , K , mid ) : NEW_LINE INDENT Pre = [ [ 0 for x in range ( N + 5 ) ] for y in range ( N + 5 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT Pre [ i + 1 ] [ j + 1 ] = ( Pre [ i + 1 ] [ j ] + Pre [ i ] [ j + 1 ] - Pre [ i ] [ j ] ) NEW_LINE if ( arr [ i ] [ j ] <= mid ) : NEW_LINE INDENT Pre [ i + 1 ] [ j + 1 ] += 1 NEW_LINE DEDENT DEDENT DEDENT required = ( K * K + 1 ) // 2 NEW_LINE flag = 0 NEW_LINE for i in range ( K , N + 1 ) : NEW_LINE INDENT for j in range ( K , N + 1 ) : NEW_LINE INDENT X = ( Pre [ i ] [ j ] - Pre [ i - K ] [ j ] - Pre [ i ] [ j - K ] + Pre [ i - K ] [ j - K ] ) NEW_LINE if ( X < required ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT DEDENT return flag NEW_LINE DEDENT def maximumMedian ( arr , N , K ) : NEW_LINE INDENT low = 0 NEW_LINE high = 1000000009 NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( isMaximumMedian ( arr , N , K , mid ) ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid NEW_LINE DEDENT DEDENT return low NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 5 , 12 ] , [ 6 , 7 , 11 ] , [ 8 , 9 , 10 ] ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE print ( maximumMedian ( arr , N , K ) ) NEW_LINE DEDENT
Minimum days to make Array elements with value at least K sum at least X | Function to find the minimum number of days such that the sum of array elements >= K is at least X ; Initialize the boundaries of search space ; Perform the binary search ; Find the value of mid ; Traverse the array , arr [ ] ; Find the value of arr [ i ] after mid number of days ; Check if temp is not less than K ; Update the value of sum ; Check if the value of sum is greater than X ; Update value of high ; Update the value of low ; Print the minimum number of days ; Driver Code
def findMinDays ( arr , R , N , X , K ) : NEW_LINE INDENT low = 0 NEW_LINE high = X NEW_LINE minDays = 0 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp = arr [ i ] + R [ i ] * mid NEW_LINE if ( temp >= K ) : NEW_LINE INDENT sum += temp NEW_LINE DEDENT DEDENT if ( sum >= X ) : NEW_LINE INDENT minDays = mid NEW_LINE high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT print ( minDays ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 100 NEW_LINE K = 45 NEW_LINE arr = [ 2 , 5 , 2 , 6 ] NEW_LINE R = [ 10 , 13 , 15 , 12 ] NEW_LINE N = len ( arr ) NEW_LINE findMinDays ( arr , R , N , X , K ) NEW_LINE DEDENT
Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Generate all possible pairs from the array arr [ ] ; If the given condition satisfy then increment the value of count ; Return the resultant count ; Driver Code
def getPairsCount ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( ( a [ i ] * a [ j ] ) == abs ( i - j ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( getPairsCount ( arr , N ) ) NEW_LINE DEDENT
Lexicographically smallest string formed repeatedly deleting character from substring 10 | Function to find smallest lexicogra - phically smallest string ; Stores the index of last occuring 0 ; Stores the lexicographically smallest string ; Traverse the S ; If str [ i ] is 0 ; Assign i to lastZe ; Traverse the str ; If i is less than or equal to lastZe and str [ i ] is 0 ; If i is greater than lastZe ; Return ans ; Driver Code ; Input ; Function Call
def lexicographicallySmallestString ( S , N ) : NEW_LINE INDENT LastZe = - 1 NEW_LINE ans = " " NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT LastZe = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( i <= LastZe and S [ i ] == '0' ) : NEW_LINE INDENT ans += S [ i ] NEW_LINE DEDENT elif ( i > LastZe ) : NEW_LINE INDENT ans += S [ i ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "11001101" NEW_LINE N = len ( S ) NEW_LINE print ( lexicographicallySmallestString ( S , N ) ) NEW_LINE DEDENT
Count of Nodes at distance K from S in its subtree for Q queries | Python3 program for the above approach ; Function to add edges ; Function to perform Depth First Search ; Stores the entry time of a node ; Stores the entering time of a node at depth d ; Iterate over the children of node ; Stores the Exit time of a node ; Function to find number of nodes at distance K from node S in the subtree of S ; Distance from root node ; Index of node with greater tin value then tin [ S ] ; Index of node with greater tout value then tout [ S ] ; Answer to the Query ; Function for performing DFS and answer to queries ; DFS function call ; Traverse the array Q [ ] ; Driver Code ; Input ; Function call
from bisect import bisect_left , bisect_right NEW_LINE tin = [ 0 ] * 100 NEW_LINE tout = [ 0 ] * 100 NEW_LINE depth = [ 0 ] * 100 NEW_LINE t = 0 NEW_LINE def Add_edge ( parent , child , adj ) : NEW_LINE INDENT adj [ parent ] . append ( child ) NEW_LINE adj [ child ] . append ( parent ) NEW_LINE return adj NEW_LINE DEDENT def dfs ( node , parent , d ) : NEW_LINE INDENT global tin , tout , depth , adj , levels , t NEW_LINE tin [ node ] = t NEW_LINE t += 1 NEW_LINE levels [ d ] . append ( tin [ node ] ) NEW_LINE depth [ node ] = d NEW_LINE for x in adj [ node ] : NEW_LINE INDENT if ( x != parent ) : NEW_LINE INDENT dfs ( x , node , d + 1 ) NEW_LINE DEDENT DEDENT tout [ node ] = t NEW_LINE t += 1 NEW_LINE DEDENT def numberOfNodes ( node , dist ) : NEW_LINE INDENT global levels , tin , tout NEW_LINE dist += depth [ node ] NEW_LINE start = bisect_left ( levels [ dist ] , tin [ node ] ) NEW_LINE ed = bisect_left ( levels [ dist ] , tout [ node ] ) NEW_LINE print ( ed - start ) NEW_LINE DEDENT def numberOfNodesUtil ( Q , M , N ) : NEW_LINE INDENT global t , adj NEW_LINE adj = Add_edge ( 1 , 2 , adj ) NEW_LINE adj = Add_edge ( 1 , 3 , adj ) NEW_LINE adj = Add_edge ( 2 , 4 , adj ) NEW_LINE adj = Add_edge ( 2 , 5 , adj ) NEW_LINE adj = Add_edge ( 2 , 6 , adj ) NEW_LINE t = 1 NEW_LINE dfs ( 1 , 1 , 0 ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT numberOfNodes ( Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE Q = [ [ 2 , 1 ] , [ 1 , 1 ] ] NEW_LINE M = len ( Q ) NEW_LINE adj = [ [ ] for i in range ( N + 5 ) ] NEW_LINE levels = [ [ ] for i in range ( N + 5 ) ] NEW_LINE numberOfNodesUtil ( Q , M , N ) NEW_LINE DEDENT
Maximize boxes required to keep at least one black and one white shirt | Function to find the maximum number of boxes such that each box contains three shirts comprising of at least one white and black shirt ; Stores the low and high pointers for binary search ; Store the required answer ; Loop while low <= high ; Store the mid value ; Check if the mid number of boxes can be used ; Update answer and recur for the right half ; Else , recur for the left half ; Print result ; Driver Code
def numberofBoxes ( W , B , O ) : NEW_LINE INDENT low = 0 NEW_LINE high = min ( W , B ) NEW_LINE ans = 0 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( ( ( W >= mid ) and ( B >= mid ) ) and ( ( W - mid ) + ( B - mid ) + O ) >= mid ) : NEW_LINE INDENT ans = mid NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT W = 3 NEW_LINE B = 3 NEW_LINE O = 1 NEW_LINE numberofBoxes ( W , B , O ) NEW_LINE DEDENT
Minimum swaps needed to convert given Binary Matrix A to Binary Matrix B | Function to count the minimum number of swaps required to convert matrix A to matrix B ; Stores number of cells such that matrix A contains 0 and matrix B contains 1 ; Stores number of cells such that matrix A contains 1 and matrix B contains 0 ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; If A [ i ] [ j ] = 1 and B [ i ] [ j ] = 0 ; If A [ i ] [ j ] = 0 and B [ i ] [ j ] = 1 ; If count01 is equal to count10 ; Otherwise , ; Driver Code
def minSwaps ( N , M , A , B ) : NEW_LINE INDENT count01 = 0 NEW_LINE count10 = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , M ) : NEW_LINE INDENT if ( A [ i ] [ j ] != B [ i ] [ j ] ) : NEW_LINE INDENT if ( A [ i ] [ j ] == 1 ) : NEW_LINE INDENT count10 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count01 += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( count01 == count10 ) : NEW_LINE INDENT return count01 NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT A = [ [ 1 , 1 , 0 ] , [ 0 , 0 , 1 ] , [ 0 , 1 , 0 ] ] NEW_LINE B = [ [ 0 , 0 , 1 ] , [ 0 , 1 , 0 ] , [ 1 , 1 , 0 ] ] NEW_LINE N = len ( A ) NEW_LINE M = len ( B [ 0 ] ) NEW_LINE print ( minSwaps ( N , M , A , B ) ) NEW_LINE
Maximize X such that sum of numbers in range [ 1 , X ] is at most K | Function to count the elements with sum of the first that many natural numbers less than or equal to K ; If K equals to 0 ; Stores sum of first i natural numbers ; Stores the result ; Iterate over the range [ 1 , N ] ; Increment sum by i ; Is sum is less than or equal to K ; Otherwise , ; Return res ; Driver Code ; Input ; Function call
def Count ( N , K ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT sum = 0 NEW_LINE res = 0 NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT sum += i NEW_LINE if ( sum <= K ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE K = 14 NEW_LINE print ( Count ( N , K ) ) NEW_LINE DEDENT
Remove last occurrence of a word from a given sentence string | Function to remove last occurrence of W from S ; If M is greater than N ; Iterate while i is greater than or equal to 0 ; of W has been found or not ; Iterate over the range [ 0 , M ] ; If S [ j + 1 ] is not equal to W [ j ] ; Mark flag true and break ; If occurrence has been found ; Delete the subover the range [ i , i + M ] ; Resize the S ; Return S ; Driver Code ; Input ; Function call
def removeLastOccurrence ( S , W , N , M ) : NEW_LINE INDENT S = [ i for i in S ] NEW_LINE W = [ i for i in W ] NEW_LINE if ( M > N ) : NEW_LINE INDENT return S NEW_LINE DEDENT for i in range ( N - M , - 1 , - 1 ) : NEW_LINE INDENT flag = 0 NEW_LINE for j in range ( M ) : NEW_LINE INDENT if ( S [ j + i ] != W [ j ] ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT for j in range ( i , N - M ) : NEW_LINE INDENT S [ j ] = S [ j + M ] NEW_LINE DEDENT S = S [ : N - M ] NEW_LINE break NEW_LINE DEDENT DEDENT return " " . join ( S ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " This ▁ is ▁ GeeksForGeeks " NEW_LINE W = " Geeks " NEW_LINE N = len ( S ) NEW_LINE M = len ( W ) NEW_LINE print ( removeLastOccurrence ( S , W , N , M ) ) NEW_LINE DEDENT
Rearrange the Array to maximize the elements which is smaller than both its adjacent elements | Function to rearrange array such that count of element that are smaller than their adjacent elements is maximum ; Stores the rearranged array ; Stores the maximum count of elements ; Sort the given array ; Place the smallest ( N - 1 ) / 2 elements at odd indices ; Placing the rest of the elements at remaining indices ; If no element of the array has been placed here ; Print the resultant array ; Input ; Function call
def maximumIndices ( arr , N ) : NEW_LINE INDENT temp = [ 0 ] * N NEW_LINE maxIndices = ( N - 1 ) // 2 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( maxIndices ) : NEW_LINE INDENT temp [ 2 * i + 1 ] = arr [ i ] NEW_LINE DEDENT j = 0 NEW_LINE i = maxIndices NEW_LINE while ( i < N ) : NEW_LINE INDENT if ( temp [ j ] == 0 ) : NEW_LINE INDENT temp [ j ] = arr [ i ] NEW_LINE i += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( temp [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE maximumIndices ( arr , N ) NEW_LINE
Check if given Strings can be made equal by inserting at most 1 String | Python3 program for the above approach ; Function to check whether two sentences can be made equal by inserting at most one sentence in one of them ; Size of sentence S1 ; Size of sentence S2 ; Check if S1 and S2 are of equal sizes ; If both sentences are the same , return True ; Otherwise , return false ; Declare 2 deques X and Y ; Insert ' ▁ ' at the end of both sentences so that the last word can be identified ; Traverse the sentence S1 ; Push temp in deque when a space comes in sentence i . e a word has been formed ; temp stores words of the sentence ; Traverse the sentence S1 ; Push temp in deque when a space comes in sentence i . e a word has been formed ; temp stores words of the sentence ; Check for prefixes of both sentences ; Pop the prefix from both deques till they are equal ; Check for suffixes of both sentences ; Pop the suffix from both deques till they are equal ; If any of the deques is empty return True ; If both the deques are not empty return false ; Driver code ; Input ; Function call
from collections import deque NEW_LINE def areSimilar ( S1 , S2 ) : NEW_LINE INDENT S1 = [ i for i in S1 ] NEW_LINE S2 = [ i for i in S2 ] NEW_LINE N = len ( S1 ) NEW_LINE M = len ( S2 ) NEW_LINE if ( N == M ) : NEW_LINE INDENT if ( S1 == S2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT X , Y = deque ( ) , deque ( ) NEW_LINE S1 . append ( ' ▁ ' ) NEW_LINE S2 . append ( ' ▁ ' ) NEW_LINE temp = " " NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT if ( S1 [ i ] == ' ▁ ' ) : NEW_LINE INDENT X . append ( temp ) NEW_LINE temp = " " NEW_LINE DEDENT else : NEW_LINE INDENT temp += S1 [ i ] NEW_LINE DEDENT DEDENT for i in range ( M + 1 ) : NEW_LINE INDENT if ( S2 [ i ] == ' ▁ ' ) : NEW_LINE INDENT Y . append ( temp ) NEW_LINE temp = " " NEW_LINE DEDENT else : NEW_LINE INDENT temp += S2 [ i ] NEW_LINE DEDENT DEDENT while ( len ( X ) > 0 and len ( Y ) > 0 and X [ 0 ] == Y [ 0 ] ) : NEW_LINE INDENT X . popleft ( ) NEW_LINE Y . popleft ( ) NEW_LINE DEDENT while ( len ( X ) > 0 and len ( Y ) > 0 and X [ - 1 ] == Y [ - 1 ] ) : NEW_LINE INDENT X . pop ( ) NEW_LINE Y . pop ( ) NEW_LINE DEDENT if ( len ( X ) == 0 or len ( Y ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S1 = " Start ▁ practicing ▁ on ▁ GeeksforGeeks " NEW_LINE S2 = " Start ▁ GeeksforGeeks " NEW_LINE if ( areSimilar ( S1 , S2 ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT DEDENT
Find the repeating element in an Array of size N consisting of first M natural numbers | Function to calculate the repeating character in a given permutation ; variables to store maximum element and sum of the array respectively . ; calculate sum of array ; calculate maximum element in the array ; calculating sum of permutation ; calculate required answer ; Driver code ; Input ; Function call
def repeatingElement ( arr , N ) : NEW_LINE INDENT M = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE M = max ( M , arr [ i ] ) NEW_LINE DEDENT sum1 = M * ( M + 1 ) // 2 NEW_LINE ans = ( sum - sum1 ) // ( N - M ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 6 , 4 , 3 , 1 , 5 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( repeatingElement ( arr , N ) ) NEW_LINE DEDENT
Maximize value at Kth index to create N size array with adjacent difference 1 and sum less than M | Function to calculate maximum value that can be placed at the Kth index in a distribution in which difference of adjacent elements is less than 1 and total sum of distribution is M . ; Variable to store final answer ; Variables for binary search ; Binary search ; Variable for binary search ; Variable to store total sum of array ; Number of indices on the left excluding the Kth index ; Number of indices on the left excluding the Kth index ; Add mid to final sum ; Distribution on left side is possible ; Sum of distribution on the left side ; Sum of distribution on the left side with ( L - mid ) 1 s ; Distribution on right side is possible ; Sum of distribution on the right side ; Sum of distribution on the left side with ( R - mid ) 1 s ; Distribution is valid ; Return answer ; Input ; Function call
def calculateMax ( N , M , K ) : NEW_LINE INDENT ans = - 1 NEW_LINE low = 0 NEW_LINE high = M NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) / 2 NEW_LINE val = 0 NEW_LINE L = K - 1 NEW_LINE R = N - K NEW_LINE val += mid NEW_LINE if ( mid >= L ) : NEW_LINE INDENT val += ( L ) * ( 2 * mid - L - 1 ) / 2 NEW_LINE DEDENT else : NEW_LINE INDENT val += mid * ( mid - 1 ) / 2 + ( L - mid ) NEW_LINE DEDENT if ( mid >= R ) : NEW_LINE INDENT val += ( R ) * ( 2 * mid - R - 1 ) / 2 NEW_LINE DEDENT else : NEW_LINE INDENT val += mid * ( mid - 1 ) / 2 + ( R - mid ) NEW_LINE DEDENT if ( val <= M ) : NEW_LINE INDENT ans = max ( ans , mid ) NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return int ( ans ) NEW_LINE DEDENT N = 7 NEW_LINE M = 100 NEW_LINE K = 6 NEW_LINE print ( calculateMax ( N , M , K ) ) ; NEW_LINE
Minimum time required to color all edges of a Tree | Stores the required answer ; Stores the graph ; Function to add edges ; Function to calculate the minimum time required to color all the edges of a tree ; Starting from time = 0 , for all the child edges ; If the edge is not visited yet . ; Time of coloring of the current edge ; If the parent edge has been colored at the same time ; Update the maximum time ; Recursively call the function to its child node ; Driver Code ; Function call ; Finally , print the answer
ans = 0 NEW_LINE edges = [ [ ] for i in range ( 100000 ) ] NEW_LINE def Add_edge ( u , v ) : NEW_LINE INDENT global edges NEW_LINE edges [ u ] . append ( v ) NEW_LINE edges [ v ] . append ( u ) NEW_LINE DEDENT def minTimeToColor ( node , parent , arrival_time ) : NEW_LINE INDENT global ans NEW_LINE current_time = 0 NEW_LINE for x in edges [ node ] : NEW_LINE INDENT if ( x != parent ) : NEW_LINE INDENT current_time += 1 NEW_LINE if ( current_time == arrival_time ) : NEW_LINE INDENT current_time += 1 NEW_LINE DEDENT ans = max ( ans , current_time ) NEW_LINE minTimeToColor ( x , node , current_time ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 4 ] ] NEW_LINE for i in A : NEW_LINE INDENT Add_edge ( i [ 0 ] , i [ 1 ] ) NEW_LINE DEDENT minTimeToColor ( 1 , - 1 , 0 ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Largest number having both positive and negative values present in the array | Function to find the largest number k such that both k and - k are present in the array ; Stores the resultant value of K ; Sort the array arr [ ] ; Initialize two variables to use two pointers technique ; Iterate until the value of l is less than r ; Find the value of the sum ; If the sum is 0 , then the resultant element is found ; If the sum is negative ; Otherwise , decrement r ; Driver Code
def largestNum ( arr ) : NEW_LINE INDENT res = 0 NEW_LINE arr = sorted ( arr ) NEW_LINE l = 0 NEW_LINE r = len ( arr ) - 1 NEW_LINE while ( l < r ) : NEW_LINE INDENT sum = arr [ l ] + arr [ r ] NEW_LINE if ( sum == 0 ) : NEW_LINE INDENT res = max ( res , max ( arr [ l ] , arr [ r ] ) ) NEW_LINE return res NEW_LINE DEDENT elif ( sum < 0 ) : NEW_LINE INDENT l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT r -= 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , - 2 , 5 , - 3 ] NEW_LINE print ( largestNum ( arr ) ) NEW_LINE DEDENT
Find elements larger than half of the elements in an array | Set 2 | Function to find the element that are larger than half of elements of the array ; Find the value of mid ; Stores the maximum element ; Stores the frequency of each array element ; Traverse the array in the reverse order ; Decrement the value of count [ i ] and mid ; Prthe current element ; Check if the value of mid is equal to 0 ; Driver Code
def findLarger ( arr , n ) : NEW_LINE INDENT mid = ( n + 1 ) // 2 NEW_LINE mx = max ( arr ) NEW_LINE count = [ 0 ] * ( mx + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in range ( mx , - 1 , - 1 ) : NEW_LINE INDENT while ( count [ i ] > 0 ) : NEW_LINE INDENT count [ i ] -= 1 NEW_LINE mid -= 1 NEW_LINE print ( i , end = " ▁ " ) NEW_LINE if ( mid == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( mid == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 4 , 2 , 8 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE findLarger ( arr , N ) NEW_LINE DEDENT