codeparrot/xlcost-text-to-code huggingface dataset:
Container with Most Water | Javascript code for Max Water Container ; Calculating the max area ; Driver code
function maxArea ( A , len ) { let area = 0 ; for ( let i = 0 ; i < len ; i ++ ) { for ( let j = i + 1 ; j < len ; j ++ ) { area = Math . max ( area , Math . min ( A [ j ] , A [ i ] ) * ( j - i ) ) ; } } return area ; } let a = [ 1 , 5 , 4 , 3 ] ; let b = [ 3 , 1 , 2 , 4 , 5 ] ; let len1 = a . length ; document . write ( maxArea ( a , len1 ) + " " ) ; let len2 = b . length ; document . write ( maxArea ( b , len2 ) ) ;
Program for focal length of a lens | Function to determine the focal length of a lens ; variable to store the distance between the lens and the image ; variable to store the distance between the lens and the object
function focal_length ( image_distance , object_distance ) { return 1 / ( ( 1 / image_distance ) + ( 1 / object_distance ) ) ; } let image_distance = 2 ; let object_distance = 50 ; document . write ( " " + focal_length ( image_distance , object_distance ) + " " ) ;
Count numbers in range L | check if the number is divisible by the digits . ; function to calculate the number of numbers ; Driver function
function check ( n ) { let m = n ; while ( n ) { let r = n % 10 ; if ( r > 0 ) if ( ( n % r ) != 0 ) return false ; n /= 10 ; } return true ; } function countIn ( l , r ) { let ans = 0 ; for ( let i = l ; i <= r ; i ++ ) if ( check ( i ) ) ans += 1 ; return ans ; } let l = 10 ; let r = 20 ; document . write ( countIn ( l , r ) ) ;
Sum of the Series 1 / ( 1 * 2 ) + 1 / ( 2 * 3 ) + 1 / ( 3 * 4 ) + 1 / ( 4 * 5 ) + . . . . . | function to find the sum of given series ; Computing sum term by term ; Driver code
function sumOfTheSeries ( n ) { let sum = 0.0 ; for ( let i = 1 ; i <= n ; i ++ ) sum += 1.0 / ( i * ( i + 1 ) ) ; return sum ; } let n = 10 ; document . write ( sumOfTheSeries ( n ) ) ;
Sum of series ( n / 1 ) + ( n / 2 ) + ( n / 3 ) + ( n / 4 ) + ... ... . + ( n / n ) | function to find sum of series ; Driver code
function sum ( n ) { let root = parseInt ( Math . sqrt ( n ) ) ; let ans = 0 ; for ( let i = 1 ; i <= root ; i ++ ) ans += parseInt ( n / i ) ; ans = ( 2 * ans ) - ( root * root ) ; return ans ; } let n = 35 ; document . write ( sum ( n ) ) ;
Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + â €¦ â €¦ + ( 2 + 4 + 6 + 8 + â €¦ . + 2 n ) | function to find the sum of the given series ; sum of 1 st n natural numbers ; sum of squares of 1 st n natural numbers ; required sum ; Driver code
function sumOfTheSeries ( n ) { let sum_n = ( n * ( n + 1 ) / 2 ) ; let sum_sq_n = ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; return ( sum_n + sum_sq_n ) ; } let n = 5 ; document . write ( " " + sumOfTheSeries ( n ) ) ;
Sum of squares of binomial coefficients | Return the sum of square of binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Finding the sum of square of binomial coefficient . ; Driver code
function sumofsquare ( n ) { let C = new Array ( n + 1 ) ; for ( let i = 0 ; i < C . length ; i ++ ) { C [ i ] = new Array ( 2 ) ; } let i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , n ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } let sum = 0 ; for ( i = 0 ; i <= n ; i ++ ) sum += ( C [ n ] [ i ] * C [ n ] [ i ] ) ; return sum ; } let n = 4 ; document . write ( sumofsquare ( n ) ) ;
Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n | Function that find sum of series . ; Driver Code ; Function call
function sumOfSeries ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; i ++ ) for ( let j = 1 ; j <= i ; j ++ ) sum = sum + i ; return sum ; } let n = 10 ; document . write ( sumOfSeries ( n ) ) ;
Find sum of even index binomial coefficients | Return the sum of even index term ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; finding sum of even index term . ; Driver Program
function evenSum ( n ) { var C = Array . from ( Array ( n + 1 ) , ( ) => Array ( n + 1 ) . fill ( 0 ) ) ; var i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , n ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } var sum = 0 ; for ( var i = 0 ; i <= n ; i += 2 ) sum += C [ n ] [ i ] ; return sum ; } var n = 4 ; document . write ( evenSum ( n ) ) ;
Program to print triangular number series till n | Function to find triangular number ; Driven Function
function triangular_series ( n ) { for ( let i = 1 ; i <= n ; i ++ ) document . write ( " " + i * ( i + 1 ) / 2 ) ; } let n = 5 ; triangular_series ( n ) ;
Check if a number can be written as sum of three consecutive integers | function to check if a number can be written as sum of three consecutive integers . ; if n is multiple of 3 ; else prvar " - 1" . ; Driver program to test above function
function checksum ( n ) { if ( n % 3 == 0 ) document . write ( n / 3 - 1 + " " + n / 3 + " " + ( n / 3 + 1 ) ) ; else document . write ( " " ) ; } var n = 6 ; checksum ( n ) ;
Sum of all divisors from 1 to n | Utility function to find sum of all divisor of number up to ' n ' ; Driver code
function divisorSum ( n ) { let sum = 0 ; for ( let i = 1 ; i <= n ; ++ i ) sum += Math . floor ( n / i ) * i ; return sum ; } let n = 4 ; document . write ( divisorSum ( n ) + " " ) ; n = 5 ; document . write ( divisorSum ( n ) + " " ) ;
Sum of all divisors from 1 to n |  ; var t1 = i * ( num / i - i + 1 ) ; adding i every time it appears with numbers greater than or equal to itself var t2 = ( ( ( num / i ) * ( num / i + 1 ) ) / 2 ) - ( ( i * ( i + 1 ) ) / 2 ) ; adding numbers that appear with i and are greater than i ; Driver code
function sum_all_divisors ( num ) { var sum = 0 ; for ( var i = 1 ; i <= Math . sqrt ( num ) ; i ++ ) { sum += t1 + t2 ; } return sum ; } var n ; var sum = sum_all_divisors ( n ) ; document . write ( sum ) ;
N | function to evaluate Nth polite number ; Driver code
function polite ( n ) { n += 1 ; let base = 2 ; return n + ( Math . log ( ( n + ( Math . log ( n ) / Math . log ( base ) ) ) ) ) / Math . log ( base ) ; } n = 7 ; document . write ( parseInt ( polite ( n ) ) ) ;
Find the number of stair steps | Modified Binary search function to solve the equation ; If mid is solution to equation ; If our solution to equation lies between mid and mid - 1 ; If solution to equation is greater than mid ; If solution to equation is less than mid ; Driver code ; Call binary search method to solve for limits 1 to T ; Because our pattern starts from 2 , 3 , 4 , 5. . . so , we subtract 1 from ans
function solve ( low , high , T ) { while ( low <= high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( ( mid * ( mid + 1 ) ) == T ) return mid ; if ( mid > 0 && ( mid * ( mid + 1 ) ) > T && ( mid * ( mid - 1 ) ) <= T ) return mid - 1 ; if ( ( mid * ( mid + 1 ) ) > T ) high = mid - 1 ; else low = mid + 1 ; } return - 1 ; } let T = 15 ; let ans = solve ( 1 , T , 2 * T ) ; if ( ans != - 1 ) ans -- ; document . write ( " " + ans ) ;
Check for integer overflow on multiplication | Function to check whether there is overflow in a * b or not . It returns true if there is overflow . ; Check if either of them is zero ; Driver code
function isOverflow ( a , b ) { if ( a == 0 b == 0 ) return false ; var result = a * b ; if ( result >= 9223372036854775807 result <= - 9223372036854775808 ) result = 0 if ( a == parseInt ( result / b ) ) return false ; else return true ; } var a = 10000000000 , b = - 10000000000 ; if ( isOverflow ( a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of first n odd numbers in O ( 1 ) Complexity | Returns the sum of first n odd numbers ; Driver Code
function oddSum ( n ) { let sum = 0 ; curr = 1 ; for ( let i = 0 ; i < n ; i ++ ) { sum += curr ; curr += 2 ; } return sum ; } let n = 20 ; document . write ( " " + n + " " + oddSum ( n ) ) ;
Sum of first n odd numbers in O ( 1 ) Complexity | Returns the sum of first n odd numbers ; Driver code
function oddSum ( n ) { return ( n * n ) ; } let n = 20 ; document . write ( " " + n + " " + oddSum ( n ) ) ;
K | Returns the sum of first n odd numbers ; Count prime factors of all numbers till B . ; Print let all numbers with k prime factors ; Driver code
function prletKPFNums ( A , B , K ) { let prime = [ ] ; for ( let i = 0 ; i < B + 1 ; i ++ ) prime [ i ] = true ; let p_factors = [ ] ; for ( let i = 0 ; i < B + 1 ; i ++ ) p_factors [ i ] = 0 ; for ( let p = 2 ; p <= B ; p ++ ) if ( p_factors [ p ] == 0 ) for ( let i = p ; i <= B ; i += p ) p_factors [ i ] ++ ; for ( let i = A ; i <= B ; i ++ ) if ( p_factors [ i ] == K ) document . write ( i + " " ) ; } let A = 14 , B = 18 , K = 2 ; prletKPFNums ( A , B , K ) ;
Queries for maximum difference between prime numbers in given ranges | JavaScript program to find maximum differences between two prime numbers in given ranges ; Precompute Sieve , Prefix array , Suffix array ; Sieve of Eratosthenes ; Precomputing Prefix array . ; Precompute Suffix array . ; Function to solve each query ; Driver Code
let MAX = 100005 ; function precompute ( prefix , suffix ) { let prime = [ ] ; for ( let i = 0 ; i < MAX ; i ++ ) { prime [ i ] = true ; } for ( let i = 2 ; i * i < MAX ; i ++ ) { if ( prime [ i ] ) { for ( let j = i * i ; j < MAX ; j += i ) { prime [ j ] = false ; } } } prefix [ 1 ] = 1 ; suffix [ MAX - 1 ] = 1e9 + 7 ; for ( let i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] ) { prefix [ i ] = i ; } else { prefix [ i ] = prefix [ i - 1 ] ; } } for ( let i = MAX - 2 ; i > 1 ; i -- ) { if ( prime [ i ] ) { suffix [ i ] = i ; } else { suffix [ i ] = suffix [ i + 1 ] ; } } } function query ( prefix , suffix , L , R ) { if ( prefix [ R ] < L suffix [ L ] > R ) { return 0 ; } else { return prefix [ R ] - suffix [ L ] ; } } let q = 3 ; let L = [ 2 , 2 , 24 ] ; let R = [ 5 , 2 , 28 ] ; let prefix = [ ] , suffix = [ ] ; precompute ( prefix , suffix ) ; for ( let i = 0 ; i < q ; i ++ ) { document . write ( query ( prefix , suffix , L [ i ] , R [ i ] ) + " " ) ; }
Sum of the Series 1 + x / 1 + x ^ 2 / 2 + x ^ 3 / 3 + . . + x ^ n / n | Function to print the sum of the series ; Driver code
function sum ( x , n ) { let i , total = 1.0 ; for ( i = 1 ; i <= n ; i ++ ) total = total + ( Math . pow ( x , i ) / i ) ; return total ; } let g ; let x = 2 ; let n = 5 ; document . write ( sum ( x , n ) . toFixed ( 2 ) ) ;
Find if a number is part of AP whose first element and difference are given | returns yes if exist else no . ; If difference is 0 , then x must be same as a . ; Else difference between x and a must be divisible by d . ; Driver code
function isMember ( a , d , x ) { if ( d == 0 ) return ( x == a ) ; return ( ( x - a ) % d == 0 && ( x - a ) / d >= 0 ) ; } let a = 1 , x = 7 , d = 3 ; if ( isMember ( a , d , x ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check whether a number can be represented by sum of two squares | function to check if there exist two numbers sum of whose squares is n . ; driver Program
function sumSquare ( n ) { for ( i = 1 ; i * i <= n ; i ++ ) for ( j = 1 ; j * j <= n ; j ++ ) if ( i * i + j * j == n ) { document . write ( i + " " + j + " " + " " ) ; return true ; } return false ; } var n = 25 ; if ( sumSquare ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Queries to count minimum flips required to fill a binary submatrix with 0 s only | javascript program of the above approach ; Function to compute the matrix prefixCnt [ M ] [ N ] from mat [ M ] [ N ] such that prefixCnt [ i ] [ j ] stores the count of 0 's from (0, 0) to (i, j) ; Initialize prefixCnt [ i ] [ j ] with 1 if mat [ i ] [ j ] is 0 ; Otherwise , assign with 0 ; Calculate prefix sum for each row ; Calculate prefix sum for each column ; Function to compute count of 0 's in submatrix from (pi, pj) to (qi, qj) from prefixCnt[M][N] ; Initialize that count of 0 's  in the sub-matrix within  indices (0, 0) to (qi, qj) ; Subtract count of 0 's within  indices (0, 0) and (pi-1, qj) ; Subtract count of 0 's within  indices (0, 0) and (qi, pj-1) ; Add prefixCnt [ pi - 1 ] [ pj - 1 ] because its value has been added ; once but subtracted twice ; Function to count the 0 s in the each given submatrix ; Stores the prefix sum of each row and column ; Compute matrix prefixCnt [ ] [ ] ; Function Call for each query ; Given matrix ; Function Call
let M = 6 ; let N = 7 ; function preCompute ( mat , prefixCnt ) { for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { if ( mat [ i ] [ j ] == 0 ) { prefixCnt [ i ] [ j ] = 1 ; } else { prefixCnt [ i ] [ j ] = 0 ; } } } for ( let i = 0 ; i < M ; i ++ ) for ( let j = 1 ; j < N ; j ++ ) prefixCnt [ i ] [ j ] += prefixCnt [ i ] [ j - 1 ] ; for ( let i = 1 ; i < M ; i ++ ) for ( let j = 0 ; j < N ; j ++ ) prefixCnt [ i ] [ j ] += prefixCnt [ i - 1 ] [ j ] ; } function countQuery ( prefixCnt , pi , pj , qi , qj ) { let cnt = prefixCnt [ qi ] [ qj ] ; if ( pi > 0 ) cnt -= prefixCnt [ pi - 1 ] [ qj ] ; if ( pj > 0 ) cnt -= prefixCnt [ qi ] [ pj - 1 ] ; if ( pi > 0 && pj > 0 ) cnt += prefixCnt [ pi - 1 ] [ pj - 1 ] ; return cnt ; } function count0s ( mat , Q , sizeQ ) { let prefixCnt = new Array ( M ) ; for ( var i = 0 ; i < prefixCnt . length ; i ++ ) { prefixCnt [ i ] = new Array ( 2 ) ; } preCompute ( mat , prefixCnt ) ; for ( let i = 0 ; i < sizeQ ; i ++ ) { document . write ( countQuery ( prefixCnt , Q [ i ] [ 0 ] , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , Q [ i ] [ 3 ] ) + " " ) ; } } let mat = [ [ 0 , 1 , 0 , 1 , 1 , 1 , 0 ] , [ 1 , 0 , 1 , 1 , 1 , 0 , 1 ] , [ 1 , 1 , 0 , 0 , 1 , 1 , 0 ] , [ 1 , 1 , 1 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 1 , 0 , 1 , 1 , 1 ] , [ 1 , 1 , 0 , 1 , 1 , 0 , 1 ] ] ; let Q = [ [ 0 , 1 , 3 , 2 ] , [ 2 , 2 , 4 , 5 ] , [ 4 , 3 , 5 , 6 ] ] ; let sizeQ = Q . length ; count0s ( mat , Q , sizeQ ) ;
Smallest root of the equation x ^ 2 + s ( x ) * x | Function to check if the sum of digits is equal to the summation assumed ; Calculate the sum of digit ; Function to find the largest root possible . ; Iterate for all possible sum of digits . ; Check if discriminent is a perfect square . ; Check if discriminent is a perfect square and if it as perefect root of the equation ; Function returns answer ; Driver code
function check ( a , b ) { var c = 0 ; while ( a != 0 ) { c = c + a % 10 ; a = parseInt ( a / 10 ) ; } return ( c == b ) ; } function root ( n ) { var found = false ; var mx = 1E18 ; for ( i = 0 ; i <= 90 ; i ++ ) { var s = i * i + 4 * n ; var sq = Math . sqrt ( s ) ; if ( sq * sq == s && check ( ( sq - i ) / 2 , i ) ) { found = true ; mx = Math . min ( mx , ( sq - i ) / 2 ) ; } } if ( found ) return mx ; else return - 1 ; } var n = 110 ; document . write ( root ( n ) ) ;
Sum of digits of a given number to a given power | Function to calculate sum ; Driver Code
function calculate ( n , power ) { sum = 0 ; bp = Math . pow ( n , power ) ; while ( bp != 0 ) { d = bp % 10 ; sum = sum + d ; bp = Math . floor ( bp / 10 ) ; } return sum ; } n = 5 ; power = 4 ; document . write ( calculate ( n , power ) ) ;
Co | Utility function ; function to check if pair is co - prime or not ; function to find and prlet co - prime pair ; Driver code
function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function coprime ( a , b ) { return ( __gcd ( a , b ) == 1 ) ; } function pairSum ( n ) { let mid = Math . floor ( n / 2 ) ; for ( let i = mid ; i >= 1 ; i -- ) { if ( coprime ( i , n - i ) == true ) { document . write ( i + " " + ( n - i ) ) ; break ; } } } let n = 11 ; pairSum ( n ) ;
Program for quotient and remainder of big number | Function to calculate the modulus ; Store the modulus of big number ; Do step by step division ; Update modulo by concatenating current digit . ; Update quotient ; Update mod for next iteration . ; Flag used to remove starting zeros ; Driver Code
function modBigNumber ( num , m ) { let vec = [ ] ; let x = 0 ; let mod = 0 ; for ( let i = 0 ; i < num . length ; i ++ ) { digit = num [ i ] - ' ' ; mod = mod * 10 + digit ; quo = parseInt ( mod / m ) ; vec [ x ++ ] = quo ; mod = mod % m ; } document . write ( " " + mod + " " ) ; document . write ( " " ) ; let zeroflag = 0 ; for ( let i = 0 ; i < x ; i ++ ) { if ( vec [ i ] == 0 && zeroflag == 0 ) continue ; zeroflag = 1 ; document . write ( vec [ i ] ) ; } return ; } let num = " " ; let m = 487 ; modBigNumber ( num , m ) ;
Queries to find whether a number has exactly four distinct factors or not | Initialize global variable according to given condition so that it can be accessible to all function ; Function to calculate all number having four distinct distinct factors ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Initialize prime [ ] array which will contains all the primes from 1 - N ; Iterate over all the prime numbers ; Mark cube root of prime numbers ; Mark product of prime numbers ; Driver Code
var N = 1000000 ; var fourDiv = Array ( N + 1 ) . fill ( false ) ; function fourDistinctFactors ( ) { var primeAll = Array ( N + 1 ) . fill ( false ) ; for ( var p = 2 ; p * p <= N ; p ++ ) { if ( primeAll [ p ] == false ) { for ( var i = p * 2 ; i <= N ; i += p ) primeAll [ i ] = true ; } } var prime = [ ] ; for ( var p = 2 ; p <= N ; p ++ ) if ( ! primeAll [ p ] ) prime . push ( p ) ; for ( var i = 0 ; i < prime . length ; ++ i ) { var p = prime [ i ] ; if ( p * p * p <= N ) fourDiv [ p * p * p ] = true ; for ( var j = i + 1 ; j < prime . length ; ++ j ) { var q = prime [ j ] ; if ( p * q > N ) break ; fourDiv [ p * q ] = true ; } } } fourDistinctFactors ( ) ; var num = 10 ; if ( fourDiv [ num ] ) document . write ( " " ) ; else document . write ( " " ) ; num = 12 ; if ( fourDiv [ num ] ) document . write ( " " ) ; else document . write ( " " ) ;
Cholesky Decomposition : Matrix Decomposition | javascript program to decompose a matrix using Cholesky Decomposition function MAX = 100 ; ; Decomposing a matrix into Lower Triangular ; summation for diagonals ; Evaluating L ( i , j ) using L ( j , j ) ; Displaying Lower Triangular and its Transpose ; Lower Triangular ; Transpose of Lower Triangular ; Driver Code
function Cholesky_Decomposition ( matrix , n ) { var lower = Array ( n ) . fill ( 0 ) . map ( x => Array ( n ) . fill ( 0 ) ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j <= i ; j ++ ) { var sum = 0 ; if ( j == i ) { for ( var k = 0 ; k < j ; k ++ ) sum += parseInt ( Math . pow ( lower [ j ] [ k ] , 2 ) ) ; lower [ j ] [ j ] = parseInt ( Math . sqrt ( matrix [ j ] [ j ] - sum ) ) ; } else { for ( var k = 0 ; k < j ; k ++ ) sum += ( lower [ i ] [ k ] * lower [ j ] [ k ] ) ; lower [ i ] [ j ] = ( matrix [ i ] [ j ] - sum ) / lower [ j ] [ j ] ; } } } document . write ( " " ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < n ; j ++ ) document . write ( lower [ i ] [ j ] + " " ) ; for ( var j = 0 ; j < n ; j ++ ) document . write ( lower [ j ] [ i ] + " " ) ; document . write ( ' ' ) ; } } var n = 3 ; var matrix = [ [ 4 , 12 , - 16 ] , [ 12 , 37 , - 43 ] , [ - 16 , - 43 , 98 ] ] ; Cholesky_Decomposition ( matrix , n ) ;
Program for sum of arithmetic series | Efficient solution to find sum of arithmetic series . ; Driver code
function sumOfAP ( a , d , n ) { let sum = ( n / 2 ) * ( 2 * a + ( n - 1 ) * d ) ; return sum ; } let n = 20 ; let a = 2.5 , d = 1.5 ; document . write ( sumOfAP ( a , d , n ) ) ;
Program for cube sum of first n natural numbers | Returns the sum of series ; Driven Program
function sumOfSeries ( n ) { x = 0 if ( n % 2 == 0 ) x = ( n / 2 ) * ( n + 1 ) else x = ( ( n + 1 ) / 2 ) * n return ( x * x ) } let n = 5 ; document . write ( sumOfSeries ( n ) ) ;
Maximum value of | arr [ i ] | Javascript program to find maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Return maximum value of | arr [ i ] - arr [ j ] | + | i - j | ; Iterating two for loop , one for i and another for j . ; Evaluating | arr [ i ] - arr [ j ] | + | i - j | and compare with previous maximum . ; Driver code
var MAX = 10 ; function findValue ( arr , n ) { var ans = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = 0 ; j < n ; j ++ ) ans = Math . max ( ans , Math . abs ( arr [ i ] - arr [ j ] ) + Math . abs ( i - j ) ) ; return ans ; } var arr = [ 1 , 2 , 3 , 1 ] ; var n = arr . length ; document . write ( findValue ( arr , n ) ) ;
Maximum value of | arr [ i ] | Return maximum | arr [ i ] - arr [ j ] | + | i - j | ; Calculating first_array and second_array ; Finding maximum and minimum value in first_array ; Storing the difference between maximum and minimum value in first_array ; Finding maximum and minimum value in second_array ; Storing the difference between maximum and minimum value in second_array ; Driver Code
function findValue ( arr , n ) { let a = new Array ( n ) ; let b = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { a [ i ] = ( arr [ i ] + i ) ; b [ i ] = ( arr [ i ] - i ) ; } let x = a [ 0 ] , y = a [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) x = a [ i ] ; if ( a [ i ] < y ) y = a [ i ] ; } let ans1 = ( x - y ) ; x = b [ 0 ] ; y = b [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( b [ i ] > x ) x = b [ i ] ; if ( b [ i ] < y ) y = b [ i ] ; } let ans2 = ( x - y ) ; return Math . max ( ans1 , ans2 ) ; } let arr = [ 1 , 2 , 3 , 1 ] ; let n = arr . length ; document . write ( findValue ( arr , n ) ) ;
Number of subarrays having product less than K | javascript program to count subarrays having product less than k . ; Counter for single element ; Multiple subarray ; If this multiple is less than k , then increment ; Driver Code
function countsubarray ( array , n , k ) { var count = 0 ; var i , j , mul ; for ( i = 0 ; i < n ; i ++ ) { if ( array [ i ] < k ) count ++ ; mul = array [ i ] ; for ( j = i + 1 ; j < n ; j ++ ) { mul = mul * array [ j ] ; if ( mul < k ) count ++ ; else break ; } } return count ; } var array = [ 1 , 2 , 3 , 4 ] ; var k = 10 ; var size = array . length ; var count = countsubarray ( array , size , k ) ; document . write ( count ) ;
Perfect Cube factors of a Number | Function that returns the count of factors that are perfect cube ; To store the count of number of times a prime number divides N . ; To store the number of factors that are perfect cube ; Count number of 2 's that divides N ; Calculate ans according to above formula ; Check for all the possible numbers that can divide it ; Loop to check the number of times prime number i divides it ; Calculate ans according to above formula ; Return final count ; Given number ; Function Call
function noOfFactors ( N ) { if ( N == 1 ) return 1 ; let count = 0 ; let ans = 1 ; while ( N % 2 == 0 ) { count ++ ; N = parseInt ( N / 2 ) ; } ans *= ( parseInt ( count / 3 ) + 1 ) ; for ( let i = 3 ; i * i <= N ; i = i + 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = parseInt ( N / i ) ; } ans *= ( parseInt ( count / 3 ) + 1 ) ; } return ans ; } let N = 216 ; document . write ( noOfFactors ( N ) ) ;