codeparrot/xlcost-text-to-code huggingface dataset:
Minimum value of K such that sum of cubes of first K natural number is greater than equal to N | Function to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Variable to store the sum of cubes ; Loop to find the number K ; If C is just greater then N , then break the loop ; Driver code
def naive_find_x ( N ) : NEW_LINE INDENT c = 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT c += i * i * i NEW_LINE if c >= N : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return i NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 100 NEW_LINE print ( naive_find_x ( N ) ) NEW_LINE DEDENT
Minimum value of K such that sum of cubes of first K natural number is greater than equal to N | Function to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Left bound ; Right bound ; Variable to store the answer ; Applying binary search ; Calculating mid value of the range ; If the sum of cubes of first mid natural numbers is greater than equal to N iterate the left half ; Sum of cubes of first mid natural numbers is less than N , then move to the right segment ; Driver code
def binary_searched_find_x ( k ) : NEW_LINE INDENT l = 0 NEW_LINE r = k NEW_LINE ans = 0 NEW_LINE while l <= r : NEW_LINE INDENT mid = l + ( r - l ) // 2 NEW_LINE if ( ( mid * ( mid + 1 ) ) // 2 ) ** 2 >= k : NEW_LINE INDENT ans = mid NEW_LINE r = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 100 NEW_LINE print ( binary_searched_find_x ( N ) ) NEW_LINE DEDENT
Length of the longest ZigZag subarray of the given array | Function to find the length of longest zigZag contiguous subarray ; ' _ max ' to store the length of longest zigZag subarray ; ' _ len ' to store the lengths of longest zigZag subarray at different instants of time ; Traverse the array from the beginning ; Check if ' _ max ' length is less than the length of the current zigzag subarray . If true , then update '_max ; Reset ' _ len ' to 1 as from this element , again the length of the new zigzag subarray is being calculated ; Comparing the length of the last zigzag subarray with '_max ; Return required maximum length ; Driver code
def lenOfLongZigZagArr ( a , n ) : NEW_LINE INDENT _max = 1 NEW_LINE _len = 1 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if i % 2 == 0 and a [ i ] < a [ i + 1 ] : NEW_LINE INDENT _len += 1 NEW_LINE DEDENT elif i % 2 == 1 and a [ i ] > a [ i + 1 ] : NEW_LINE INDENT _len += 1 NEW_LINE DEDENT else : NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if _max < _len : NEW_LINE INDENT _max = _len NEW_LINE DEDENT _len = 1 NEW_LINE DEDENT ' NEW_LINE INDENT if _max < _len : NEW_LINE INDENT _max = _len NEW_LINE DEDENT return _max NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( lenOfLongZigZagArr ( arr , n ) ) NEW_LINE
Check if a given number is a Perfect square using Binary Search | Function to check for perfect square number ; Find the mid value from start and last ; Check if we got the number which is square root of the perfect square number N ; If the square ( mid ) is greater than N it means only lower values then mid will be possibly the square root of N ; If the square ( mid ) is less than N it means only higher values then mid will be possibly the square root of N ; Driver code
def checkPerfectSquare ( N , start , last ) : NEW_LINE INDENT mid = int ( ( start + last ) / 2 ) NEW_LINE if ( start > last ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( mid * mid == N ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( mid * mid > N ) : NEW_LINE INDENT return checkPerfectSquare ( N , start , mid - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return checkPerfectSquare ( N , mid + 1 , last ) NEW_LINE DEDENT DEDENT N = 65 NEW_LINE print ( checkPerfectSquare ( N , 1 , N ) ) NEW_LINE
Maximum count number of valley elements in a subarray of size K | Function to find the valley elements in the array which contains in the subarrays of the size K ; Increment min_point if element at index i is smaller than element at index i + 1 and i - 1 ; final_point to maintain maximum of min points of subarray ; Iterate over array from kth element ; Leftmost element of subarray ; Rightmost element of subarray ; if new subarray have greater number of min points than previous subarray , then final_point is modified ; Max minimum points in subarray of size k ; Driver Code
def minpoint ( arr , n , k ) : NEW_LINE INDENT min_point = 0 NEW_LINE for i in range ( 1 , k - 1 ) : NEW_LINE INDENT if ( arr [ i ] < arr [ i - 1 ] and arr [ i ] < arr [ i + 1 ] ) : NEW_LINE INDENT min_point += 1 NEW_LINE DEDENT DEDENT final_point = min_point NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT if ( arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) + 1 ] and arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) - 1 ] ) : NEW_LINE INDENT min_point -= 1 NEW_LINE DEDENT if ( arr [ i - 1 ] < arr [ i ] and arr [ i - 1 ] < arr [ i - 2 ] ) : NEW_LINE INDENT min_point += 1 NEW_LINE DEDENT if ( min_point > final_point ) : NEW_LINE INDENT final_point = min_point NEW_LINE DEDENT DEDENT print ( final_point ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 1 , 4 , 2 , 3 , 4 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 NEW_LINE minpoint ( arr , n , k ) NEW_LINE DEDENT
For each lowercase English alphabet find the count of strings having these alphabets | Function to find the countStrings for each letter [ a - z ] ; Initialize result as zero ; Mark all letter as not visited ; Loop through each strings ; Increment the global counter for current character of string ; Instead of re - initialising boolean vector every time we just reset all visited letter to false ; Print count for each letter ; Driver code ; Given array of strings ; Call the countStrings function
def CountStrings ( s ) : NEW_LINE INDENT size = len ( s ) NEW_LINE count = [ 0 ] * 26 NEW_LINE visited = [ False ] * 26 NEW_LINE for i in range ( size ) : NEW_LINE INDENT for j in range ( len ( s [ i ] ) ) : NEW_LINE INDENT if visited [ ord ( s [ i ] [ j ] ) - ord ( ' a ' ) ] == False : NEW_LINE INDENT count [ ord ( s [ i ] [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT visited [ ord ( s [ i ] [ j ] ) - ord ( ' a ' ) ] = True NEW_LINE DEDENT for j in range ( len ( s [ i ] ) ) : NEW_LINE INDENT visited [ ord ( s [ i ] [ j ] ) - ord ( ' a ' ) ] = False NEW_LINE DEDENT DEDENT for i in range ( 26 ) : NEW_LINE INDENT print ( count [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = [ " i " , " will " , " practice " , " everyday " ] NEW_LINE CountStrings ( s ) NEW_LINE DEDENT
Find subarray of Length K with Maximum Peak | Function to find the subarray ; Make prefix array to store the prefix sum of peak count ; Count peak for previous index ; Check if this element is a peak ; Increment the count ; Check if number of peak in the sub array whose l = i is greater or not ; Print the result ; Driver code
def findSubArray ( a , n , k ) : NEW_LINE INDENT pref = [ 0 for i in range ( n ) ] NEW_LINE pref [ 0 ] = 0 NEW_LINE for i in range ( 1 , n - 1 , 1 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] NEW_LINE if ( a [ i ] > a [ i - 1 ] and a [ i ] > a [ i + 1 ] ) : NEW_LINE INDENT pref [ i ] += 1 NEW_LINE DEDENT DEDENT peak = 0 NEW_LINE left = 0 NEW_LINE for i in range ( 0 , n - k + 1 , 1 ) : NEW_LINE INDENT if ( pref [ i + k - 2 ] - pref [ i ] > peak ) : NEW_LINE INDENT peak = pref [ i + k - 2 ] - pref [ i ] NEW_LINE left = i NEW_LINE DEDENT DEDENT print ( " Left ▁ = " , left + 1 ) NEW_LINE print ( " Right ▁ = " , left + k ) NEW_LINE print ( " Peak ▁ = " , peak ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE findSubArray ( arr , n , k ) NEW_LINE DEDENT
Sort integers in array according to their distance from the element K | Function to get sorted array based on their distance from given integer K ; Vector to store respective elements with their distance from integer K ; Find the position of integer K ; Insert the elements with their distance from K in vector ; Element at distance 0 ; Elements at left side of K ; Elements at right side of K ; Print the vector content in sorted order ; Sort elements at same distance ; Print elements at distance i from K ; Driver code
def distanceSort ( arr , K , n ) : NEW_LINE INDENT vd = [ [ ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == K ) : NEW_LINE INDENT pos = i NEW_LINE break NEW_LINE DEDENT DEDENT i = pos - 1 NEW_LINE j = pos + 1 NEW_LINE vd [ 0 ] . append ( arr [ pos ] ) NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT vd [ pos - i ] . append ( arr [ i ] ) NEW_LINE i -= 1 NEW_LINE DEDENT while ( j < n ) : NEW_LINE INDENT vd [ j - pos ] . append ( arr [ j ] ) NEW_LINE j += 1 NEW_LINE DEDENT for i in range ( max ( pos , n - pos - 1 ) + 1 ) : NEW_LINE INDENT vd [ i ] . sort ( reverse = False ) NEW_LINE for element in vd [ i ] : NEW_LINE INDENT print ( element , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 14 , 1101 , 10 , 35 , 0 ] NEW_LINE K = 35 NEW_LINE n = len ( arr ) NEW_LINE distanceSort ( arr , K , n ) NEW_LINE DEDENT
Find Quotient and Remainder of two integer without using division operators | Function to the quotient and remainder ; Check if start is greater than the end ; Calculate mid ; Check if n is greater than divisor then increment the mid by 1 ; Check if n is less than 0 then decrement the mid by 1 ; Check if n equals to divisor ; Return the final answer ; Recursive calls ; Driver code
def find ( dividend , divisor , start , end ) : NEW_LINE INDENT if ( start > end ) : NEW_LINE INDENT return ( 0 , dividend ) ; NEW_LINE DEDENT mid = start + ( end - start ) // 2 ; NEW_LINE n = dividend - divisor * mid ; NEW_LINE if ( n > divisor ) : NEW_LINE INDENT start = mid + 1 ; NEW_LINE DEDENT elif ( n < 0 ) : NEW_LINE INDENT end = mid - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( n == divisor ) : NEW_LINE INDENT mid += 1 ; NEW_LINE n = 0 ; NEW_LINE DEDENT return ( mid , n ) ; NEW_LINE DEDENT return find ( dividend , divisor , start , end ) ; NEW_LINE DEDENT def divide ( dividend , divisor ) : NEW_LINE INDENT return find ( dividend , divisor , 1 , dividend ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT dividend = 10 ; divisor = 3 ; NEW_LINE ans = divide ( dividend , divisor ) ; NEW_LINE print ( ans [ 0 ] , " , ▁ " , ans [ 1 ] ) NEW_LINE DEDENT
Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X | Function to count the number of triplets ; Iterate through all the triplets ; Rearrange the numbers in ascending order ; Check if the necessary conditions satisfy ; Increment count ; Return the answer ; Driver code
def countTriplets ( a , n , x ) : NEW_LINE INDENT answer = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT temp = [ ] NEW_LINE temp . append ( a [ i ] ) NEW_LINE temp . append ( a [ j ] ) NEW_LINE temp . append ( a [ k ] ) NEW_LINE temp . sort ( ) NEW_LINE if ( temp [ 0 ] < temp [ 1 ] and temp [ 1 ] < temp [ 2 ] and temp [ 0 ] * temp [ 1 ] * temp [ 2 ] <= x ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return answer NEW_LINE DEDENT A = [ 3 , 2 , 5 , 7 ] NEW_LINE N = len ( A ) NEW_LINE X = 42 NEW_LINE print ( countTriplets ( A , N , X ) ) NEW_LINE
Largest value of x such that axx is N | Python3 implementation of the above approach ; Function to find log_b ( a ) ; Set two pointer for binary search ; Calculating number of digits of a * mid ^ mid in base b ; If number of digits > n we can simply ignore it and decrease our pointer ; if number of digits <= n , we can go higher to reach value exactly equal to n ; return the largest value of x ; Driver Code
from math import log10 , ceil , log NEW_LINE def log1 ( a , b ) : NEW_LINE INDENT return log10 ( a ) // log10 ( b ) NEW_LINE DEDENT def get ( a , b , n ) : NEW_LINE INDENT lo = 0 NEW_LINE hi = 1e6 NEW_LINE ans = 0 NEW_LINE while ( lo <= hi ) : NEW_LINE INDENT mid = ( lo + hi ) // 2 NEW_LINE dig = ceil ( ( mid * log ( mid , b ) + log ( a , b ) ) ) NEW_LINE if ( dig > n ) : NEW_LINE INDENT hi = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans = mid NEW_LINE lo = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 2 NEW_LINE b = 2 NEW_LINE n = 6 NEW_LINE print ( int ( get ( a , b , n ) ) ) NEW_LINE DEDENT
Maximum size square Sub | Function to find the maximum size of matrix with sum <= K ; N size of rows and M size of cols ; To store the prefix sum of matrix ; Create Prefix Sum ; Traverse each rows ; Update the prefix sum till index i x j ; To store the maximum size of matrix with sum <= K ; Traverse the sum matrix ; Index out of bound ; Maximum possible size of matrix ; Binary Search ; Find middle index ; Check whether sum <= K or not If Yes check for other half of the search ; Else check it in first half ; Update the maximum size matrix ; Print the final answer ; Driver Code ; Given target sum ; Function Call
def findMaxMatrixSize ( arr , K ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE m = len ( arr [ 0 ] ) NEW_LINE sum = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( m + 1 ) : NEW_LINE INDENT if ( i == 0 or j == 0 ) : NEW_LINE INDENT sum [ i ] [ j ] = 0 NEW_LINE continue NEW_LINE DEDENT sum [ i ] [ j ] = arr [ i - 1 ] [ j - 1 ] + sum [ i - 1 ] [ j ] + sum [ i ] [ j - 1 ] - sum [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( i + ans - 1 > n or j + ans - 1 > m ) : NEW_LINE INDENT break NEW_LINE DEDENT mid = ans NEW_LINE lo = ans NEW_LINE hi = min ( n - i + 1 , m - j + 1 ) NEW_LINE while ( lo < hi ) : NEW_LINE INDENT mid = ( hi + lo + 1 ) // 2 NEW_LINE if ( sum [ i + mid - 1 ] [ j + mid - 1 ] + sum [ i - 1 ] [ j - 1 ] - sum [ i + mid - 1 ] [ j - 1 ] - sum [ i - 1 ] [ j + mid - 1 ] <= K ) : NEW_LINE INDENT lo = mid NEW_LINE DEDENT else : NEW_LINE INDENT hi = mid - 1 NEW_LINE DEDENT DEDENT ans = max ( ans , lo ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 1 , 3 , 2 , 4 , 3 , 2 ] , [ 1 , 1 , 3 , 2 , 4 , 3 , 2 ] , [ 1 , 1 , 3 , 2 , 4 , 3 , 2 ] ] NEW_LINE K = 4 NEW_LINE findMaxMatrixSize ( arr , K ) NEW_LINE DEDENT
Rank of all elements in a Stream in descending order when they arrive | FindRank function to find rank ; Rank of first element is always 1 ; Iterate over array ; As element let say its rank is 1 ; Element is compared with previous elements ; If greater than previous than rank is incremented ; print rank ; Driver code ; array named arr ; length of arr
def FindRank ( arr , length ) : NEW_LINE INDENT print ( 1 , end = " ▁ " ) NEW_LINE for i in range ( 1 , length ) : NEW_LINE INDENT rank = 1 NEW_LINE for j in range ( 0 , i ) : NEW_LINE INDENT if ( arr [ j ] > arr [ i ] ) : NEW_LINE INDENT rank = rank + 1 NEW_LINE DEDENT DEDENT print ( rank , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 88 , 14 , 69 , 30 , 29 , 89 ] NEW_LINE length = len ( arr ) NEW_LINE FindRank ( arr , length ) NEW_LINE DEDENT
Longest permutation subsequence in a given array | Program to find length of Longest Permutation Subsequence in a given array ; Function to find the longest permutation subsequence ; Map data structure to count the frequency of each element ; If frequency of element is 0 , then we can not move forward as every element should be present ; Increasing the length by one ; Driver Code
from collections import defaultdict NEW_LINE def longestPermutation ( a , n ) : NEW_LINE INDENT freq = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ a [ i ] ] += 1 NEW_LINE DEDENT length = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( freq [ i ] == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT length += 1 NEW_LINE DEDENT return length NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 6 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( longestPermutation ( arr , n ) ) NEW_LINE DEDENT
Pair of fibonacci numbers with a given sum and minimum absolute difference | Python 3 program to find the pair of Fibonacci numbers with a given sum and minimum absolute difference ; Hash to store all the Fibonacci numbers ; Function to generate fibonacci Series and create hash table to check Fibonacci numbers ; Adding the first two Fibonacci numbers into the Hash set ; Computing the remaining Fibonacci numbers based on the previous two numbers ; Function to find the pair of Fibonacci numbers with the given sum and minimum absolute difference ; Start from N / 2 such that the difference between i and N - i will be minimum ; If both ' i ' and ' sum ▁ - ▁ i ' are fibonacci numbers then print them and break the loop ; If there is no Fibonacci pair possible ; Driver code ; Generate the Fibonacci numbers ; Find the Fibonacci pair
MAX = 10005 NEW_LINE fib = set ( ) NEW_LINE def fibonacci ( ) : NEW_LINE INDENT global fib NEW_LINE global MAX NEW_LINE prev = 0 NEW_LINE curr = 1 NEW_LINE l = 2 NEW_LINE fib . add ( prev ) NEW_LINE fib . add ( curr ) NEW_LINE while ( l <= MAX ) : NEW_LINE INDENT temp = curr + prev NEW_LINE fib . add ( temp ) NEW_LINE prev = curr NEW_LINE curr = temp NEW_LINE l += 1 NEW_LINE DEDENT DEDENT def findFibonacci ( N ) : NEW_LINE INDENT global fib NEW_LINE i = N // 2 NEW_LINE while ( i > 1 ) : NEW_LINE INDENT if ( i in fib and ( N - i ) in fib ) : NEW_LINE INDENT print ( i , ( N - i ) ) NEW_LINE return NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT print ( " - 1" ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT fibonacci ( ) NEW_LINE sum = 199 NEW_LINE findFibonacci ( sum ) NEW_LINE DEDENT
Check if sum of Fibonacci elements in an Array is a Fibonacci number or not | Python 3 program to check whether the sum of fibonacci elements of the array is a Fibonacci number or not ; Hash to store the Fibonacci numbers up to Max ; Function to create the hash table to check Fibonacci numbers ; Inserting the first two Fibonacci numbers into the hash ; Add the remaining Fibonacci numbers based on the previous two numbers ; Function to check if the sum of Fibonacci numbers is Fibonacci or not ; Find the sum of all Fibonacci numbers ; Iterating through the array ; If the sum is Fibonacci then return true ; Driver code ; array of elements ; Creating a set containing all fibonacci numbers
MAX = 100005 NEW_LINE fibonacci = set ( ) NEW_LINE def createHash ( ) : NEW_LINE INDENT global fibonacci NEW_LINE prev , curr = 0 , 1 NEW_LINE fibonacci . add ( prev ) NEW_LINE fibonacci . add ( curr ) NEW_LINE while ( curr <= MAX ) : NEW_LINE INDENT temp = curr + prev NEW_LINE if temp <= MAX : NEW_LINE INDENT fibonacci . add ( temp ) NEW_LINE DEDENT prev = curr NEW_LINE curr = temp NEW_LINE DEDENT DEDENT def checkArray ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] in fibonacci ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT DEDENT if ( sum in fibonacci ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 8 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE createHash ( ) NEW_LINE if ( checkArray ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of numbers whose difference with Fibonacci count upto them is atleast K | Python 3 program to find the count of numbers whose difference with Fibonacci count upto them is atleast K ; fibUpto [ i ] denotes the count of fibonacci numbers upto i ; Function to compute all the Fibonacci numbers and update fibUpto array ; Store the first two Fibonacci numbers ; Compute the Fibonacci numbers and store them in isFib array ; Compute fibUpto array ; Function to return the count of valid numbers ; Compute fibUpto array ; Binary search to find the minimum number that follows the condition ; Check if the number is valid , try to reduce it ; Ans is the minimum valid number ; Driver Code
MAX = 1000005 NEW_LINE fibUpto = [ 0 ] * ( MAX + 1 ) NEW_LINE def compute ( sz ) : NEW_LINE INDENT isFib = [ False ] * ( sz + 1 ) NEW_LINE prev = 0 NEW_LINE curr = 1 NEW_LINE isFib [ prev ] = True NEW_LINE isFib [ curr ] = True NEW_LINE while ( curr <= sz ) : NEW_LINE INDENT temp = curr + prev NEW_LINE if ( temp <= sz ) : NEW_LINE INDENT isFib [ temp ] = True NEW_LINE DEDENT prev = curr NEW_LINE curr = temp NEW_LINE DEDENT fibUpto [ 0 ] = 1 NEW_LINE for i in range ( 1 , sz + 1 ) : NEW_LINE INDENT fibUpto [ i ] = fibUpto [ i - 1 ] NEW_LINE if ( isFib [ i ] ) : NEW_LINE INDENT fibUpto [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT def countOfNumbers ( N , K ) : NEW_LINE INDENT compute ( N ) NEW_LINE low , high , ans = 1 , N , 0 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) >> 1 NEW_LINE if ( mid - fibUpto [ mid ] >= K ) : NEW_LINE INDENT ans = mid NEW_LINE high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT if ( ans ) : NEW_LINE INDENT return ( N - ans + 1 ) NEW_LINE DEDENT return 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 NEW_LINE K = 3 NEW_LINE print ( countOfNumbers ( N , K ) ) NEW_LINE DEDENT
Smallest Palindromic Subsequence of Even Length in Range [ L , R ] | Python3 program to find lexicographically smallest palindromic subsequence of even length ; Frequency array for each character ; Preprocess the frequency array calculation ; Frequency array to track each character in position 'i ; Calculating prefix sum over this frequency array to get frequency of a character in a range [ L , R ] . ; Util function for palindromic subsequences ; Find frequency of all characters ; For each character find it 's frequency  in range [L, R] ; If frequency in this range is > 1 , then we must take this character , as it will give lexicographically smallest one ; There is no character in range [ L , R ] such that it 's frequency is > 1. ; Return the character 's value ; Function to find lexicographically smallest palindromic subsequence of even length ; Find in the palindromic subsequences ; No such subsequence exists ; Driver Code ; Function calls
N = 100001 NEW_LINE f = [ [ 0 for x in range ( N ) ] for y in range ( 26 ) ] NEW_LINE def precompute ( s , n ) : NEW_LINE ' NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT f [ ord ( s [ i ] ) - ord ( ' a ' ) ] [ i ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT for j in range ( 1 , n ) : NEW_LINE INDENT f [ i ] [ j ] += f [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT def palindromicSubsequencesUtil ( L , R ) : NEW_LINE INDENT ok = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT cnt = f [ i ] [ R ] NEW_LINE if ( L > 0 ) : NEW_LINE INDENT cnt -= f [ i ] [ L - 1 ] NEW_LINE DEDENT if ( cnt > 1 ) : NEW_LINE INDENT ok = 1 NEW_LINE c = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( ok == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return c NEW_LINE DEDENT def palindromicSubsequences ( Q , l ) : NEW_LINE INDENT for i in range ( l ) : NEW_LINE INDENT x = palindromicSubsequencesUtil ( Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) NEW_LINE if ( x == - 1 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT c = ord ( ' a ' ) + x NEW_LINE print ( 2 * chr ( c ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " dbdeke " NEW_LINE Q = [ [ 0 , 5 ] , [ 1 , 5 ] , [ 1 , 3 ] ] NEW_LINE n = len ( st ) NEW_LINE l = len ( Q ) NEW_LINE precompute ( st , n ) NEW_LINE palindromicSubsequences ( Q , l ) NEW_LINE DEDENT