codeparrot/xlcost-text-to-code huggingface dataset:
Distribute R , B beans such that each packet has at least 1 R and 1 B bean with absolute difference at most D | Function to check if it is possible to distribute R red and B blue beans in packets such that the difference between the beans in each packet is atmost D ; Check for the condition to distributing beans ; Print the answer ; Distribution is not possible ; Driver Code
function checkDistribution ( R , B , D ) { if ( Math . max ( R , B ) <= Math . min ( R , B ) * ( D + 1 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let R = 1 , B = 1 , D = 0 ; checkDistribution ( R , B , D ) ;
Generate a sequence with product N such that for every pair of indices ( i , j ) and i < j , arr [ j ] is divisible by arr [ i ] | Function to calculate the prime factors of N with their count ; Initialize a vector , say v ; Initialize the count ; Count the number of divisors ; Divide the value of N by 2 ; For factor 2 divides it ; Find all prime factors ; Count their frequency ; Push it to the vector ; Push N if it is not 1 ; Function to print the array that have the maximum size ; Stores the all prime factor and their powers ; Traverse the vector and find the maximum power of prime factor ; If max size is less than 2 ; Otherwise ; Print the maximum size of sequence ; Print the final sequence ; Print the prime factor ; Print the last value of the sequence ; Driver Code
function primeFactor ( N ) { let v = [ ] ; let count = 0 ; while ( ! ( N % 2 ) ) { N >>= 1 ; count ++ ; } if ( count ) v . push ( [ 2 , count ] ) ; for ( let i = 3 ; i <= Math . sqrt ( N ) ; i += 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = Math . floor ( N / i ) ; } if ( count ) { v . push ( [ i , count ] ) ; } } if ( N > 2 ) v . push ( [ N , 1 ] ) ; return v ; } function printAnswer ( n ) { let v = primeFactor ( n ) ; let maxi_size = 0 , prime_factor = 0 ; for ( let i = 0 ; i < v . length ; i ++ ) { if ( maxi_size < v [ i ] [ 1 ] ) { maxi_size = v [ i ] [ 1 ] ; prime_factor = v [ i ] [ 0 ] ; } } if ( maxi_size < 2 ) { document . write ( 1 + ' ' + n ) ; } else { let product = 1 ; document . write ( maxi_size + " " ) ; for ( let i = 0 ; i < maxi_size - 1 ; i ++ ) { document . write ( prime_factor + " " ) ; product *= prime_factor ; } document . write ( Math . floor ( ( n / product ) ) ) ; } } let N = 360 ; printAnswer ( N ) ;
Check if it is possible to reach destination in even number of steps in an Infinite Matrix | Function to check destination can be reached from source in even number of steps ; Coordinates differences ; minimum number of steps required ; Minsteps is even ; Minsteps is odd ; Given Input ; Function Call
function IsEvenPath ( Source , Destination ) { let x_dif = Math . abs ( Source [ 0 ] - Destination [ 0 ] ) ; let y_dif = Math . abs ( Source [ 1 ] - Destination [ 1 ] ) ; let minsteps = x_dif + y_dif ; if ( minsteps % 2 == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } let Source = [ 2 , 1 ] ; let Destination = [ 1 , 4 ] ; IsEvenPath ( Source , Destination ) ;
Find the closest Fraction to given fraction having minimum absolute difference | Function to find the absolute value of x ; Function to find the fraction with minimum absolute difference ; Initialize the answer variables ; Iterate over the range ; Nearest fraction ; x / y - d / i < x / y - A / B ( B * x - y * A ) * ( i * y ) > ( i * x - y * d ) * ( B * y ) ; Check for d + 1 ; Print the answer ; Driver code
function ABS ( x ) { return Math . max ( x , - x ) ; } function findFraction ( x , y , n ) { let A = - 1 , B = - 1 ; for ( let i = 1 ; i <= n ; i ++ ) { let d = Math . floor ( ( i * x ) / y ) ; if ( d >= 0 && ( A == - 1 || ABS ( B * x - y * A ) * ABS ( i * y ) > ABS ( i * x - y * d ) * ABS ( B * y ) ) ) A = d ; B = i ; d ++ ; if ( d >= 0 && ( A == - 1 || ABS ( B * x - y * A ) * ABS ( i * y ) > ABS ( i * x - y * d ) * ABS ( B * y ) ) ) A = d ; B = i ; } A -- ; B -- ; document . write ( A + " " + B ) ; } let x = 3 ; let y = 7 ; let n = 6 ; findFraction ( x , y , n ) ;
Represent a number N in base | Function to convert N to equivalent representation in base - 2 ; Stores the required answer ; Iterate until N is not equal to zero ; If N is Even ; Add char '0' in front of string ; Add char '1' in front of string ; Decrement N by 1 ; Divide N by - 2 ; If string is empty , that means N is zero ; Put '0' in string s ; Given Input ; Function Call
function BaseConversion ( N ) { let s = " " ; while ( N != 0 ) { if ( N % 2 == 0 ) { s = " " + s ; } else { s = " " + s ; N -- ; } N /= - 2 ; } if ( s == " " ) { s = " " ; } return s ; } let N = - 9 ; document . write ( BaseConversion ( N ) ) ;
Find instances at end of time frame after auto scaling | 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
function finalInstances ( instances , arr ) { let i = 0 ; while ( i < arr . length ) { if ( arr [ i ] < 25 && instances > 1 ) { instances = Math . ceil ( instances / 2 ) ; i = i + 10 ; } else if ( arr [ i ] > 60 && instances <= Math . pow ( 10 , 8 ) ) { instances = instances * 2 ; i = i + 10 ; } i = i + 1 ; } document . write ( instances ) ; } let instances = 2 ; let arr = [ 25 , 23 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 76 , 80 ] ; finalInstances ( instances , arr ) ;
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 ; Otherwise , ; Driver code
function isMakeZero ( arr , N , K ) { let MP = new Map ( ) ; let V = [ ] ; let X = 1 ; let i ; while ( X > 0 && X < Number . MAX_VALUE ) { V . push ( X ) ; X *= K ; } for ( i = 0 ; i < N ; i ++ ) { for ( let j = V . length - 1 ; j >= 0 ; j -- ) { if ( MP . has ( V [ j ] ) == false && V [ j ] <= arr [ i ] ) { arr [ i ] -= V [ j ] ; MP . set ( V [ j ] , 1 ) ; } } if ( arr [ i ] != 0 ) break ; } if ( i < N ) return " " ; else return " " ; } let arr = [ 8 , 0 , 3 , 4 , 80 ] ; let N = arr . length ; let K = 2 ; document . write ( isMakeZero ( arr , N , K ) ) ;
Check whether the product of every subsequence is a perfect square or not | 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
function perfectSquare ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { let p = Math . sqrt ( arr [ i ] ) ; if ( p * p != arr [ i ] ) { return " " ; } } return " " ; } let arr = [ 1 , 4 , 100 ] ; let N = arr . length ; document . write ( perfectSquare ( arr , N ) ) ;
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 ; Given Input ; Function Call
function countKAverageSubarrays ( arr , n , k ) { let res = 0 ; for ( let L = 0 ; L < n ; L ++ ) { let sum = 0 ; for ( let R = L ; R < n ; R ++ ) { sum += arr [ R ] ; let len = R - L + 1 ; if ( sum % len == 0 ) { let avg = sum / len ; if ( avg == k ) res ++ ; } } } return res ; } let K = 6 ; let arr = [ 12 , 5 , 3 , 10 , 4 , 8 , 10 , 12 , - 6 , - 1 ] ; let N = arr . length ; document . write ( countKAverageSubarrays ( arr , N , K ) ) ;
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
function minimumElement ( arr , N , K ) { let minElement = arr [ 0 ] ; for ( let i = 0 ; i < N ; ++ i ) { minElement = Math . min ( minElement , arr [ i ] ) ; } let reqOperations = 0 ; for ( let i = 0 ; i < N ; ++ i ) { reqOperations += arr [ i ] - minElement ; } if ( reqOperations < K ) { K -= reqOperations ; minElement -= Math . floor ( ( K + N - 1 ) / N ) ; } return minElement ; } let arr = [ 10 , 10 , 10 , 10 ] ; let K = 7 ; let N = arr . length ; document . write ( minimumElement ( arr , N , K ) ) ;
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
function FractionSplit ( n , d ) { let UnitFactions = [ ] ; while ( n > 0 ) { let x = Math . floor ( ( d + n - 1 ) / n ) ; let s = " " + String ( x ) ; UnitFactions . push ( s ) ; n = n * x - d ; d = d * x ; } return UnitFactions ; } let n = 13 , d = 18 ; let res = FractionSplit ( n , d ) ; for ( let s of res ) document . write ( s + " " ) ;
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
function findPermutation ( N ) { var i ; var arr = new Array ( N ) ; for ( i = 0 ; i < N ; i ++ ) { arr [ i ] = i + 1 ; } for ( i = 1 ; i < N ; i += 2 ) { var temp = arr [ i ] ; arr [ i ] = arr [ i - 1 ] ; arr [ i - 1 ] = temp ; } if ( N % 2 == 1 && N > 1 ) { var temp = arr [ N - 1 ] ; arr [ N - 1 ] = arr [ N - 2 ] ; arr [ N - 2 ] = temp ; } for ( i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } var N = 7 ; findPermutation ( N ) ;
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
function addAP ( A , Q , operations ) { for ( let Q of operations ) { let L = Q [ 0 ] , R = Q [ 1 ] , a = Q [ 2 ] , d = Q [ 3 ] curr = a for ( let i = L - 1 ; i < R ; i ++ ) { A [ i ] += curr curr += d } } for ( let i of A ) { document . write ( i + " " ) } } let A = [ 5 , 4 , 2 , 8 ] let Q = 2 let Query = [ [ 1 , 2 , 1 , 3 ] , [ 1 , 4 , 4 , 1 ] ] addAP ( A , Q , Query )
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
function maximizeNumber ( N , K ) { let s = String ( N ) ; let L = s . length ; let result = [ ] ; let i = 0 ; while ( ( i < L ) && ( K <= ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ) { result . push ( s [ i ] ) ; ++ i ; } result . push ( String . fromCharCode ( K + ' ' . charCodeAt ( 0 ) ) ) ; while ( i < L ) { result . push ( s [ i ] ) ; ++ i ; } document . write ( result . join ( " " ) ) ; } let N = 6673 , K = 6 ; maximizeNumber ( N , K ) ;
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
function power ( x , y ) { let res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; } function countArrays ( N , K ) { document . write ( power ( N , K ) ) ; } let N = 5 , K = 6 ; countArrays ( N , K ) ;
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
function minimumDistance ( arr , N ) { let ind = 0 ; let prev = arr [ ind ] ; let s = arr . length ; for ( let i = 0 ; i < N ; i ++ ) { let distance = Number . MAX_SAFE_INTEGER ; if ( i < arr [ 0 ] ) { distance = arr [ 0 ] - i ; } else if ( i >= prev && ind + 1 < s && i <= arr [ ind + 1 ] ) { distance = Math . min ( i - prev , arr [ ind + 1 ] - i ) ; if ( i == arr [ ind + 1 ] ) { distance = 0 ; prev = arr [ ind + 1 ] ; ind ++ ; } } else { distance = i - prev ; } document . write ( distance + " " ) ; } } let N = 5 ; let arr = [ 0 , 4 ] ; minimumDistance ( arr , N ) ;
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
function countOfPairs ( arr , N , X ) { let count = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { for ( let j = i + 1 ; j < N ; j ++ ) { if ( ( ( arr [ i ] ^ arr [ j ] ) & X ) == 0 ) count ++ ; } } return count ; } let arr = [ 3 , 2 , 5 , 4 , 6 , 7 ] ; let X = 6 ; let N = arr . length ; document . write ( countOfPairs ( arr , N , X ) ) ;
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 ; Initializing the map M ; Populating 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
function countOfPairs ( arr , N , X ) { let count = 0 ; let M = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( M . has ( arr [ i ] & X ) ) { M . set ( arr [ i ] & X , M . get ( arr [ i ] & X ) + 1 ) ; } else { M . set ( arr [ i ] & X , 1 ) ; } } for ( let m of M ) { let p = m [ 1 ] ; count += ( p * ( p - 1 ) ) / 2 ; } return count ; } let arr = [ 3 , 2 , 5 , 4 , 6 , 7 ] ; let X = 6 ; let N = arr . length ; document . write ( countOfPairs ( arr , N , X ) ) ;
Count of square submatrices with average at least K | Javascript program for the above approach ; 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 ; Given Input ; Function Call
let MAX = 1000 function cntMatrices ( arr , N , M , K ) { let cnt = 0 ; let pre = new Array ( N + 1 ) . fill ( 0 ) . map ( ( ) => new Array ( M + 1 ) . fill ( 0 ) ) for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= M ; j ++ ) { pre [ i ] [ j ] = arr [ i - 1 ] [ j - 1 ] + pre [ i - 1 ] [ j ] + pre [ i ] [ j - 1 ] - pre [ i - 1 ] [ j - 1 ] ; } } for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= M ; j ++ ) { for ( let l = i , r = j ; l > 0 && r > 0 ; l -- , r -- ) { let sum1 = ( K * ( i - l + 1 ) * ( i - r + 1 ) ) ; let sum2 = pre [ i ] [ j ] - pre [ l - 1 ] [ r ] - pre [ l ] [ r - 1 ] + pre [ l - 1 ] [ r - 1 ] ; if ( sum1 <= sum2 ) cnt ++ ; } } } return cnt ; } let arr = [ [ 2 , 2 , 3 ] , [ 3 , 4 , 5 ] , [ 4 , 5 , 5 ] ] ; let K = 4 ; let N = arr . length ; let M = arr [ 0 ] . length ; document . write ( cntMatrices ( arr , N , M , K ) ) ;
Concatenate the Array of elements into a single element | 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 ; Input ; Function call
function ConcatenateArr ( arr , N ) { let ans = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { let l = Math . floor ( Math . log10 ( arr [ i ] ) + 1 ) ; ans = ans * Math . pow ( 10 , l ) ; ans += arr [ i ] ; } return ans ; } let arr = [ 1 , 23 , 456 ] ; let N = arr . length ; document . write ( ConcatenateArr ( arr , N ) ) ;
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 ; Given Input ; Function Call
function countXor ( N ) { let cnt = Math . floor ( N / 2 ) + 1 ; return cnt ; } let N = 4 ; document . write ( countXor ( N ) ) ;
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
function DistRecursion ( S , i , dist ) { if ( i == S . length ) return Math . abs ( dist ) ; if ( S [ i ] == ' ' ) return DistRecursion ( S , i + 1 , dist - 1 ) ; if ( S [ i ] == ' ' ) return DistRecursion ( S , i + 1 , dist + 1 ) ; return Math . max ( DistRecursion ( S , i + 1 , dist - 1 ) , DistRecursion ( S , i + 1 , dist + 1 ) ) ; } function maxDistance ( S ) { return DistRecursion ( S , 0 , 0 ) ; } let S = " " ; document . write ( maxDistance ( S ) ) ;
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 , print - 1 ; Print the pairs which satisfy the given condition ; Given Input ; Function Call
function findPairs ( p , q , r ) { let v = [ ] ; for ( let i = p ; i <= q ; i ++ ) { if ( i % r == 0 ) { v . push ( i ) ; } } let ans = [ ] ; for ( let i = 0 ; i < v . length ; i ++ ) { for ( let j = i + 1 ; j < v . length ; j ++ ) { if ( v [ i ] * v [ j ] >= p * q / 4 && v [ i ] * v [ j ] <= p * q ) { ans . push ( [ v [ i ] , v [ j ] ] ) ; } } } if ( ans . length == 0 ) { document . write ( - 1 + " " ) ; } else { for ( let i = 0 ; i < ans . length ; i ++ ) { document . write ( ans [ i ] [ 0 ] + " " + ans [ i ] [ 1 ] + " " ) ; } } } let p = 14 , q = 30 , r = 5 ; findPairs ( p , q , r ) ;
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
function printRectPattern ( c1 , c2 , n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { document . write ( Math . max ( Math . abs ( c1 - i ) , Math . abs ( c2 - j ) ) + " " ) ; } document . write ( " " ) ; } } let c1 = 2 ; let c2 = 2 ; let n = 5 ; printRectPattern ( c1 , c2 , n ) ;
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
function countNumbers ( l , r ) { let count = 0 ; for ( let i = l ; i <= r ; i ++ ) { if ( ( i & 1 ) == 0 ) { count ++ ; } } return count ; } let l = 10 , r = 20 ; document . write ( countNumbers ( l , r ) ) ;
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
function countNumbers ( l , r ) { return ( parseInt ( r / 2 ) - parseInt ( ( l - 1 ) / 2 ) ) ; } let l = 10 , r = 20 ; document . write ( countNumbers ( l , r ) ) ;
Length of longest Fibonacci subarray | A utility function that returns true if x is perfect square ; Returns true if n is a Fibinacci Number , else false ; Here n is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both is a perferct 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 ; Given Input ; Function Call
function isPerfectSquare ( x ) { let s = parseInt ( Math . sqrt ( x ) ) ; return ( s * s == x ) ; } function isFibonacci ( n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } function contiguousFibonacciNumber ( arr , n ) { let current_length = 0 ; let max_length = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isFibonacci ( arr [ i ] ) ) { current_length ++ ; } else { current_length = 0 ; } max_length = Math . max ( max_length , current_length ) ; } return max_length ; } let arr = [ 11 , 8 , 21 , 5 , 3 , 28 , 4 ] ; let n = arr . length ; document . write ( contiguousFibonacciNumber ( arr , n ) ) ;
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
let direction = [ [ - 1 , 0 ] , [ 0 , 1 ] , [ 0 , - 1 ] , [ 1 , 0 ] ] ; function dfs ( matrix , i , j , N , M ) { if ( i == N - 1 && j == M - 1 ) { return true ; } matrix [ i ] [ j ] = 1 ; for ( let k = 0 ; k < 4 ; k ++ ) { let newX = i + direction [ k ] [ 0 ] ; let newY = j + direction [ k ] [ 1 ] ; if ( newX >= 0 && newX < N && newY >= 0 && newY < M && matrix [ newX ] [ newY ] == 0 ) { if ( dfs ( matrix , newX , newY , N , M ) ) { return true ; } } } return false ; } function solve ( matrix ) { let N = matrix . length ; let M = matrix [ 0 ] . length ; if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 0 ; } if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 1 ; } return 2 ; } let mat = [ [ 0 , 1 , 0 , 0 ] , [ 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ] ; document . write ( solve ( mat ) ) ;