codeparrot/xlcost-text-to-code huggingface dataset:
Sum of given N fractions in reduced form | Function to find GCD of a & b using Euclid Lemma ; Base Case ; Function to find the LCM of all elements in arr [ ] ; Initialize result ; Iterate arr [ ] to find LCM ; Return the final LCM ; Function to find the sum of N fraction in reduced form ; To store the sum of all final numerators ; Find the LCM of all denominator ; Find the sum of all N numerators & denominators ; Add each fraction one by one ; Find GCD of final numerator and denominator ; Convert into reduced form by dividing from GCD ; Print the final fraction ; Given N ; Given Numerator ; Given Denominator ; Function Call
function gcd ( a , b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } function findlcm ( arr , n ) { var ans = arr [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) { ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; } return ans ; } function addReduce ( n , num , den ) { var final_numerator = 0 ; var final_denominator = findlcm ( den , n ) ; for ( var i = 0 ; i < n ; i ++ ) { final_numerator = final_numerator + ( num [ i ] ) * parseInt ( final_denominator / den [ i ] ) ; } var GCD = gcd ( final_numerator , final_denominator ) ; final_numerator = parseInt ( final_numerator / GCD ) ; final_denominator = parseInt ( final_denominator / GCD ) ; document . write ( final_numerator + " " + final_denominator + " " ) ; } var N = 3 ; var arr1 = [ 1 , 2 , 5 ] ; var arr2 = [ 2 , 1 , 6 ] ; addReduce ( N , arr1 , arr2 ) ;
Minimum LCM of all pairs in a given array | function to compute GCD of two numbers ; function that return minimum possible lcm from any pair ; fix the ith element and iterate over all the array to find minimum LCM ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function minLCM ( arr , n ) { let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { let g = gcd ( arr [ i ] , arr [ j ] ) ; let lcm = arr [ i ] / g * arr [ j ] ; ans = Math . min ( ans , lcm ) ; } } return ans ; } let arr = [ 2 , 4 , 3 , 6 , 5 ] ; let n = arr . length ; document . write ( minLCM ( arr , n ) ) ;
Find two numbers whose difference of fourth power is equal to N | Function which find required x & y ; Upper limit of x & y , if such x & y exists ; num1 stores x ^ 4 ; num2 stores y ^ 4 ; If condition is satisfied the print and return ; If no such pair exists ; Driver code
function solve ( n ) { let upper_limit = Math . ceil ( Math . pow ( n , 1.0 / 4 ) ) ; for ( let x = 0 ; x <= upper_limit ; x ++ ) { for ( let y = 0 ; y <= upper_limit ; y ++ ) { let num1 = x * x * x * x ; let num2 = y * y * y * y ; if ( num1 - num2 == n ) { document . write ( " " + x + " " + y ) ; return ; } } } document . write ( - 1 ) ; } let n = 15 ; solve ( n ) ;
Check if count of even divisors of N is equal to count of odd divisors | Function to check if count of even and odd divisors are equal ; To store the count of even factors and odd factors ; Loop till [ 1 , sqrt ( N ) ] ; If divisors are equal add only one ; Check for even divisor ; Odd divisor ; Check for both divisor i . e . , i and N / i ; Check if i is odd or even ; Check if N / i is odd or even ; Return true if count of even_div and odd_div are equals ; Given Number ; Function Call
function divisorsSame ( n ) { let even_div = 0 , odd_div = 0 ; for ( let i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( Math . floor ( n / i ) == i ) { if ( i % 2 == 0 ) { even_div ++ ; } else { odd_div ++ ; } } else { if ( i % 2 == 0 ) { even_div ++ ; } else { odd_div ++ ; } if ( Math . floor ( n / i ) % 2 == 0 ) { even_div ++ ; } else { odd_div ++ ; } } } } return ( even_div == odd_div ) ; } let N = 6 ; if ( divisorsSame ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Check if N is a Balanced Prime number or not | Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if n is a Balanced prime ; If n is not a prime number or n is the first prime then return false ; Initialize previous_prime to n - 1 and next_prime to n + 1 ; Find next prime number ; Find previous prime number ; Arithmetic mean ; If n is a weak prime ; Driver code
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function isBalancedPrime ( n ) { if ( ! isPrime ( n ) n == 2 ) return false ; let previous_prime = n - 1 ; let next_prime = n + 1 ; while ( ! isPrime ( next_prime ) ) next_prime ++ ; while ( ! isPrime ( previous_prime ) ) previous_prime -- ; let mean = ( previous_prime + next_prime ) / 2 ; if ( n == mean ) return true ; else return false ; } let n = 53 ; if ( isBalancedPrime ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of nodes having odd divisors in the given subtree for Q queries | Javascript implementation to count the number of nodes having odd number of divisors for each query ; Adjacency list for tree . ; Array for values and answer at ith node . ; Function to check whether N has odd divisors or not ; DFS function to pre - compute the answers ; Initialize the count ; Repeat for every child ; Increase the count if current node has odd number of divisors ; Driver Code ; Adjacency List ; Function call
let N = 100001 ; let adj = new Array ( N ) ; let a = new Array ( N ) ; let ans = new Array ( N ) ; function hasOddNumberOfDivisors ( n ) { if ( Math . sqrt ( n ) == parseInt ( Math . sqrt ( n ) , 10 ) ) return true ; return false ; } function dfs ( node , parent ) { let count = 0 ; for ( let i = 0 ; i < adj [ node ] . length ; i ++ ) { if ( adj [ node ] [ i ] != parent ) { count += dfs ( adj [ node ] [ i ] , node ) ; } } if ( hasOddNumberOfDivisors ( a [ node ] ) ) ++ count ; ans [ node ] = count ; return count ; } let n = 5 ; let q = [ 4 , 1 , 5 , 3 ] ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; adj [ 1 ] . push ( 2 ) ; adj [ 2 ] . push ( 1 ) ; adj [ 2 ] . push ( 3 ) ; adj [ 3 ] . push ( 2 ) ; adj [ 3 ] . push ( 4 ) ; adj [ 4 ] . push ( 3 ) ; adj [ 1 ] . push ( 5 ) ; adj [ 5 ] . push ( 1 ) ; a [ 1 ] = 4 ; a [ 2 ] = 9 ; a [ 3 ] = 14 ; a [ 4 ] = 100 ; a [ 5 ] = 5 ; dfs ( 1 , - 1 ) ; for ( let i = 0 ; i < q . length ; i ++ ) { document . write ( ans [ q [ i ] ] + " " ) ; }
Minimum Cost to make all array elements equal using given operations | javascript implementation to find the minimum cost to make all array elements equal ; Checks if the value is less than middle element of the array ; Function that returns the cost of making all elements equal to current element ; Compute the lower bound of current element ; Calculate the requirement of add operation ; Calculate the requirement of subtract operation ; Compute minimum of left and right ; Computing the total cost of add and subtract operations ; Function that prints minimum cost of making all elements equal ; Sort the given array ; Calculate minimum from a + r and m ; Compute prefix sum and store in pref array ; Find the minimum cost from the given elements ; Finding the minimum cost from the other cases where minimum cost can occur ; Printing the minimum cost of making all elements equal ; Driver Code ; Function Call
function lowerBound ( array , length , value ) { var low = 0 ; var high = length ; while ( low < high ) { var mid = parseInt ( ( low + high ) / 2 ) ; if ( value <= array [ mid ] ) { high = mid ; } else { low = mid + 1 ; } } return low ; } function costCalculation ( current , arr , n , pref , a , r , minimum ) { var index = lowerBound ( arr , arr . length , current ) ; var left = index * current - pref [ index ] ; var right = pref [ n ] - pref [ index ] - ( n - index ) * current ; var res = Math . min ( left , right ) ; left -= res ; right -= res ; var total = res * minimum ; total += left * a ; total += right * r ; return total ; } function solve ( arr , n , a , r , m ) { arr . sort ( ) ; var minimum = Math . min ( a + r , m ) ; var pref = Array ( n + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) pref [ i + 1 ] = pref [ i ] + arr [ i ] ; var ans = 10000 ; for ( i = 0 ; i < n ; i ++ ) ans = Math . min ( ans , costCalculation ( arr [ i ] , arr , n , pref , a , r , minimum ) ) ; ans = Math . min ( ans , costCalculation ( pref [ n ] / n , arr , n , pref , a , r , minimum ) ) ; ans = Math . min ( ans , costCalculation ( pref [ n ] / n + 1 , arr , n , pref , a , r , minimum ) ) ; document . write ( ans ) ; } var arr = [ 5 , 5 , 3 , 6 , 5 ] ; var A = 1 , R = 2 , M = 4 ; var size = arr . length ; solve ( arr , size , A , R , M ) ;
Count of integers up to N which represent a Binary number | Function to return the count ; If the current last digit is 1 ; Add 2 ^ ( ctr - 1 ) possible integers to the answer ; If the current digit exceeds 1 ; Set answer as 2 ^ ctr - 1 as all possible binary integers with ctr number of digits can be obtained ; Driver Code
function countBinaries ( N ) { let ctr = 1 ; let ans = 0 ; while ( N > 0 ) { if ( N % 10 == 1 ) { ans += Math . pow ( 2 , ctr - 1 ) ; } else if ( N % 10 > 1 ) { ans = Math . pow ( 2 , ctr ) - 1 ; } ctr ++ ; N /= 10 ; } return ans ; } let N = 20 ; document . write ( countBinaries ( N ) ) ;
Count of integers up to N which represent a Binary number | Function to return the count ; PreCompute and store the powers of 2 ; If the current last digit is 1 ; Add 2 ^ ( ctr - 1 ) possible integers to the answer ; If the current digit exceeds 1 ; Set answer as 2 ^ ctr - 1 as all possible binary integers with ctr number of digits can be obtained ; Driver Code
function countBinaries ( N ) { let powersOfTwo = [ ] ; powersOfTwo . push ( 1 ) ; for ( let i = 1 ; i < 11 ; i ++ ) { powersOfTwo . push ( powersOfTwo [ i - 1 ] * 2 ) ; } let ctr = 1 ; let ans = 0 ; while ( N > 0 ) { if ( N % 10 == 1 ) { ans += powersOfTwo [ ctr - 1 ] ; } else if ( N % 10 > 1 ) { ans = powersOfTwo [ ctr ] - 1 ; } ctr ++ ; N /= 10 ; } return ans ; } let N = 20 ; document . write ( countBinaries ( N ) ) ;
Find the sum of the first Nth Centered Hexadecagonal Number | Centered_Hexadecagonal number function ; Formula to calculate nth Centered_Hexadecagonal number & return it into main function . ; Function to find the sum of the first N centered hexadecagonal number ; Variable to store the sum ; Loop to iterate through the first N numbers ; Finding the sum ; Driver Code ; Display first Nth Centered_Hexadecagonal number
function Centered_Hexadecagonal_num ( n ) { return ( 8 * n * n - 8 * n + 1 ) ; } function sum_Centered_Hexadecagonal_num ( n ) { let summ = 0 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Hexadecagonal_num ( i ) ; } return summ ; } let n = 5 ; document . write ( sum_Centered_Hexadecagonal_num ( n ) ) ;
Find the sum of the first N Centered heptagonal number | Function to find the N - th centered heptagonal number ; Formula to calculate nth centered heptagonal number ; Function to find the sum of the first N centered heptagonal numbers ; Variable to store the sum ; Iterating through the range 1 to N ; Driver Code
function center_heptagonal_num ( n ) { return ( 7 * n * n - 7 * n + 2 ) / 2 ; } function sum_center_heptagonal_num ( n ) { let summ = 0 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { summ += center_heptagonal_num ( i ) ; } return summ ; } let n = 5 ; document . write ( sum_center_heptagonal_num ( n ) ) ;
Find the sum of the first N Centered Dodecagonal Number | Function to find the N - th Centered Dodecagonal number ; Formula to calculate nth Centered_Dodecagonal number ; Function to find the sum of the first N Centered_Dodecagonal number ; Variable to store the sum ; Iterating from 1 to N ; Finding the sum ; Driver code
function Centered_Dodecagonal_num ( n ) { return 6 * n * ( n - 1 ) + 1 ; } function sum_Centered_Dodecagonal_num ( n ) { let summ = 0 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Dodecagonal_num ( i ) ; } return summ ; } let n = 5 ; document . write ( sum_Centered_Dodecagonal_num ( n ) ) ;
Find the sum of the first N Centered Octagonal Number | Function to find the N - th centered octagonal number ; Formula to calculate nth centered octagonal number ; Function to find the sum of the first N centered octagonal numbers ; Variable to store the sum ; Iterating through the range 1 to N ; Driver code
function center_Octagonal_num ( n ) { return ( 4 * n * n - 4 * n + 1 ) ; } function sum_center_Octagonal_num ( n ) { let summ = 0 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { summ += center_Octagonal_num ( i ) ; } return summ ; } let n = 5 ; document . write ( sum_center_Octagonal_num ( n ) ) ;
Find the sum of the first N Centered Decagonal Numbers | Function to find the N - th centred decagonal number ; Formula to calculate nth centered_decagonal number & return it into main function . ; Function to find the sum of the first N centered decagonal numbers ; Variable to store the sum ; Iterating through the range ; Driver code ; Display first Nth centered_decagonal number
function Centered_decagonal_num ( n ) { return ( 5 * n * n - 5 * n + 1 ) ; } function sum_Centered_decagonal_num ( n ) { let summ = 0 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_decagonal_num ( i ) ; } return summ ; } let n = 5 ; document . write ( sum_Centered_decagonal_num ( n ) ) ;
Find the sum of the first N Centered Octadecagonal Numbers | Function to find the N - th centered octadecagonal number ; Formula to calculate nth centered octadecagonal number ; Function to find the sum of the first N centered octadecagonal numbers ; Variable to store the sum ; Iterating through the range 1 to N ; Driver Code
function center_octadecagon_num ( n ) { return ( 9 * n * n - 9 * n + 1 ) ; } function sum_center_octadecagon_num ( n ) { let summ = 0 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { summ += center_octadecagon_num ( i ) ; } return summ ; } let n = 3 ; document . write ( sum_center_octadecagon_num ( n ) ) ;
Find the sum of the first Nth Centered Pentadecagonal Number | Function to find the centered pentadecagonal number ; Formula to calculate N - th centered pentadecagonal number ; Function to find the sum of the first N centered pentadecagonal numbers ; Variable to store the sum ; Driver Code
function Centered_Pentadecagonal_num ( n ) { return ( 15 * n * n - 15 * n + 2 ) / 2 ; } function sum_Centered_Pentadecagonal_num ( n ) { let summ = 0 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Pentadecagonal_num ( i ) ; } return summ ; } let n = 5 ; document . write ( sum_Centered_Pentadecagonal_num ( n ) ) ;
Program to check if N is a Octagonal Number | Function to check if N is a Octagonal Number ; Condition to check if the number is a octagonal number ; Given Number ; Function call
function isoctagonal ( N ) { var n = ( 2 + Math . sqrt ( 12 * N + 4 ) ) / 6 ; return ( n - parseInt ( n ) == 0 ) ; } var N = 8 ; if ( isoctagonal ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Program to check if N is a Pentadecagonal Number | Function to check if N is a Pentadecagon number ; Condition to check if the number is a Pentadecagon number ; Given Number ; Function call
function isPentadecagon ( N ) { var n = ( 11 + Math . sqrt ( 104 * N + 121 ) ) / 26 ; return ( n - parseInt ( n ) ) == 0 ; } var N = 15 ; if ( isPentadecagon ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Program to check if N is a Tetradecagonal Number | Function to check if N is a Tetradecagonal Number ; Condition to check if the number is a tetradecagonal number ; Given Number ; Function call
function istetradecagonal ( N ) { n = ( 10 + Math . sqrt ( 96 * N + 100 ) ) / 24 ; return ( n - parseInt ( n ) ) == 0 ; } N = 11 ; if ( istetradecagonal ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Find the sum of the first Nth Icosagonal Numbers | Function to calculate the N - th icosagonal number ; Formula to calculate nth icosagonal number & return it ; Function to find the sum of the first N icosagonal numbers ; Variable to store the sum ; Loop to iterate through the first N values and find the sum of first N icosagonal numbers ; Function to get the Icosagonal_num ; Driver code ; Display the sum of first N icosagonal number
function Icosagonal_num ( n ) { return ( 18 * n * n - 16 * n ) / 2 ; } function sum_Icosagonal_num ( n ) { let summ = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { summ += Icosagonal_num ( i ) ; } return summ ; } let n = 5 ; document . write ( sum_Icosagonal_num ( n ) ) ;
Find the sum of the first N Centered Pentagonal Number | Function to find the Centered_Pentagonal number ; Formula to calculate nth Centered_Pentagonal number & return it into main function . ; Function to find the sum of the first N Centered_Pentagonal numbers ; To get the sum ; Iterating through the range 1 to N ; Driver code ; Display first Nth Centered_Pentagonal number
function Centered_Pentagonal_num ( n ) { return ( 5 * n * n - 5 * n + 2 ) / 2 ; } function sum_Centered_Pentagonal_num ( n ) { let summ = 0 ; for ( let i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Pentagonal_num ( i ) ; } return summ ; } let n = 5 ; document . write ( sum_Centered_Pentagonal_num ( n ) ) ;
Find the sum of the first Nth Centered Tridecagonal Numbers | Function to calculate the N - th centered tridecagonal number ; Formula to calculate Nth centered tridecagonal number & return it ; Function to find the sum of the first N centered tridecagonal numbers ; Variable to store the sum ; Loop to iterate and find the sum of first N centered tridecagonal numbers ; Driver code
function Centered_tridecagonal_num ( n ) { return ( 13 * n * ( n - 1 ) + 2 ) / 2 ; } function sum_Centered_tridecagonal_num ( n ) { let summ = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { summ += Centered_tridecagonal_num ( i ) ; } return summ ; } let n = 5 ; document . write ( sum_Centered_tridecagonal_num ( n ) ) ;
Count Sexy Prime Pairs in the given array | To store check the prime number ; A utility function that find the Prime Numbers till N ; Resize the Prime Number ] ; Loop till sqrt ( N ) to find prime numbers and make their multiple false in the bool array Prime ; Function that returns the count of SPP ( Sexy Prime Pair ) Pairs ; Find the maximum element in the given array arr [ ] ; Function to calculate the prime numbers till N ; To store the count of pairs ; To store the frequency of element in the array arr [ ] ; Sort before traversing the array ; Traverse the array and find the pairs with SPP ( Sexy Prime Pair ) ; If current element is Prime , then check for ( current element + 6 ) ; Return the count of pairs ; Driver code ; Function call to find SPP ( Sexy Prime Pair ) pair
var Prime = Array ( 100 ) . fill ( true ) ; function computePrime ( N ) { var i , j ; Prime [ 0 ] = Prime [ 1 ] = false ; for ( i = 2 ; i * i <= N ; i ++ ) { if ( Prime [ i ] ) { for ( j = i * i ; j < N ; j += i ) { Prime [ j ] = false ; } } } } function countSexyPairs ( arr , n ) { var maxE = Math . max . apply ( Math , arr ) ; computePrime ( maxE ) ; var count = 0 ; var freq = Array ( maxE + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } arr . sort ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( Prime [ arr [ i ] ] ) { if ( freq [ arr [ i ] + 6 ] > 0 && Prime [ arr [ i ] + 6 ] ) { count ++ ; } } } return count ; } var arr = [ 6 , 7 , 5 , 11 , 13 ] ; var n = arr . length ; document . write ( countSexyPairs ( arr , n ) ) ;
Count of ways to write N as a sum of three numbers | Function to find the number of ways ; Check if number is less than 2 ; Calculate the sum ; Driver code
function countWays ( n ) { if ( n <= 2 ) document . write ( " " ) ; else { var ans = ( n - 1 ) * ( n - 2 ) / 2 ; document . write ( ans ) ; } } var N = 5 ; countWays ( N ) ;
Logarithm tricks for Competitive Programming | Function to check if the number is a power of two ; Driver Code
function isPowerOfTwo ( n ) { return ( Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) == Math . floor ( Math . log ( n ) / Math . log ( 2 ) ) ) ; } let N = 8 ; if ( isPowerOfTwo ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Count of pairs having bit size at most X and Bitwise OR equal to X | Function to count the pairs ; Initializing answer with 1 ; Iterating through bits of x ; Check if bit is 1 ; Multiplying ans by 3 if bit is 1 ; Driver code
function count_pairs ( x ) { var ans = 1 ; while ( x > 0 ) { if ( x % 2 == 1 ) ans = ans * 3 ; x = parseInt ( x / 2 ) ; } return ans ; } var X = 6 ; document . write ( count_pairs ( X ) + " " ) ;
Find the Kth number which is not divisible by N | Function to find the Kth not divisible by N ; Lowest possible value ; Highest possible value ; To store the Kth non divisible number of N ; Using binary search ; Calculating mid value ; Sol would have the value by subtracting all multiples of n till mid ; Check if sol is greater than k ; H should be reduced to find minimum possible value ; Check if sol is less than k then L will be mid + 1 ; Check if sol is equal to k ; ans will be mid ; H would be reduced to find any more possible value ; Print the answer ; Driver code ; Function Call
function kthNonDivisible ( N , K ) { let L = 1 ; let H = 2147483647 ; let ans = 0 ; while ( L <= H ) { let mid = parseInt ( ( L + H ) / 2 , 10 ) ; let sol = mid - parseInt ( mid / N , 10 ) ; if ( sol > K ) { H = mid - 1 ; } else if ( sol < K ) { L = mid + 1 ; } else { ans = mid ; H = mid - 1 ; } } document . write ( ans ) ; } let N = 3 ; let K = 7 ; kthNonDivisible ( N , K ) ;
Print any pair of integers with sum of GCD and LCM equals to N | Function to prvar the required pair ; Prvar the pair ; Driver code
function printPair ( n ) { document . write ( 1 + " " + ( n - 1 ) ) ; } var n = 14 ; printPair ( n ) ;
Find the length of largest subarray in which all elements are Autobiographical Numbers | function to check number is autobiographical ; Convert integer to string ; Iterate for every digit to check for their total count ; Check occurrence of every number and count them ; Check if any position mismatches with total count them return with false else continue with loop ; Function to return the length of the largest subarray whose every element is a Autobiographical number ; Utility function which checks every element of array for Autobiographical number ; Check if element arr [ i ] is an Autobiographical number ; Increment the current length ; Update max_length value ; Return the final result ; Driver code
function isAutoBiographyNum ( number ) { let count = 0 , position , size , digit ; let NUM ; NUM = number . toString ( ) ; size = NUM . length ; for ( let i = 0 ; i < size ; i ++ ) { position = NUM [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; count = 0 ; for ( let j = 0 ; j < size ; j ++ ) { digit = NUM [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( digit == i ) count ++ ; } if ( position != count ) return false ; } return true ; } function checkArray ( arr , n ) { let current_length = 0 ; let max_length = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( isAutoBiographyNum ( arr [ i ] ) ) current_length ++ ; else current_length = 0 ; max_length = Math . max ( max_length , current_length ) ; } return max_length ; } let arr = [ 21200 , 1 , 1303 , 1210 , 2020 ] ; let n = arr . length ; document . write ( checkArray ( arr , n ) ) ;