codeparrot/xlcost-text-to-code huggingface dataset:
Find instances at end of time frame after auto scaling | Python program for the above approach ; Function to find the number of instances after completion ; Traverse the array , arr [ ] ; If current element is less than 25 ; Divide instances by 2 ; If the current element is greater than 60 ; Double the instances ; Print the instances at the end of the traversal ; Driver Code ; Function Call
from math import ceil NEW_LINE def finalInstances ( instances , arr ) : NEW_LINE INDENT i = 0 NEW_LINE while i < len ( arr ) : NEW_LINE INDENT if arr [ i ] < 25 and instances > 1 : NEW_LINE INDENT instances = ceil ( instances / 2 ) NEW_LINE i += 10 NEW_LINE DEDENT elif arr [ i ] > 60 and instances <= 10 ** 8 : NEW_LINE INDENT instances *= 2 NEW_LINE i += 10 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT print ( instances ) NEW_LINE DEDENT instances = 2 NEW_LINE arr = [ 25 , 23 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 76 , 80 ] NEW_LINE finalInstances ( instances , arr ) NEW_LINE
Modify array by making all array elements equal to 0 by subtracting K ^ i from an array element in every i | Function to check whether all array elements can be made zero or not ; Stores if a power of K has already been subtracted or not ; Stores all the Kth power ; Iterate until X is less than INT_MAX ; Traverse the array arr [ ] ; Iterate over the range [ 0 , M ] ; If MP [ V [ j ] ] is 0 and V [ j ] is less than or equal to arr [ i ] ; If arr [ i ] is not 0 ; If i is less than N - 1 ; Otherwise , ; Driver code
def isMakeZero ( arr , N , K ) : NEW_LINE INDENT MP = { } NEW_LINE V = [ ] NEW_LINE X = 1 NEW_LINE while ( X > 0 and X < 10 ** 20 ) : NEW_LINE INDENT V . append ( X ) NEW_LINE X *= K NEW_LINE DEDENT for i in range ( 0 , N , 1 ) : NEW_LINE INDENT for j in range ( len ( V ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( V [ j ] not in MP and V [ j ] <= arr [ i ] ) : NEW_LINE INDENT arr [ i ] -= V [ j ] NEW_LINE MP [ V [ j ] ] = 1 NEW_LINE DEDENT DEDENT if ( arr [ i ] != 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( i < N - 1 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT else : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT arr = [ 8 , 0 , 3 , 4 , 80 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE print ( isMakeZero ( arr , N , K ) ) NEW_LINE
Check whether the product of every subsequence is a perfect square or not | Python 3 program for the above approach ; Function to check if the product of every subsequence of the array is a perfect square or not ; Traverse the given array ; If arr [ i ] is a perfect square or not ; Return " Yes " ; Driver Code
from math import sqrt NEW_LINE def perfectSquare ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT p = sqrt ( arr [ i ] ) NEW_LINE if ( p * p != arr [ i ] ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT return " Yes " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 100 ] NEW_LINE N = len ( arr ) NEW_LINE print ( perfectSquare ( arr , N ) ) NEW_LINE DEDENT
Count of subarrays with average K | Function to count subarray having average exactly equal to K ; To Store the final answer ; Calculate all subarrays ; Calculate required average ; Check if average is equal to k ; Required average found ; Increment res ; Driver code ; Given Input ; Function Call
def countKAverageSubarrays ( arr , n , k ) : NEW_LINE INDENT res = 0 NEW_LINE for L in range ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for R in range ( L , n , 1 ) : NEW_LINE INDENT sum += arr [ R ] NEW_LINE len1 = ( R - L + 1 ) NEW_LINE if ( sum % len1 == 0 ) : NEW_LINE INDENT avg = sum // len1 NEW_LINE if ( avg == k ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 6 NEW_LINE arr = [ 12 , 5 , 3 , 10 , 4 , 8 , 10 , 12 , - 6 , - 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countKAverageSubarrays ( arr , N , K ) ) NEW_LINE DEDENT
Maximize minimum array element possible by exactly K decrements | Function to find the maximized minimum element of the array after performing given operation exactly K times ; Stores the minimum element ; Traverse the given array ; Update the minimum element ; Stores the required operations to make all elements equal to the minimum element ; Update required operations ; If reqOperations < K ; Decrement the value of K by reqOperations ; Update minElement ; Return minimum element ; Driver Code
def minimumElement ( arr , N , K ) : NEW_LINE INDENT minElement = arr [ 0 ] ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT minElement = min ( minElement , arr [ i ] ) ; NEW_LINE DEDENT reqOperations = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT reqOperations += arr [ i ] - minElement NEW_LINE DEDENT if ( reqOperations < K ) : NEW_LINE INDENT K -= reqOperations ; NEW_LINE minElement -= ( K + N - 1 ) // N ; NEW_LINE DEDENT return minElement ; NEW_LINE DEDENT arr = [ 10 , 10 , 10 , 10 ] ; NEW_LINE K = 7 ; NEW_LINE N = len ( arr ) NEW_LINE print ( minimumElement ( arr , N , K ) ) ; NEW_LINE
Split the fraction into sum of multiple fractions having numerator as 1 | Function to split the fraction into distinct unit fraction ; To store answer ; While numerator is positive ; Finding x = ceil ( d / n ) ; Add 1 / x to list of ans ; Update fraction ; Given Input ; Function Call ; Print Answer
def FractionSplit ( n , d ) : NEW_LINE INDENT UnitFactions = [ ] NEW_LINE while ( n > 0 ) : NEW_LINE INDENT x = ( d + n - 1 ) // n NEW_LINE s = "1 / " + str ( x ) NEW_LINE UnitFactions . append ( s ) ; NEW_LINE n = n * x - d ; NEW_LINE d = d * x NEW_LINE DEDENT return UnitFactions ; NEW_LINE DEDENT n = 13 ; NEW_LINE d = 18 ; NEW_LINE res = FractionSplit ( n , d ) ; NEW_LINE for s in res : NEW_LINE INDENT print ( s + " , ▁ " , end = " ▁ " ) ; NEW_LINE DEDENT
Find permutation of [ 1 , N ] such that ( arr [ i ] != i + 1 ) and sum of absolute difference between arr [ i ] and ( i + 1 ) is minimum | Function to generate the permutation of the first N natural numbers having sum of absolute difference between element and indices as minimum ; Initialize array arr [ ] from 1 to N ; Swap alternate positions ; Check N is greater than 1 and N is odd ; Swapping last two positions ; Print the permutation ; Driver Code
def findPermutation ( N ) : NEW_LINE INDENT arr = [ i + 1 for i in range ( N ) ] NEW_LINE for i in range ( 1 , N , 2 ) : NEW_LINE INDENT arr [ i ] , arr [ i - 1 ] = arr [ i - 1 ] , arr [ i ] NEW_LINE DEDENT if N % 2 and N > 1 : NEW_LINE INDENT arr [ - 1 ] , arr [ - 2 ] = arr [ - 2 ] , arr [ - 1 ] NEW_LINE DEDENT for i in arr : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 NEW_LINE findPermutation ( N ) NEW_LINE DEDENT
Find Array obtained after adding terms of AP for Q queries | Function to find array after performing the given query to the array elements ; Traverse the given query ; Traverse the given array ; Update the value of A [ i ] ; Update the value of curr ; Print the array elements ; Driver Code ; Function Call
def addAP ( A , Q , operations ) : NEW_LINE INDENT for L , R , a , d in operations : NEW_LINE INDENT curr = a NEW_LINE for i in range ( L - 1 , R ) : NEW_LINE INDENT A [ i ] += curr NEW_LINE curr += d NEW_LINE DEDENT DEDENT for i in A : NEW_LINE INDENT print ( i , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT A = [ 5 , 4 , 2 , 8 ] NEW_LINE Q = 2 NEW_LINE Query = [ ( 1 , 2 , 1 , 3 ) , ( 1 , 4 , 4 , 1 ) ] NEW_LINE addAP ( A , Q , Query ) NEW_LINE
Maximize the number N by inserting given digit at any position | Function to find the maximum value of N after inserting the digit K ; Convert it into N to string ; Stores the maximum value of N after inserting K ; Iterate till all digits that are not less than K ; Add the current digit to the string result ; Add digit ' K ' to result ; Iterate through all remaining characters ; Add current digit to result ; Print the maximum number formed ; Driver Code
def maximizeNumber ( N , K ) : NEW_LINE INDENT s = str ( N ) NEW_LINE L = len ( s ) NEW_LINE result = " " NEW_LINE i = 0 NEW_LINE while ( ( i < L ) and ( K <= ( ord ( s [ i ] ) - ord ( '0' ) ) ) ) : NEW_LINE INDENT result += ( s [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT result += ( chr ( K + ord ( '0' ) ) ) NEW_LINE while ( i < L ) : NEW_LINE INDENT result += ( s [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT print ( result ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6673 NEW_LINE K = 6 NEW_LINE maximizeNumber ( N , K ) NEW_LINE DEDENT
Count of N | Function to find the value of X to the power Y ; Stores the value of X ^ Y ; If y is odd , multiply x with result ; Update the value of y and x ; Return the result ; Function to count number of arrays having element over the range [ 0 , 2 ^ K - 1 ] with Bitwise AND value 0 having maximum possible sum ; Print the value of N ^ K ; Driver Code
def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = res * x NEW_LINE DEDENT y = y >> 1 NEW_LINE x = x * x NEW_LINE DEDENT return res NEW_LINE DEDENT def countArrays ( N , K ) : NEW_LINE INDENT print ( power ( N , K ) ) NEW_LINE DEDENT N = 5 ; NEW_LINE K = 6 ; NEW_LINE countArrays ( N , K ) NEW_LINE
Minimum absolute value of ( K – arr [ i ] ) for all possible values of K over the range [ 0 , N – 1 ] | Function to find the minimum absolute value of ( K - arr [ i ] ) for each possible value of K over the range [ 0 , N - 1 ] | ; Traverse the given array arr [ ] ; Stores the mininimum distance to any array element arr [ i ] ; Check if there is no safe position smaller than i ; Check if the current position is between two safe positions ; Take the minimum of two distances ; Check if the current index is a safe position ; Check if there is no safe position greater than i ; Print the minimum distance ; Driver Code
def minimumDistance ( arr , N ) : NEW_LINE INDENT ind = 0 ; NEW_LINE prev = arr [ ind ] ; NEW_LINE s = len ( arr ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT distance = 10 ** 9 ; NEW_LINE if ( i < arr [ 0 ] ) : NEW_LINE INDENT distance = arr [ 0 ] - i ; NEW_LINE DEDENT elif ( i >= prev and ind + 1 < s and i <= arr [ ind + 1 ] ) : NEW_LINE INDENT distance = min ( i - prev , arr [ ind + 1 ] - i ) ; NEW_LINE if ( i == arr [ ind + 1 ] ) : NEW_LINE INDENT distance = 0 ; NEW_LINE prev = arr [ ind + 1 ] ; NEW_LINE ind += 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT distance = i - prev ; NEW_LINE DEDENT print ( distance , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT N = 5 ; NEW_LINE arr = [ 0 , 4 ] ; NEW_LINE minimumDistance ( arr , N ) ; NEW_LINE
Count of pairs in Array such that bitwise AND of XOR of pair and X is 0 | Function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & X is 0 ; Stores the resultant count of pairs ; Iterate over the range [ 0 , N ) ; Iterate over the range ; Check for the given condition ; Return the resultant count ; Driver Code
def countOfPairs ( arr , N , X ) : 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 ( ( ( arr [ i ] ^ arr [ j ] ) & X ) == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 5 , 4 , 6 , 7 ] NEW_LINE X = 6 NEW_LINE N = len ( arr ) NEW_LINE print ( countOfPairs ( arr , N , X ) ) NEW_LINE DEDENT
Count of pairs in Array such that bitwise AND of XOR of pair and X is 0 | Function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & X is 0 ; Stores the resultant count of pairs ; Initialize the dictionary M ; Populate the map ; Count number of pairs for every element in map using mathematical concept of combination ; As nC2 = n * ( n - 1 ) / 2 ; Return the resultant count ; Driver Code
def countOfPairs ( arr , N , X ) : NEW_LINE INDENT count = 0 NEW_LINE M = dict ( ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT k = arr [ i ] & X NEW_LINE if k in M : NEW_LINE INDENT M [ k ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT M [ k ] = 1 NEW_LINE DEDENT DEDENT for m in M . keys ( ) : NEW_LINE INDENT p = M [ m ] NEW_LINE count += p * ( p - 1 ) // 2 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 5 , 4 , 6 , 7 ] NEW_LINE X = 6 NEW_LINE N = len ( arr ) NEW_LINE print ( countOfPairs ( arr , N , X ) ) NEW_LINE DEDENT
Count of square submatrices with average at least K | Function to count submatrixes with average greater than or equals to K ; Stores count of submatrices ; Stores the prefix sum of matrix ; Iterate over the range [ 1 , N ] ; Iterate over the range [ 1 , M ] ; Update the prefix sum ; Iterate over the range [ 1 , N ] ; Iterate over the range [ 1 , M ] ; Iterate until l and r are greater than 0 ; Update count ; Stores sum of submatrix with bottom right corner as ( i , j ) and top left corner as ( l , r ) ; If sum1 is less than or equal to sum2 ; Increment cnt by 1 ; Return cnt as the answer ; Driver Code ; Given Input ; Function Call
def cntMatrices ( arr , N , M , K ) : NEW_LINE INDENT cnt = 0 NEW_LINE pre = [ [ 0 for i in range ( M + 1 ) ] for i in range ( N + 1 ) ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , M + 1 ) : NEW_LINE INDENT pre [ i ] [ j ] = ( arr [ i - 1 ] [ j - 1 ] + pre [ i - 1 ] [ j ] + pre [ i ] [ j - 1 ] - pre [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , M + 1 ) : NEW_LINE INDENT l , r = i , j NEW_LINE while l > 0 and r > 0 : NEW_LINE INDENT sum1 = ( K * ( i - l + 1 ) * ( i - r + 1 ) ) NEW_LINE sum2 = ( pre [ i ] [ j ] - pre [ l - 1 ] [ r ] - pre [ l ] [ r - 1 ] + pre [ l - 1 ] [ r - 1 ] ) NEW_LINE if ( sum1 <= sum2 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT l -= 1 NEW_LINE r -= 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 2 , 2 , 3 ] , [ 3 , 4 , 5 ] , [ 4 , 5 , 5 ] ] NEW_LINE K = 4 NEW_LINE N = len ( arr ) NEW_LINE M = len ( arr [ 0 ] ) NEW_LINE print ( cntMatrices ( arr , N , M , K ) ) NEW_LINE DEDENT
Concatenate the Array of elements into a single element | Python3 program for the above approach ; Function to find the integer value obtained by joining array elements together ; Stores the resulting integer value ; Traverse the array arr [ ] ; Stores the count of digits of arr [ i ] ; Update ans ; Increment ans by arr [ i ] ; Return the ans ; Driver Code ; Input ; Function call
import math NEW_LINE def ConcatenateArr ( arr , N ) : NEW_LINE INDENT ans = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT l = math . floor ( math . log10 ( arr [ i ] ) + 1 ) NEW_LINE ans = ans * math . pow ( 10 , l ) NEW_LINE ans += arr [ i ] NEW_LINE DEDENT return int ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 23 , 456 ] NEW_LINE N = len ( arr ) NEW_LINE print ( ConcatenateArr ( arr , N ) ) NEW_LINE DEDENT
Count of integers K in range [ 0 , N ] such that ( K XOR K + 1 ) equals ( K + 2 XOR K + 3 ) | Function to count all the integers less than N satisfying the given condition ; Store the count of even numbers less than N + 1 ; Return the count ; Driver Code ; Given Input ; Function Call
def countXor ( N ) : NEW_LINE INDENT cnt = N // 2 + 1 NEW_LINE return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE print ( countXor ( N ) ) NEW_LINE DEDENT
Maximize absolute displacement from origin by moving on X | Recursive function to find the maximum absolute displacement from origin by performing the given set of moves ; If i is equal to N ; If S [ i ] is equal to 'L ; If S [ i ] is equal to 'R ; If S [ i ] is equal to '? ; Function to find the maximum absolute displacement from the origin ; Return the maximum absolute displacement ; Input ; Function call
def DistRecursion ( S , i , dist ) : NEW_LINE INDENT if i == len ( S ) : NEW_LINE INDENT return abs ( dist ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if S [ i ] == ' L ' : NEW_LINE INDENT return DistRecursion ( S , i + 1 , dist - 1 ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if S [ i ] == ' R ' : NEW_LINE INDENT return DistRecursion ( S , i + 1 , dist + 1 ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT return max ( DistRecursion ( S , i + 1 , dist - 1 ) , DistRecursion ( S , i + 1 , dist + 1 ) ) NEW_LINE DEDENT def maxDistance ( S ) : NEW_LINE INDENT return DistRecursion ( S , 0 , 0 ) NEW_LINE DEDENT S = " ? RRR ? " NEW_LINE print ( maxDistance ( S ) ) NEW_LINE
Count of pairs in range [ P , Q ] with numbers as multiple of R and their product lie in range [ P * Q / 4 , P * Q ] | Function to find the number of pairs such that both the elements are in the range [ P , Q ] and the numbers should be multiple of R , and the product of numbers should lie in the range [ P * Q / 4 , P * Q ] ; Store multiple of r in range of [ P , Q ] ; Itearte in the range [ p , q ] ; Vector to store pair of answer ; Iterate through the vector v ; Iterate in the range [ i + 1 , v . size ( ) - 1 ] ; If pair follow this condition insert the pair in vector ans ; If no pair satisfy the conditions , pr - 1 ; Print the pairs which satisfy the given condition ; Driver Code ; Given Input ; Function Call
def findPairs ( p , q , r ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( p , q + 1 ) : NEW_LINE INDENT if ( i % r == 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT DEDENT ans = [ ] NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( v ) ) : NEW_LINE INDENT if ( v [ i ] * v [ j ] >= p * q // 4 and v [ i ] * v [ j ] <= p * q ) : NEW_LINE INDENT ans . append ( [ v [ i ] , v [ j ] ] ) NEW_LINE DEDENT DEDENT DEDENT if ( len ( ans ) == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] [ 0 ] , ans [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT p = 14 NEW_LINE q = 30 NEW_LINE r = 5 NEW_LINE findPairs ( p , q , r ) NEW_LINE DEDENT
Print rectangular pattern with given center | Function to print the matrix filled with rectangle pattern having center coordinates are c1 , c2 ; Iterate in the range [ 0 , n - 1 ] ; Iterate in the range [ 0 , n - 1 ] ; Given Input ; Function Call
def printRectPattern ( c1 , c2 , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( max ( abs ( c1 - i ) , abs ( c2 - j ) ) , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT c1 = 2 NEW_LINE c2 = 2 NEW_LINE n = 5 NEW_LINE printRectPattern ( c1 , c2 , n ) NEW_LINE
Count of numbers in range [ L , R ] with LSB as 0 in their Binary representation | Function to return the count of required numbers ; If rightmost bit is 0 ; Return the required count ; Driver code ; Call function countNumbers
def countNumbers ( l , r ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT if ( ( i & 1 ) == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT l = 10 NEW_LINE r = 20 NEW_LINE print ( countNumbers ( l , r ) ) NEW_LINE
Count of numbers in range [ L , R ] with LSB as 0 in their Binary representation | Function to return the count of required numbers ; Count of numbers in range which are divisible by 2 ; Driver code
def countNumbers ( l , r ) : NEW_LINE INDENT return ( ( r // 2 ) - ( l - 1 ) // 2 ) NEW_LINE DEDENT l = 10 NEW_LINE r = 20 NEW_LINE print ( countNumbers ( l , r ) ) NEW_LINE
Length of longest Fibonacci subarray | Python3 program for the above approach ; A utility function that returns true if x is perfect square ; Returns true if n is a Fibonacci Number , else false ; Here n is fibonacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both is a perfect square ; Function to find the length of the largest sub - array of an array every element of whose is a Fibonacci number ; Traverse the array arr ; Check if arr [ i ] is a Fibonacci number ; stores the maximum length of the Fibonacci number subarray ; Finally , return the maximum length ; Driver code ; Given Input ; Function Call
import math NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = int ( math . sqrt ( x ) ) NEW_LINE if s * s == x : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def isFibonacci ( n ) : NEW_LINE INDENT return ( isPerfectSquare ( 5 * n * n + 4 ) or isPerfectSquare ( 5 * n * n - 4 ) ) NEW_LINE DEDENT def contiguousFibonacciNumber ( arr , n ) : NEW_LINE INDENT current_length = 0 NEW_LINE max_length = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if isFibonacci ( arr [ i ] ) : NEW_LINE INDENT current_length += 1 NEW_LINE DEDENT else : NEW_LINE INDENT current_length = 0 NEW_LINE DEDENT max_length = max ( max_length , current_length ) NEW_LINE DEDENT return max_length NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 11 , 8 , 21 , 5 , 3 , 28 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( contiguousFibonacciNumber ( arr , n ) ) NEW_LINE DEDENT
Minimum number of flips required such that a Binary Matrix doesn 't contain any path from the top left to the bottom right consisting only of 0s | The four direction coordinates changes from the current cell ; Function that returns true if there exists any path from the top - left to the bottom - right cell of 0 s ; If the bottom - right cell is reached ; Update the cell to 1 ; Traverse in all four directions ; Find the new coordinates ; If the new cell is valid ; Recursively call DFS ; If path exists , then return true ; Return false , if there doesn 't  exists any such path ; Function to flip the minimum number of cells such that there doesn 't exists any such path from (0, 0) to (N - 1, M - 1) cell consisting of 0s ; Case 1 : If no such path exists already ; Case 2 : If there exists only one path ; Case 3 : If there exists two - path ; Driver Code
direction = [ [ - 1 , 0 ] , [ 0 , 1 ] , [ 0 , - 1 ] , [ 1 , 0 ] ] NEW_LINE def dfs ( i , j , N , M ) : NEW_LINE INDENT global matrix NEW_LINE if ( i == N - 1 and j == M - 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT matrix [ i ] [ j ] = 1 NEW_LINE for k in range ( 4 ) : NEW_LINE INDENT newX = i + direction [ k ] [ 0 ] NEW_LINE newY = j + direction [ k ] [ 1 ] NEW_LINE if ( newX >= 0 and newX < N and newY >= 0 and newY < M and matrix [ newX ] [ newY ] == 0 ) : NEW_LINE INDENT if ( dfs ( newX , newY , N , M ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT def solve ( ) : NEW_LINE INDENT global matrix NEW_LINE N = len ( matrix ) NEW_LINE M = len ( matrix [ 0 ] ) NEW_LINE if ( not dfs ( 0 , 0 , N , M ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( not dfs ( 0 , 0 , N , M ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 0 , 1 , 0 , 0 ] , [ 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ] NEW_LINE print ( solve ( ) ) NEW_LINE DEDENT