codeparrot/xlcost-text-to-code huggingface dataset:
Lexicographically smallest string which differs from given strings at exactly K indices | javascript program for the above approach ; Function to find the String which differ at exactly K positions ; Initialise s3 as s2 ; Number of places at which s3 differ from s2 ; Minimum possible value is Math . ceil ( d / 2 ) if it is not possible then - 1 ; Case 2 when K is less equal d ; X show the modification such that this position only differ from only one String ; modify the position such that this differ from both S1 & S2 & decrease the T at each step ; Finding the character which is different from both S1 and S2 ; After we done T start modification to meet our requirement for X type ; Resultant String ; Case 1 when K > d In first step , modify all the character which are not same in S1 and S3 ; Finding character which is different from both S1 and S2 ; Our requirement not satisfied by performing step 1. We need to modify the position which matches in both String ; Finding the character which is different from both S1 and S2 ; Resultant String ; Driver Code ; Given two Strings ; Function Call
var arr = [ ' ' , ' ' , ' ' ] ; function findString ( n , k , s1 , s2 ) { var s3 = s2 ; var d = 0 ; for ( i = 0 ; i < s1 . length ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) d ++ ; } if ( ( d + 1 ) / 2 > k ) { document . write ( " " + " " ) ; return ; } else { if ( k <= d ) { var X = d - k ; var T = 2 * k - d ; for ( i = 0 ; i < s3 . length ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { if ( T > 0 ) { for ( j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s2 [ i ] ) { s3 [ i ] = arr [ j ] ; T -- ; break ; } } } else if ( X > 0 ) { s3 [ i ] = s1 [ i ] ; X -- ; } } } document . write ( s3 . join ( ' ' ) + " " ) ; } else { for ( i = 0 ; i < s1 . length ; i ++ ) { if ( s1 [ i ] != s3 [ i ] ) { for ( j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s3 [ i ] ) { s3 [ i ] = arr [ j ] ; k -- ; break ; } } } } for ( i = 0 ; i < s1 . length ; i ++ ) { if ( s1 [ i ] == s3 [ i ] && k > 0 ) { for ( j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s3 [ i ] ) { s3 [ i ] = arr [ j ] ; k -- ; break ; } } } } document . write ( s3 . join ( ' ' ) + " " ) ; } } } var N = 4 , k = 2 ; var S1 = " " ; var S2 = " " ; findString ( N , k , S1 . split ( ' ' ) , S2 . split ( ' ' ) ) ;
Count of pairs of integers whose difference of squares is equal to N | Function to find the integral solutions of the given equation ; Initialise count to 0 ; Iterate till sqrt ( N ) ; If divisor 's pair sum is even ; Print the total possible solutions ; Given number N ; Function Call
function findSolutions ( N ) { let count = 0 ; for ( let i = 1 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( ( i + parseInt ( N / i ) ) % 2 == 0 ) { count ++ ; } } } document . write ( 4 * count + " " ) ; } let N = 80 ; findSolutions ( N ) ;
Find the integral roots of a given Cubic equation | Function to find the value at x of the given equation ; Find the value equation at x ; Return the value of ans ; Function to find the integral solution of the given equation ; Initialise start and end ; Implement Binary Search ; Find mid ; Find the value of f ( x ) using current mid ; Check if current mid satisfy the equation ; Print mid and return ; Print " NA " if not found any integral solution ; Driver Code ; Function Call
function check ( A , B , C , D , x ) { var ans ; ans = ( A * x * x * x + B * x * x + C * x + D ) ; return ans ; } function findSolution ( A , B , C , D , E ) { var start = 0 , end = 100000 ; var mid , ans ; while ( start <= end ) { mid = parseInt ( start + ( end - start ) / 2 ) ; ans = check ( A , B , C , D , mid ) ; if ( ans == E ) { document . write ( mid ) ; return ; } if ( ans < E ) start = mid + 1 ; else end = mid - 1 ; } document . write ( " " ) ; } var A = 1 , B = 0 , C = 0 ; var D = 0 , E = 27 ; findSolution ( A , B , C , D , E ) ;
Count of ordered triplets ( R , G , B ) in a given original string | function to count the ordered triplets ( R , G , B ) ; count the B ( blue ) colour ; Driver program
function countTriplets ( color ) { var result = 0 , Blue_Count = 0 ; var Red_Count = 0 ; var data = color . split ( ' ' ) ; for ( var i = 0 ; i < data . length ; i ++ ) { if ( data [ i ] == ' ' ) Blue_Count ++ ; } for ( var i = 0 ; i < data . length ; i ++ ) { if ( data [ i ] == ' ' ) Blue_Count -- ; if ( data [ i ] == ' ' ) Red_Count ++ ; if ( data [ i ] == ' ' ) result += Red_Count * Blue_Count ; } return result ; } var color = " " ; document . write ( countTriplets ( color ) ) ;
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
function naive_find_x ( N ) { var c = 0 , i ; for ( i = 1 ; i < N ; i ++ ) { c += i * i * i ; if ( c >= N ) break ; } return i ; } var N = 100 ; document . write ( naive_find_x ( N ) ) ;
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
function binary_searched_find_x ( k ) { var l = 0 ; var r = k ; var ans = 0 ; while ( l <= r ) { var mid = parseInt ( l + ( r - l ) / 2 ) ; if ( Math . pow ( ( ( mid * ( mid + 1 ) ) / 2 ) , 2 ) >= k ) { ans = mid ; r = mid - 1 ; } else { l = mid + 1 ; } } return ans ; } var N = 100 ; document . write ( binary_searched_find_x ( N ) ) ;
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
function lenOfLongZigZagArr ( a , n ) { var max = 1 , len = 1 ; for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( i % 2 == 0 && ( a [ i ] < a [ i + 1 ] ) ) len ++ ; else if ( i % 2 == 1 && ( a [ i ] > a [ i + 1 ] ) ) len ++ ; else { if ( max < len ) max = len ; len = 1 ; } } if ( max < len ) max = len ; return max ; } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var n = arr . length ; document . write ( lenOfLongZigZagArr ( arr , n ) ) ;
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
function checkPerfectSquare ( N , start , last ) { let mid = parseInt ( ( start + last ) / 2 ) ; if ( start > last ) { return - 1 ; } if ( mid * mid == N ) { return mid ; } else if ( mid * mid > N ) { return checkPerfectSquare ( N , start , mid - 1 ) ; } else { return checkPerfectSquare ( N , mid + 1 , last ) ; } } let N = 65 ; document . write ( checkPerfectSquare ( N , 1 , N ) ) ;
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
function minpoint ( arr , n , k ) { let min_point = 0 ; for ( let i = 1 ; i < k - 1 ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) min_point += 1 ; } let final_point = min_point ; for ( let i = k ; i < n ; i ++ ) { if ( arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) + 1 ] && arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) - 1 ] ) min_point -= 1 ; if ( arr [ i - 1 ] < arr [ i ] && arr [ i - 1 ] < arr [ i - 2 ] ) min_point += 1 ; if ( min_point > final_point ) final_point = min_point ; } document . write ( final_point ) ; } let arr = [ 2 , 1 , 4 , 2 , 3 , 4 , 1 , 2 ] ; let n = arr . length ; let k = 4 ; minpoint ( arr , n , k ) ;
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
function findSubArray ( a , n , k ) { let pref = new Array ( n ) ; pref [ 0 ] = 0 ; for ( let i = 1 ; i < n - 1 ; ++ i ) { pref [ i ] = pref [ i - 1 ] ; if ( a [ i ] > a [ i - 1 ] && a [ i ] > a [ i + 1 ] ) pref [ i ] ++ ; } let peak = 0 , left = 0 ; for ( let i = 0 ; i + k - 1 < n ; ++ i ) if ( pref [ i + k - 2 ] - pref [ i ] > peak ) { peak = pref [ i + k - 2 ] - pref [ i ] ; left = i ; } document . write ( " " + ( left + 1 ) + " " ) ; document . write ( " " + ( left + k ) + " " ) ; document . write ( " " + peak + " " ) ; } let arr = [ 3 , 2 , 3 , 2 , 1 ] ; let n = arr . length ; let k = 3 ; findSubArray ( arr , n , k ) ;
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
function find ( dividend , divisor , start , end ) { if ( start > end ) return [ 0 , dividend ] ; var mid = start + parseInt ( ( end - start ) / 2 ) ; var n = dividend - divisor * mid ; if ( n > divisor ) start = mid + 1 ; else if ( n < 0 ) end = mid - 1 ; else { if ( n == divisor ) { ++ mid ; n = 0 ; } return [ mid , n ] ; } return find ( dividend , divisor , start , end ) ; } function divide ( dividend , divisor ) { return find ( dividend , divisor , 1 , dividend ) ; } var dividend = 10 , divisor = 3 ; var ans = divide ( dividend , divisor ) ; document . write ( ans [ 0 ] + " " ) ; document . write ( ans [ 1 ] + " " ) ;
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
function countTriplets ( a , n , x ) { var answer = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i + 1 ; j < n ; j ++ ) { for ( var k = j + 1 ; k < n ; k ++ ) { var temp = [ ] ; temp . push ( a [ i ] ) ; temp . push ( a [ j ] ) ; temp . push ( a [ k ] ) ; temp . sort ( ( a , b ) => a - b ) if ( temp [ 0 ] < temp [ 1 ] && temp [ 1 ] < temp [ 2 ] && temp [ 0 ] * temp [ 1 ] * temp [ 2 ] <= x ) answer ++ ; } } } return answer ; } var A = [ 3 , 2 , 5 , 7 ] ; var N = A . length ; var X = 42 ; document . write ( countTriplets ( A , N , X ) ) ;
Largest value of x such that axx is N | Function to find log_b ( a ) ; Set two poleter 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 poleter ; If number of digits <= n , we can go higher to reach value exactly equal to n ; Return the largest value of x ; Driver Code
function log ( a , b ) { return ( Math . log10 ( a ) / Math . log10 ( b ) ) ; } function get ( a , b , n ) { let lo = 0 , hi = 1e6 ; let ans = 0 ; while ( lo <= hi ) { let mid = Math . floor ( ( lo + hi ) / 2 ) ; let dig = Math . ceil ( ( mid * log ( mid , b ) + log ( a , b ) ) ) ; if ( dig > n ) { hi = mid - 1 ; } else { ans = mid ; lo = mid + 1 ; } } return ans ; } let a = 2 , b = 2 , n = 6 ; document . write ( get ( a , b , n ) + " " ) ;
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
function findMaxMatrixSize ( arr , K ) { let i , j ; let n = arr . length ; let m = arr [ 0 ] . length ; let sum = [ ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { sum [ i ] = [ ] ; for ( j = 0 ; j < m + 1 ; j ++ ) { sum [ i ] [ j ] = 0 ; } } for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= m ; j ++ ) { if ( i == 0 j == 0 ) { sum [ i ] [ j ] = 0 ; continue ; } sum [ i ] [ j ] = arr [ i - 1 ] [ j - 1 ] + sum [ i - 1 ] [ j ] + sum [ i ] [ j - 1 ] - sum [ i - 1 ] [ j - 1 ] ; } } let ans = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { if ( i + ans - 1 > n j + ans - 1 > m ) break ; let mid , lo = ans ; let hi = Math . min ( n - i + 1 , m - j + 1 ) ; while ( lo < hi ) { mid = Math . floor ( ( hi + lo + 1 ) / 2 ) ; 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 ) { lo = mid ; } else { hi = mid - 1 ; } } ans = Math . max ( ans , lo ) ; } } document . write ( ans , ' ' ) ; } let arr = [ [ 1 , 1 , 3 , 2 , 4 , 3 , 2 ] , [ 1 , 1 , 3 , 2 , 4 , 3 , 2 ] , [ 1 , 1 , 3 , 2 , 4 , 3 , 2 ] ] ; let K = 4 ; findMaxMatrixSize ( arr , K ) ;
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 ; prvar rank ; array named arr ; length of arr
function FindRank ( arr , length ) { document . write ( " " + " " ) ; for ( i = 1 ; i < arr . length ; i ++ ) { var rank = 1 ; for ( j = 0 ; j < i ; j ++ ) { if ( arr [ j ] > arr [ i ] ) rank ++ ; } document . write ( rank + " " ) ; } } var arr = [ 88 , 14 , 69 , 30 , 29 , 89 ] ; var len = arr . length ; FindRank ( arr , len ) ;
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
function longestPermutation ( arr , n ) { let freq = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( freq . has ( arr [ i ] ) ) { freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . set ( arr [ i ] , 1 ) ; } } let len = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { if ( ! freq . has ( i ) ) { break ; } len ++ ; } return len ; } let arr = [ 3 , 2 , 1 , 6 , 5 ] ; let n = arr . length ; document . write ( longestPermutation ( arr , n ) ) ;
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 leto 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 prlet them and break the loop ; If there is no Fibonacci pair possible ; Generate the Fibonacci numbers ; Find the Fibonacci pair
let MAX = 1000005 ; let fib = new Set ( ) ; function fibonacci ( ) { let prev = 0 , curr = 1 , len = 2 ; fib . add ( prev ) ; fib . add ( curr ) ; while ( len <= MAX ) { let temp = curr + prev ; fib . add ( temp ) ; prev = curr ; curr = temp ; len ++ ; } } function findFibonacci ( N ) { for ( let i = Math . floor ( N / 2 ) ; i > 1 ; i -- ) { if ( fib . has ( i ) && fib . has ( N - i ) ) { document . write ( i + " " + ( N - i ) ) ; return ; } } document . write ( " " ) ; } fibonacci ( ) ; let sum = 199 ; findFibonacci ( sum ) ;
Check if sum of Fibonacci elements in an Array is a Fibonacci number or not | Javascript 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 leto 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 ; array of elements ; Creating a set containing all fibonacci numbers
let MAX = 100005 ; let fibonacci = new Set ( ) ; function createHash ( ) { let prev = 0 , curr = 1 ; fibonacci . add ( prev ) ; fibonacci . add ( curr ) ; while ( curr <= MAX ) { let temp = curr + prev ; fibonacci . add ( temp ) ; prev = curr ; curr = temp ; } } function checkArray ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( fibonacci . has ( arr [ i ] ) ) sum += arr [ i ] ; if ( fibonacci . has ( sum ) ) return true ; return false ; } let arr = [ 1 , 2 , 4 , 8 , 2 ] ; let n = arr . length ; createHash ( ) ; if ( checkArray ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of numbers whose difference with Fibonacci count upto them is atleast K | Javascript 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
let MAX = 1000005 ; let fibUpto = new Array ( MAX + 1 ) ; function compute ( sz ) { let isFib = new Array ( sz + 1 ) ; let prev = 0 , curr = 1 ; isFib [ prev ] = isFib [ curr ] = true ; while ( curr <= sz ) { let temp = curr + prev ; if ( temp <= sz ) isFib [ temp ] = true ; prev = curr ; curr = temp ; } fibUpto [ 0 ] = 1 ; for ( let i = 1 ; i <= sz ; i ++ ) { fibUpto [ i ] = fibUpto [ i - 1 ] ; if ( isFib [ i ] ) fibUpto [ i ] ++ ; } } function countOfNumbers ( N , K ) { compute ( N ) ; let low = 1 , high = N , ans = 0 ; while ( low <= high ) { let mid = ( low + high ) >> 1 ; if ( mid - fibUpto [ mid ] >= K ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ( ans > 0 ? N - ans + 1 : 0 ) ; } let N = 10 , K = 3 ; document . write ( countOfNumbers ( N , K ) ) ;
Smallest Palindromic Subsequence of Even Length in Range [ L , R ] | Javascript 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
var N = 100001 ; var f = Array . from ( Array ( 26 ) , ( ) => Array ( N ) . fill ( 0 ) ) ; function precompute ( s , n ) { for ( var i = 0 ; i < n ; i ++ ) { f [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] [ i ] ++ ; } for ( var i = 0 ; i < 26 ; i ++ ) { for ( var j = 1 ; j < n ; j ++ ) { f [ i ] [ j ] += f [ i ] [ j - 1 ] ; } } } function palindromicSubsequencesUtil ( L , R ) { var c , ok = 0 ; for ( var i = 0 ; i < 26 ; i ++ ) { var cnt = f [ i ] [ R ] ; if ( L > 0 ) cnt -= f [ i ] [ L - 1 ] ; if ( cnt > 1 ) { ok = 1 ; c = i ; break ; } } if ( ok == 0 ) { return - 1 ; } return c ; } function palindromicSubsequences ( Q , l ) { for ( var i = 0 ; i < l ; i ++ ) { var x = palindromicSubsequencesUtil ( Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) ; if ( x == - 1 ) { document . write ( - 1 + " " ) ; } else { var c = String . fromCharCode ( ' ' . charCodeAt ( 0 ) + x ) ; document . write ( c + c + " " ) ; } } } var str = " " ; var Q = [ [ 0 , 5 ] , [ 1 , 5 ] , [ 1 , 3 ] ] ; var n = str . length ; var l = Q . length ; precompute ( str , n ) ; palindromicSubsequences ( Q , l ) ;
Length of longest Fibonacci subarray formed by removing only one element | Javascript program to find length of the longest subarray with all fibonacci numbers ; Function to create hash table to check for Fibonacci numbers ; Insert first two fibnonacci numbers ; Summation of last two numbers ; Update the variable each time ; Function to find the longest fibonacci subarray ; Find maximum value in the array ; Creating a set containing Fibonacci numbers ; Left array is used to count number of continuous fibonacci numbers starting from left of current element ; Check if current element is a fibonacci number ; Right array is used to count number of continuous fibonacci numbers starting from right of current element ; Check if current element is a fibonacci number ; Driver code
let N = 100000 ; function createHash ( hash , maxElement ) { let prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { let temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } function longestFibSubarray ( arr , n ) { let max_val = Math . max ( ... arr ) ; let hash = new Set ( ) ; createHash ( hash , max_val ) ; let left = Array . from ( { length : n } , ( _ , i ) => 0 ) ; let right = Array . from ( { length : n } , ( _ , i ) => 0 ) ; let fibcount = 0 , res = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { left [ i ] = fibcount ; if ( hash . has ( arr [ i ] ) ) { fibcount ++ ; } else fibcount = 0 ; } fibcount = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { right [ i ] = fibcount ; if ( hash . has ( arr [ i ] ) ) { fibcount ++ ; } else fibcount = 0 ; } for ( let i = 0 ; i < n ; i ++ ) res = Math . max ( res , left [ i ] + right [ i ] ) ; return res ; } let arr = [ 2 , 8 , 5 , 7 , 3 , 5 , 7 ] ; let n = arr . length ; document . write ( longestFibSubarray ( arr , n ) + " " ) ;