codeparrot/xlcost-text-to-code huggingface dataset:
Minimum operations of the given type required to make a complete graph | Python 3 implementation of the approach ; Function to return the minimum number of steps required ; Driver Code
from math import log2 , ceil NEW_LINE def minOperations ( N ) : NEW_LINE INDENT x = log2 ( N ) NEW_LINE ans = ceil ( x ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE print ( minOperations ( N ) ) NEW_LINE DEDENT
Greatest divisor which divides all natural number in range [ L , R ] | Function to return the greatest divisor that divides all the natural numbers in the range [ l , r ] ; Driver Code
def find_greatest_divisor ( l , r ) : NEW_LINE INDENT if ( l == r ) : NEW_LINE INDENT return l ; NEW_LINE DEDENT return 1 ; NEW_LINE DEDENT l = 2 ; NEW_LINE r = 12 ; NEW_LINE print ( find_greatest_divisor ( l , r ) ) ; NEW_LINE
Probability of getting two consecutive heads after choosing a random coin among two different types of coins | Function to return the probability of getting two consecutive heads ; Formula derived from Bayes 's theorem ; Driver code ; given the probability of getting a head for both the coins
def getProbability ( p , q ) : NEW_LINE INDENT p /= 100 NEW_LINE q /= 100 NEW_LINE probability = ( p * p + q * q ) / ( p + q ) NEW_LINE return probability NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p = 80 NEW_LINE q = 40 NEW_LINE print ( getProbability ( p , q ) ) NEW_LINE DEDENT
Check whether bitwise OR of N numbers is Even or Odd | Function to check if bitwise OR of n numbers is even or odd ; if at least one odd number is found , then the bitwise OR of all numbers will be odd ; Bitwise OR is an odd number ; Driver code
def check ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] & 1 : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 9 , 12 , 13 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE if check ( arr , n ) : NEW_LINE INDENT print ( " Odd ▁ Bit - wise ▁ OR " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Even ▁ Bit - wise ▁ OR " ) NEW_LINE DEDENT DEDENT
Time taken by Loop unrolling vs Normal loop | Python program to compare normal loops and loops with unrolling technique ; n is 8 lakhs ; t to note start time ; to store the sum ; Normal loop ; to mark end time ; to mark start time of unrolling ; Unrolling technique ( assuming that n is a multiple of 8 ) . ; to mark the end of loop
from timeit import default_timer as clock NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 800000 ; NEW_LINE t = clock ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT t = clock ( ) - t NEW_LINE print ( " sum ▁ is : ▁ " + str ( sum ) ) NEW_LINE print ( " time ▁ taken ▁ by ▁ normal ▁ " + " loops : " + str ( t ) ) NEW_LINE t = clock ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( 1 , n + 1 , 8 ) : NEW_LINE INDENT sum += i NEW_LINE sum += ( i + 1 ) NEW_LINE sum += ( i + 2 ) NEW_LINE sum += ( i + 3 ) NEW_LINE sum += ( i + 4 ) NEW_LINE sum += ( i + 5 ) NEW_LINE sum += ( i + 6 ) NEW_LINE sum += ( i + 7 ) NEW_LINE DEDENT t = clock ( ) - t NEW_LINE print ( " Sum ▁ is : ▁ " + str ( sum ) ) NEW_LINE print ( " Time ▁ taken ▁ by ▁ unrolling : ▁ " + str ( t ) ) NEW_LINE DEDENT
Iterated Logarithm log * ( n ) | Recursive Python3 program to find value of Iterated Logarithm ; Driver code
import math NEW_LINE def _log ( x , base ) : NEW_LINE INDENT return ( int ) ( math . log ( x ) / math . log ( base ) ) NEW_LINE DEDENT def recursiveLogStar ( n , b ) : NEW_LINE INDENT if ( n > 1.0 ) : NEW_LINE INDENT return 1.0 + recursiveLogStar ( _log ( n , b ) , b ) NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 100 NEW_LINE base = 5 NEW_LINE print ( " Log * ( " , n , " ) ▁ = ▁ " , recursiveLogStar ( n , base ) ) NEW_LINE DEDENT
Iterated Logarithm log * ( n ) | Iterative Python function to find value of Iterated Logarithm
def iterativeLogStar ( n , b ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n >= 1 ) : NEW_LINE INDENT n = _log ( n , b ) NEW_LINE count = count + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT
Tail Recursion | An example of tail recursive function ; The last executed statement is recursive call
def prints ( n ) : NEW_LINE INDENT if ( n < 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT print ( str ( n ) , end = ' ▁ ' ) NEW_LINE prints ( n - 1 ) NEW_LINE DEDENT
Find the maximum among the count of positive or negative integers in the array | Function to find the maximum of the count of positive or negative elements ; Initialize the pointers ; Find the value of mid ; If element is negative then ignore the left half ; If element is positive then ignore the right half ; Return maximum among the count of positive & negative element ; Driver Code
def findMaximum ( arr , size ) : NEW_LINE INDENT i = 0 NEW_LINE j = size - 1 NEW_LINE while ( i <= j ) : NEW_LINE INDENT mid = i + ( j - i ) // 2 NEW_LINE if ( arr [ mid ] < 0 ) : NEW_LINE INDENT i = mid + 1 NEW_LINE DEDENT elif ( arr [ mid ] > 0 ) : NEW_LINE INDENT j = mid - 1 NEW_LINE DEDENT DEDENT return max ( i , size - i ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 9 , - 7 , - 4 , 1 , 5 , 8 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMaximum ( arr , N ) ) NEW_LINE DEDENT
Perform given queries on Queue according to the given rules | Function to perform all the queries operations on the given queue ; Stores the count query of type 1 ; Event E1 : increase countE1 ; Event E2 : add the X in set ; Event E3 : Find position of X ; Initial position is ( position - countE1 ) ; If X is already removed or popped out ; Finding the position of X in queue ; Traverse set to decrease position of X for all the number removed in front ; Print the position of X ; Driver Code
def solve ( n , m , queries ) : NEW_LINE INDENT countE1 = 0 NEW_LINE removed = set ( ) NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT if ( queries [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT countE1 += 1 NEW_LINE DEDENT elif ( queries [ i ] [ 0 ] == 2 ) : NEW_LINE INDENT removed . add ( queries [ i ] [ 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT position = queries [ i ] [ 1 ] - countE1 NEW_LINE if ( queries [ i ] [ 1 ] in removed or position <= 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT for it in removed : NEW_LINE INDENT if ( it > queries [ i ] [ 1 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT position -= 1 NEW_LINE DEDENT print ( position ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE Q = 3 NEW_LINE queries = [ [ 0 for _ in range ( 2 ) ] for _ in range ( Q ) ] NEW_LINE queries [ 0 ] [ 0 ] = 1 NEW_LINE queries [ 0 ] [ 1 ] = 0 NEW_LINE queries [ 1 ] [ 0 ] = 3 NEW_LINE queries [ 1 ] [ 1 ] = 3 NEW_LINE queries [ 2 ] [ 0 ] = 2 NEW_LINE queries [ 2 ] [ 1 ] = 2 NEW_LINE solve ( N , Q , queries ) NEW_LINE DEDENT
Maximum index a pointer can reach in N steps by avoiding a given index B | Python 3 program for the above approach ; Function to find the maximum index reachable ; Store the answer ; Store the maximum index possible i . e , N * ( N - 1 ) ; Add bthe previous elements ; Run a loop ; Binary Search ; Driver Code
from bisect import bisect_left NEW_LINE def maxIndex ( N , B ) : NEW_LINE INDENT maximumIndexReached = 0 NEW_LINE Ans = [ ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT maximumIndexReached += i NEW_LINE Ans . append ( i ) NEW_LINE DEDENT Ans . reverse ( ) NEW_LINE for i in range ( len ( Ans ) ) : NEW_LINE INDENT Ans [ i ] += Ans [ i - 1 ] NEW_LINE DEDENT while ( maximumIndexReached ) : NEW_LINE INDENT it = bisect_left ( Ans , maximumIndexReached - B ) NEW_LINE if ( it not in Ans ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( it == maximumIndexReached - B ) : NEW_LINE INDENT maximumIndexReached -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return maximumIndexReached NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE B = 2 NEW_LINE print ( maxIndex ( N , B ) ) NEW_LINE DEDENT
Maximize Kth largest element after splitting the given Array at most C times | Function to find the K - th maximum element after upto C operations ; Check for the base case ; Stores the count iterations of BS ; Create the left and right bounds of binary search ; Perform binary search ; Find the value of mid ; Traverse the array ; Update the ranges ; Return the maximum value obtained ; Driver Code
def maxKth ( arr , N , C , K ) : NEW_LINE INDENT if ( N + C < K ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT iter = 300 NEW_LINE l = 0 NEW_LINE r = 1000000000.0 NEW_LINE while ( iter ) : NEW_LINE INDENT iter = iter - 1 NEW_LINE mid = ( l + r ) * 0.5 NEW_LINE a = 0 NEW_LINE b = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT a += arr [ i ] // mid NEW_LINE if ( arr [ i ] >= mid ) : NEW_LINE INDENT b += 1 NEW_LINE DEDENT DEDENT if ( a >= K and b + C >= K ) : NEW_LINE INDENT l = mid NEW_LINE DEDENT else : NEW_LINE INDENT r = mid NEW_LINE DEDENT DEDENT return int ( l ) NEW_LINE DEDENT arr = [ 5 , 8 ] NEW_LINE K = 1 NEW_LINE C = 1 NEW_LINE N = len ( arr ) NEW_LINE print ( maxKth ( arr , N , C , K ) ) NEW_LINE
Maximize the minimum difference between any element pair by selecting K elements from given Array | To check if selection of K elements is possible such that difference between them is greater than dif ; Selecting first element in the sorted array ; prev is the previously selected element initially at index 0 as first element is already selected ; Check if selection of K - 1 elements from array with a minimum difference of dif is possible ; If the current element is atleast dif difference apart from the previously selected element then select the current element and increase the count ; If selection of K elements with a min difference of dif is possible then return true ; Prev will become current element for the next iteration ; If selection of K elements with minimum difference of dif is not possible then return false ; Minimum largest difference possible is 1 ; Check if selection of K elements is possible with a minimum difference of dif ; If dif is greater than previous ans we update ans ; Continue to search for better answer . Try finding greater dif ; K elements cannot be selected ; Driver code ; arr should be in a sorted order
def isPossibleToSelect ( arr , N , dif , K ) : NEW_LINE INDENT count = 1 NEW_LINE prev = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ i ] >= ( prev + dif ) ) : NEW_LINE INDENT count += 1 NEW_LINE if ( count == K ) : NEW_LINE INDENT return True NEW_LINE DEDENT prev = arr [ i ] NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def binarySearch ( arr , left , right , K , N ) : NEW_LINE INDENT ans = 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT dif = left + ( right - left ) // 2 NEW_LINE if ( isPossibleToSelect ( arr , N , dif , K ) ) : NEW_LINE INDENT ans = max ( ans , dif ) NEW_LINE left = dif + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = dif - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 NEW_LINE K = 4 NEW_LINE arr = [ 1 , 4 , 9 , 0 , 2 , 13 , 3 ] NEW_LINE arr . sort ( ) NEW_LINE print ( binarySearch ( arr , 0 , arr [ N - 1 ] , K , N ) ) NEW_LINE DEDENT
Find maximum height to cut all chocolates horizontally such that at least K amount remains | Function to find the sum of remaining chocolate after making the horizontal cut at height mid ; Stores the sum of chocolates ; Traverse the array arr [ ] ; If the height is at least mid ; Return the possible sum ; Function to find the maximum horizontal cut made to all the chocolates such that the sum of the remaining element is at least K ; Ranges of Binary Search ; Perform the Binary Search ; Find the sum of removed after making cut at height mid ; If the chocolate removed is same as the chocolate needed then return the height ; If the chocolate removed is less than chocolate needed then shift to the left range ; Otherwise , shift to the right range ; Return the possible cut ; Driver Code
def cal ( arr , mid ) : NEW_LINE INDENT chocolate = 0 NEW_LINE for i in arr : NEW_LINE INDENT if i >= mid : NEW_LINE INDENT chocolate += i - mid NEW_LINE DEDENT DEDENT return chocolate NEW_LINE DEDENT def maximumCut ( arr , K ) : NEW_LINE INDENT low = 0 NEW_LINE high = max ( arr ) NEW_LINE while low <= high : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE chocolate = cal ( arr , mid ) NEW_LINE if chocolate == K : NEW_LINE INDENT return mid NEW_LINE DEDENT elif chocolate < K : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE if mid > high : NEW_LINE INDENT high = mid NEW_LINE DEDENT DEDENT DEDENT return high NEW_LINE DEDENT N , K = 4 , 7 NEW_LINE arr = [ 15 , 20 , 8 , 17 ] NEW_LINE print ( maximumCut ( arr , K ) ) NEW_LINE
Koko Eating Bananas | Python implementation for the above approach ; to get the ceil value ; in case of odd number ; in case of even number ; check if time is less than or equals to given hour ; as minimum speed of eating must be 1 ; Maximum speed of eating is the maximum bananas in given piles ; Check if the mid ( hours ) is valid ; If valid continue to search lower speed ; If cant finish bananas in given hours , then increase the speed ; Driver code
def check ( bananas , mid_val , H ) : NEW_LINE INDENT time = 0 ; NEW_LINE for i in range ( len ( bananas ) ) : NEW_LINE INDENT if ( bananas [ i ] % mid_val != 0 ) : NEW_LINE time += bananas [ i ] // mid_val + 1 ; NEW_LINE else : NEW_LINE time += bananas [ i ] // mid_val NEW_LINE DEDENT if ( time <= H ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT def minEatingSpeed ( piles , H ) : NEW_LINE INDENT start = 1 ; NEW_LINE end = sorted ( piles . copy ( ) , reverse = True ) [ 0 ] NEW_LINE while ( start < end ) : NEW_LINE INDENT mid = start + ( end - start ) // 2 ; NEW_LINE if ( check ( piles , mid , H ) == True ) : NEW_LINE end = mid ; NEW_LINE else : NEW_LINE start = mid + 1 ; NEW_LINE DEDENT return end ; NEW_LINE DEDENT piles = [ 30 , 11 , 23 , 4 , 20 ] ; NEW_LINE H = 6 ; NEW_LINE print ( minEatingSpeed ( piles , H ) ) ; NEW_LINE
Minimize the sum of pair which upon removing divides the Array into 3 subarrays | Python 3 implementation of the above approach ; Function to find minimum possible sum of pair which breaks the array into 3 non - empty subarrays ; prefixMin [ i ] contains minimum element till i ; Driver Code ; Given array
import sys NEW_LINE def minSumPair ( arr , N ) : NEW_LINE INDENT if ( N < 5 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT prefixMin = [ 0 for i in range ( N ) ] NEW_LINE prefixMin [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , N - 1 , 1 ) : NEW_LINE INDENT prefixMin [ i ] = min ( arr [ i ] , prefixMin [ i - 1 ] ) NEW_LINE DEDENT ans = sys . maxsize NEW_LINE for i in range ( 3 , N - 1 , 1 ) : NEW_LINE INDENT ans = min ( ans , arr [ i ] + prefixMin [ i - 2 ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 2 , 4 , 6 , 3 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minSumPair ( arr , N ) ) NEW_LINE DEDENT
Minimum time remaining for safety alarm to start | Function to check if the value of mid as the minimum number of hours satisfies the condition ; Stores the sum of speed ; Iterate over the range [ 0 , N ] ; Find the value of speed ; If the bike is considered to be fast add it in sum ; Return the resultant sum ; Function to find the minimum number of time required ; Stores the range of Binary Search ; Stores the minimum number of time required ; Find the value of mid ; If the mid is the resultant speed required ; Update the ans and high ; Otherwise ; Return the minimum number of hours ; Driver Code
def check ( H , A , mid , N , M , L ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT speed = mid * A [ i ] + H [ i ] NEW_LINE if ( speed >= L ) : NEW_LINE INDENT sum += speed NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT def buzzTime ( N , M , L , H , A ) : NEW_LINE INDENT low = 0 NEW_LINE high = 1e10 NEW_LINE ans = 0 NEW_LINE while ( high >= low ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( check ( H , A , mid , N , M , L ) >= M ) : NEW_LINE INDENT ans = mid NEW_LINE high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return int ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = 400 NEW_LINE L = 120 NEW_LINE H = [ 20 , 50 , 20 ] NEW_LINE A = [ 20 , 70 , 90 ] NEW_LINE N = len ( A ) NEW_LINE print ( buzzTime ( N , M , L , H , A ) ) NEW_LINE DEDENT
Count of different numbers divisible by 3 that can be obtained by changing at most one digit | Function to count the number of possible numbers divisible by 3 ; Calculate the sum ; Store the answer ; Iterate over the range ; Decreasing the sum ; Iterate over the range ; Checking if the new sum is divisible by 3 or not ; If yes increment the value of the count ; Given number
def findCount ( number ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( len ( number ) ) : NEW_LINE INDENT sum += int ( number [ i ] ) - 48 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( len ( number ) ) : NEW_LINE INDENT remaining_sum = sum - ( int ( number [ i ] ) - 48 ) NEW_LINE for j in range ( 10 ) : NEW_LINE INDENT if ( ( remaining_sum + j ) % 3 == 0 and j != int ( number [ i ] ) - 48 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( count ) NEW_LINE DEDENT number = "235" NEW_LINE findCount ( number ) NEW_LINE
Maximum number of teams of size K possible with each player from different country | Function to find if T number of teams can be formed or not ; Store the sum of array elements ; Traverse the array teams [ ] ; Required Condition ; Function to find the maximum number of teams possible ; Lower and Upper part of the range ; Perform the Binary Search ; Find the value of mid ; Perform the Binary Search ; Otherwise , update the search range ; Otherwise , update the search range ; Driver Code
def is_possible ( teams , T , k ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( len ( teams ) ) : NEW_LINE INDENT sum += min ( T , teams [ i ] ) NEW_LINE DEDENT return ( sum >= ( T * k ) ) NEW_LINE DEDENT def countOfTeams ( teams_list , N , K ) : NEW_LINE INDENT lb = 0 NEW_LINE ub = 1000000000 NEW_LINE while ( lb <= ub ) : NEW_LINE INDENT mid = lb + ( ub - lb ) // 2 NEW_LINE if ( is_possible ( teams_list , mid , K ) ) : NEW_LINE INDENT if ( is_possible ( teams_list , mid + 1 , K ) == False ) : NEW_LINE INDENT return mid NEW_LINE DEDENT else : NEW_LINE INDENT lb = mid + 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT ub = mid - 1 NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE print ( countOfTeams ( arr , N , K ) ) NEW_LINE DEDENT
Minimum number of decrements by 1 required to reduce all elements of a circular array to 0 | Function to find minimum operation require to make all elements 0 ; Stores the maximum element and its position in the array ; Traverse the array ; Update the maximum element and its index ; Print the minimum number of operations required ; Driver Code
def minimumOperations ( arr , N ) : NEW_LINE INDENT mx = 0 NEW_LINE pos = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] >= mx ) : NEW_LINE INDENT mx = arr [ i ] NEW_LINE pos = i NEW_LINE DEDENT DEDENT print ( ( mx - 1 ) * N + pos + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 0 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE minimumOperations ( arr , N ) NEW_LINE DEDENT
Maximize the smallest array element by incrementing all elements in a K | Python 3 program for above approach ; Function to check if the smallest value of v is achievable or not ; Create array to store previous moves ; Remove previous moves ; Add balance to ans ; Update contiguous subarray of length k ; Number of moves should not exceed m ; Function to find the maximum value of the smallest array element that can be obtained ; Perform Binary search ; Driver Code ; Given Input ; Function Call
n = 0 NEW_LINE m = 0 NEW_LINE k = 0 NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE i = 0 NEW_LINE from math import pow NEW_LINE def check ( v , a ) : NEW_LINE INDENT tec = 0 NEW_LINE ans = 0 NEW_LINE b = [ 0 for i in range ( n + k + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT tec -= b [ i ] NEW_LINE if ( a [ i ] + tec < v ) : NEW_LINE INDENT mov = v - a [ i ] - tec NEW_LINE ans = ans + mov NEW_LINE tec += mov NEW_LINE b [ i + k ] = mov NEW_LINE DEDENT DEDENT return ( ans <= m ) NEW_LINE DEDENT def FindLargest ( a ) : NEW_LINE INDENT l = 1 NEW_LINE r = pow ( 10 , 10 ) NEW_LINE while ( r - l > 0 ) : NEW_LINE INDENT tm = ( l + r + 1 ) // 2 NEW_LINE if ( check ( tm , a ) ) : NEW_LINE INDENT l = tm NEW_LINE DEDENT else : NEW_LINE INDENT r = tm - 1 NEW_LINE DEDENT DEDENT return l NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 2 , 2 , 2 , 2 , 1 , 1 ] NEW_LINE m = 2 NEW_LINE k = 3 NEW_LINE n = len ( a ) NEW_LINE print ( int ( FindLargest ( a ) ) ) NEW_LINE DEDENT
Altitude of largest Triangle that can be inscribed in a Rectangle | Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Variables to perform binary search ; Stores the maximum altitude possible ; Iterate until low is less than high ; Stores the mid value ; If mide is less than or equal to the B / 2 ; Update res ; Update low ; Update high ; Print the result ; Driver Code ; Given Input ; Function call
def largestAltitude ( L , B ) : NEW_LINE INDENT if ( L > B ) : NEW_LINE INDENT temp = B NEW_LINE B = L NEW_LINE L = temp NEW_LINE DEDENT low = 0 NEW_LINE high = L NEW_LINE res = 0 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( mid <= ( B / 2 ) ) : NEW_LINE INDENT res = mid NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 3 NEW_LINE B = 4 NEW_LINE print ( largestAltitude ( L , B ) ) NEW_LINE DEDENT
Find the frequency of each element in a sorted array | Function to print the frequency of each element of the sorted array ; Stores the frequency of an element ; Traverse the array arr [ ] ; If the current element is equal to the previous element ; Increment the freq by 1 ; Otherwise , ; Update freq ; Print the frequency of the last element ; Driver Code ; Given Input ; Function Call
def printFreq ( arr , N ) : NEW_LINE INDENT freq = 1 NEW_LINE for i in range ( 1 , N , 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT freq += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Frequency ▁ of " , arr [ i - 1 ] , " is : " , freq ) NEW_LINE freq = 1 NEW_LINE DEDENT DEDENT print ( " Frequency ▁ of " , arr [ N - 1 ] , " is : " , freq ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE printFreq ( arr , N ) NEW_LINE DEDENT