codeparrot/xlcost-text-to-code huggingface dataset:
Sophie Germain Prime | function to detect prime number here we have used sieve method https : www . geeksforgeeks . org / sieve - of - eratosthenes / to detect prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; We have made array till 2 * n + 1 so that we can check prime number till that and conclude about sophie german prime . ; checking every i whether it is sophie german prime or not . ; Driver code
function sieve ( n , prime ) { for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } } function printSophieGermanNumber ( n ) { let prime = new Array ( ) ; for ( let i = 0 ; i < ( 2 * n + 1 ) ; i ++ ) prime [ i ] = true ; sieve ( 2 * n + 1 , prime ) ; for ( let i = 2 ; i <= n ; ++ i ) { if ( prime [ i ] && prime [ 2 * i + 1 ] ) document . write ( i + " " ) ; } } let n = 25 ; printSophieGermanNumber ( n ) ;
Bessel 's Interpolation | Calculating u mentioned in the formula ; Calculating factorial of given number n ; Number of values given ; y is used for difference table with y [ 0 ] used for input ; Calculating the central difference table ; Displaying the central difference table ; Value to interpolate at ; Initializing u and sum ; k is origin thats is f ( 0 ) ; Origin for odd ; Origin for even ; Solving using bessel 's formula
function ucal ( u , n ) { if ( n == 0 ) return 1 ; var temp = u ; for ( var i = 1 ; i <= n / 2 ; i ++ ) temp = temp * ( u - i ) ; for ( var i = 1 ; i < n / 2 ; i ++ ) temp = temp * ( u + i ) ; return temp ; } function fact ( n ) { var f = 1 ; for ( var i = 2 ; i <= n ; i ++ ) f *= i ; return f ; } var n = 6 ; var x = [ 25 , 26 , 27 , 28 , 29 , 30 ] ; var y = Array ( n ) . fill ( 0.0 ) . map ( x => Array ( n ) . fill ( 0.0 ) ) ; ; y [ 0 ] [ 0 ] = 4.000 ; y [ 1 ] [ 0 ] = 3.846 ; y [ 2 ] [ 0 ] = 3.704 ; y [ 3 ] [ 0 ] = 3.571 ; y [ 4 ] [ 0 ] = 3.448 ; y [ 5 ] [ 0 ] = 3.333 ; for ( var i = 1 ; i < n ; i ++ ) for ( var j = 0 ; j < n - i ; j ++ ) y [ j ] [ i ] = y [ j + 1 ] [ i - 1 ] - y [ j ] [ i - 1 ] ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < n - i ; j ++ ) document . write ( y [ i ] [ j ] . toFixed ( 6 ) + " " ) ; document . write ( ' ' ) ; } var value = 27.4 ; var sum = ( y [ 2 ] [ 0 ] + y [ 3 ] [ 0 ] ) / 2 ; var k ; if ( ( n % 2 ) > 0 ) k = n / 2 ; else k = n / 2 - 1 ; var u = ( value - x [ k ] ) / ( x [ 1 ] - x [ 0 ] ) ; for ( var i = 1 ; i < n ; i ++ ) { if ( ( i % 2 ) > 0 ) sum = sum + ( ( u - 0.5 ) * ucal ( u , i - 1 ) * y [ k ] [ i ] ) / fact ( i ) ; else sum = sum + ( ucal ( u , i ) * ( y [ k ] [ i ] + y [ -- k ] [ i ] ) / ( fact ( i ) * 2 ) ) ; } document . write ( " " + value . toFixed ( 6 ) + " " + sum . toFixed ( 6 ) ) ;
An efficient way to check whether n | A simple Javascript program to check if n - th Fibonacci number is multiple of 10. ; Returns true if n - th Fibonacci number is multiple of 10. ; Driver code
function fibonacci ( n ) { let a = 0 ; let b = 1 ; let c ; if ( n <= 1 ) return n ; for ( let i = 2 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return c ; } function isMultipleOf10 ( n ) { let f = fibonacci ( 30 ) ; return ( f % 10 == 0 ) ; } let n = 30 ; if ( isMultipleOf10 ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to find whether a given number is power of 2 | Function which checks whether a number is a power of 2 ; base cases '1' is the only odd number which is a power of 2 ( 2 ^ 0 ) ; all other odd numbers are not powers of 2 ; recursive function call ; True ; False
function powerOf2 ( n ) { if ( n == 1 ) return true ; else if ( n % 2 != 0 n == 0 ) return false ; return powerOf2 ( n / 2 ) ; } var n = 64 ; var m = 12 ; if ( powerOf2 ( n ) == true ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; if ( powerOf2 ( m ) == true ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Program to find whether a given number is power of 2 | Method to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Driver method
function isPowerOfTwo ( x ) { return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ; } document . write ( isPowerOfTwo ( 31 ) ? " " : " " ) ; document . write ( " " + ( isPowerOfTwo ( 64 ) ? " " : " " ) ) ;
Program to find whether a given number is power of 2 | Function to check if x is power of 2 ; Driver code
function isPowerofTwo ( n ) { if ( n == 0 ) return false ; if ( ( n & ( ~ ( n - 1 ) ) ) == n ) return true ; return false ; } if ( isPowerofTwo ( 30 ) == true ) document . write ( " " ) ; else document . write ( " " ) ; if ( isPowerofTwo ( 128 ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Smallest power of 2 which is greater than or equal to sum of array elements | Function to find the nearest power of 2 ; The number ; If already a power of 2 ; Find the next power of 2 ; Function to find the memory used ; Sum of array ; Traverse and find the sum of array ; Function call to find the nearest power of 2 ; Driver Code
function nextPowerOf2 ( n ) { let p = 1 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( p < n ) p <<= 1 ; return p ; } function memoryUsed ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; let nearest = nextPowerOf2 ( sum ) ; return nearest ; } let arr = [ 1 , 2 , 3 , 2 ] ; let n = arr . length ; document . write ( memoryUsed ( arr , n ) ) ;
Toggling k | Javascript program to toogle k - th bit of a number ; Driver code
function toggleKthBit ( n , k ) { return ( n ^ ( 1 << ( k - 1 ) ) ) ; } let n = 5 , k = 1 ; document . write ( toggleKthBit ( n , k ) ) ;
Smallest power of 2 greater than or equal to n | JavaScript program to find smallest power of 2 greater than or equal to n ; First n in the below condition is for the case where n is 0 ; Driver Code
function nextPowerOf2 ( n ) { var count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( n != 0 ) { n >>= 1 ; count += 1 ; } return 1 << count ; } var n = 0 ; document . write ( nextPowerOf2 ( n ) ) ;
Add elements in start to sort the array | Variation of Stalin Sort | Function to sort the array ; Iterator < Integer > index = arr . iterator ( ) ; ; Driver Code ; Function call
function variationStalinsort ( arr ) { let j = 0 ; while ( true ) { let moved = 0 ; for ( let i = 0 ; i < ( arr . length - 1 - j ) ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { let index ; let temp ; index = arr [ i ] ; temp = arr [ i + 1 ] ; arr . splice ( i , 1 ) ; arr . splice ( i , 0 , temp ) ; arr [ i ] = temp ; arr . splice ( i + 1 , 1 ) ; arr . splice ( i + 1 , 0 , index ) arr [ i + 1 ] = index ; moved ++ ; } } j ++ ; if ( moved == 0 ) { break ; } } document . write ( " " + arr + " " ) ; } let arr = [ 2 , 1 , 4 , 3 , 6 , 5 , 8 , 7 , 10 , 9 ] ; let arr1 = [ ] ; for ( let i = 0 ; i < arr . length ; i ++ ) arr1 . push ( arr [ i ] ) ; variationStalinsort ( arr1 ) ;
Sort an Array which contain 1 to N values in O ( N ) using Cycle Sort | Function to print array element ; Traverse the array ; Function to sort the array in O ( N ) ; Traverse the array ; If the current element is at correct position ; Else swap the current element with it 's correct position ; Swap the value of arr [ i ] and arr [ arr [ i ] - 1 ] ; Driver Code ; Function call to sort the array ; Function call to print the array
function printArray ( arr , N ) { for ( var i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + ' ' ) ; } } function sortArray ( arr , N ) { for ( var i = 0 ; i < N ; ) { if ( arr [ i ] == i + 1 ) { i ++ ; } else { var temp1 = arr [ i ] ; var temp2 = arr [ arr [ i ] - 1 ] ; arr [ i ] = temp2 ; arr [ temp1 - 1 ] = temp1 ; } } } var arr = [ 2 , 1 , 5 , 3 , 4 ] ; var N = arr . length ; sortArray ( arr , N ) ; printArray ( arr , N ) ;
Maximum sum of values of N items in 0 | Function to find the maximum value ; base condition ; K elements already reduced to half of their weight ; Dont include item ; If weight of the item is less than or equal to the remaining weight then include the item ; Return the maximum of both cases ; If the weight reduction to half is possible ; Skip the item ; Include item with full weight if weight of the item is less than the remaining weight ; Include item with half weight if half weight of the item is less than the remaining weight ; Return the maximum of all 3 cases ; Driver code
function maximum ( value , weight , weight1 , flag , K , index ) { if ( index >= value . length ) { return 0 ; } if ( flag == K ) { var skip = maximum ( value , weight , weight1 , flag , K , index + 1 ) ; var full = 0 ; if ( weight [ index ] <= weight1 ) { full = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] , flag , K , index + 1 ) ; } return Math . max ( full , skip ) ; } else { var skip = maximum ( value , weight , weight1 , flag , K , index + 1 ) ; var full = 0 ; var half = 0 ; if ( weight [ index ] <= weight1 ) { full = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] , flag , K , index + 1 ) ; } if ( weight [ index ] / 2 <= weight1 ) { half = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] / 2 , flag , K , index + 1 ) ; } return Math . max ( full , Math . max ( skip , half ) ) ; } } var value = [ 17 , 20 , 10 , 15 ] ; var weight = [ 4 , 2 , 7 , 5 ] ; var K = 1 ; var W = 4 ; document . write ( maximum ( value , weight , W , 0 , K , 0 ) ) ;
d | JavaScript program to find the size of the minimum dominating set of the tree ; Definition of a tree node ; Helper function that allocates a new node ; DP array to precompute and store the results ; minDominatingSettion to return the size of the minimum dominating set of the array ; Base case ; Setting the compulsory value if needed ; Check if the answer is already computed ; If it is compulsory to select the node ; Choose the node and set its children as covered ; If it is covered ; If the current node is neither covered nor needs to be selected compulsorily ; Store the result ; Initialising the DP array ; Constructing the tree
let N = 1005 ; class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function newNode ( data ) { let node = new Node ( data ) ; return node ; } let dp = new Array ( N ) ; function minDominatingSet ( root , covered , compulsory ) { if ( root == null ) return 0 ; if ( root . left != null && root . right != null && covered > 0 ) compulsory = 1 ; if ( dp [ root . data ] [ covered ] [ compulsory ] != - 1 ) return dp [ root . data ] [ covered ] [ compulsory ] ; if ( compulsory > 0 ) { return dp [ root . data ] [ covered ] [ compulsory ] = 1 + minDominatingSet ( root . left , 1 , 0 ) + minDominatingSet ( root . right , 1 , 0 ) ; } if ( covered > 0 ) { return dp [ root . data ] [ covered ] [ compulsory ] = Math . min ( 1 + minDominatingSet ( root . left , 1 , 0 ) + minDominatingSet ( root . right , 1 , 0 ) , minDominatingSet ( root . left , 0 , 0 ) + minDominatingSet ( root . right , 0 , 0 ) ) ; } let ans = 1 + minDominatingSet ( root . left , 1 , 0 ) + minDominatingSet ( root . right , 1 , 0 ) ; if ( root . left != null ) { ans = Math . min ( ans , minDominatingSet ( root . left , 0 , 1 ) + minDominatingSet ( root . right , 0 , 0 ) ) ; } if ( root . right != null ) { ans = Math . min ( ans , minDominatingSet ( root . left , 0 , 0 ) + minDominatingSet ( root . right , 0 , 1 ) ) ; } dp [ root . data ] [ covered ] [ compulsory ] = ans ; return dp [ root . data ] [ covered ] [ compulsory ] ; } for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = new Array ( 5 ) ; for ( let j = 0 ; j < 5 ; j ++ ) { dp [ i ] [ j ] = new Array ( 5 ) ; for ( let l = 0 ; l < 5 ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } let root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 4 ) ; root . left . left . left = newNode ( 5 ) ; root . left . left . left . left = newNode ( 6 ) ; root . left . left . left . right = newNode ( 7 ) ; root . left . left . left . right . right = newNode ( 10 ) ; root . left . left . left . left . left = newNode ( 8 ) ; root . left . left . left . left . right = newNode ( 9 ) ; document . write ( minDominatingSet ( root , 0 , 0 ) ) ;
Number of subsets with zero sum |  ; variable to store states of dp ; To find the number of subsets with sum equal to 0 Since S can be negative , we will maxSum to it to make it positive ; Base cases ; Returns the value if a state is already solved ; If the state is not visited , then continue ; Recurrence relation ; Returning the value ; Driver function
var maxSum = 100 var arrSize = 51 var dp = Array . from ( Array ( arrSize ) , ( ) => Array ( maxSum ) ) ; var visit = Array . from ( Array ( arrSize ) , ( ) => Array ( maxSum ) ) ; function SubsetCnt ( i , s , arr , n ) { if ( i == n ) { if ( s == 0 ) return 1 ; else return 0 ; } if ( visit [ i ] [ s + maxSum ] ) return dp [ i ] [ s + maxSum ] ; visit [ i ] [ s + maxSum ] = 1 ; dp [ i ] [ s + maxSum ] = SubsetCnt ( i + 1 , s + arr [ i ] , arr , n ) + SubsetCnt ( i + 1 , s , arr , n ) ; return dp [ i ] [ s + maxSum ] ; } var arr = [ 2 , 2 , 2 , - 4 , - 4 ] ; var n = arr . length ; document . write ( SubsetCnt ( 0 , 0 , arr , n ) ) ;
Number of Binary Strings of length N with K adjacent Set Bits | JavaScript program to find the number of Bit Strings of length N with K adjacent set bits ; Function to find the number of Bit Strings of length N with K adjacent set bits ; Base Case when we form bit string of length n ; if f ( bit string ) = k , count this way ; Check if the last bit was set , if it was set then call for next index by incrementing the adjacent bit count else just call the next index with same value of adjacent bit count and either set the bit at current index or let it remain unset ; set the bit at currentIndex ; unset the bit at currentIndex ; Driver Code ; dp [ i ] [ j ] [ k ] represents bit strings of length i with f ( bit string ) = j and last bit as k ; initializes the dp ; total ways = ( ways by placing 1 st bit as 1 + ways by placing 1 st bit as 0 )
var MAX = 1000 ; function waysToKAdjacentSetBits ( dp , n , k , currentIndex , adjacentSetBits , lastBit ) { if ( currentIndex == n ) { if ( adjacentSetBits == k ) return 1 ; return 0 ; } if ( dp [ currentIndex ] [ adjacentSetBits ] [ lastBit ] != - 1 ) { return dp [ currentIndex ] [ adjacentSetBits ] [ lastBit ] ; } var noOfWays = 0 ; if ( lastBit == 1 ) { noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits + 1 , 1 ) ; noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits , 0 ) ; } else if ( ! lastBit ) { noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits , 1 ) ; noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits , 0 ) ; } dp [ currentIndex ] [ adjacentSetBits ] [ lastBit ] = noOfWays ; return noOfWays ; } var n = 5 , k = 2 ; var dp = Array . from ( Array ( MAX ) , ( ) => Array ( MAX ) ) ; for ( var i = 0 ; i < MAX ; i ++ ) for ( var j = 0 ; j < MAX ; j ++ ) dp [ i ] [ j ] = new Array ( 2 ) . fill ( - 1 ) ; var totalWays = waysToKAdjacentSetBits ( dp , n , k , 1 , 0 , 1 ) + waysToKAdjacentSetBits ( dp , n , k , 1 , 0 , 0 ) ; document . write ( " " + totalWays + " " ) ;
Tetranacci Numbers | Function to print the N - th tetranacci number ; Initialize first four numbers to base cases ; declare a current variable ; Loop to add previous four numbers for each number starting from 4 and then assign first , second , third to second , third , fourth and curr to fourth respectively ; Driver code
function printTetra ( n ) { if ( n < 0 ) return ; var first = 0 , second = 1 ; var third = 1 , fourth = 2 ; var curr ; if ( n == 0 ) cout << first ; else if ( n == 1 n == 2 ) cout << second ; else if ( n == 3 ) cout << fourth ; else { for ( var i = 4 ; i <= n ; i ++ ) { curr = first + second + third + fourth ; first = second ; second = third ; third = fourth ; fourth = curr ; } document . write ( curr ) ; } } var n = 10 ; printTetra ( n ) ;
Count ways to reach the nth stair using step 1 , 2 or 3 | A recursive function used by countWays ; Driver Code
function countWays ( n ) { let res = new Array ( n + 2 ) ; res [ 0 ] = 1 ; res [ 1 ] = 1 ; res [ 2 ] = 2 ; for ( let i = 3 ; i <= n ; i ++ ) res [ i ] = res [ i - 1 ] + res [ i - 2 ] + res [ i - 3 ] ; return res [ n ] ; } let n = 4 ; document . write ( countWays ( n ) ) ;
Count ways to reach the nth stair using step 1 , 2 or 3 | A recursive function used by countWays ; Declaring three variables and holding the ways for first three stairs ; Fourth variable ; Starting from 4 as already counted for 3 stairs ; Driver code
function countWays ( n ) { var a = 1 , b = 2 , c = 4 ; var d = 0 ; if ( n == 0 n == 1 n == 2 ) return n ; if ( n == 3 ) return c ; for ( var i = 4 ; i <= n ; i ++ ) { d = c + b + a ; a = b ; b = c ; c = d ; } return d ; } var n = 4 ; document . write ( countWays ( n ) ) ;
Subset Sum Problem in O ( sum ) space |  ; initializing with 1 as sum 0 is always possible ; loop to go through every element of the elements array ; to change the values of all possible sum values to 1 ; if sum is possible then return 1 ; Driver code
function isPossible ( elements , sum ) { var dp = [ sum + 1 ] ; dp [ 0 ] = 1 ; for ( var i = 0 ; i < elements . length ; i ++ ) { for ( var j = sum ; j >= elements [ i ] ; j -- ) { if ( dp [ j - elements [ i ] ] == 1 ) dp [ j ] = 1 ; } } if ( dp [ sum ] == 1 ) return true ; return false ; } var elements = [ 6 , 2 , 5 ] ; var sum = 7 ; if ( isPossible ( elements , sum ) ) document . write ( " " ) ; else document . write ( " " ) ;
Dynamic Programming | High | Returns maximum amount of task that can be done till day n ; If n is less than equal to 0 , then no solution exists ; Determines which task to choose on day n , then returns the maximum till that day ; Driver code
function maxTasks ( high , low , n ) { if ( n <= 0 ) return 0 ; return Math . max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) ) ; } let n = 5 ; let high = [ 3 , 6 , 8 , 7 , 6 ] ; let low = [ 1 , 5 , 4 , 5 , 3 ] ; document . write ( maxTasks ( high , low , n ) ) ; ;
Binomial Coefficient | DP | Function to find gcd of two numbers in O ( log ( min ( a , b ) ) ) ; Base case ; C ( n , r ) = C ( n , n - r ) better time complexity for lesser r value ; Array of elements from n - r + 1 to n ; For numbers from 1 to r find arr [ j ] such that gcd ( i , arr [ j ] ) > 1 ; If gcd > 1 , divide both by gcd ; If i becomes 1 , no need to search arr ; Single pass to multiply the numerator ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function nCr ( n , r ) { if ( r > n ) return 0 ; if ( r > n - r ) r = n - r ; mod = 1000000007 ; var arr = new Array ( r ) ; for ( var i = n - r + 1 ; i <= n ; i ++ ) { arr [ i + r - n - 1 ] = i ; } var ans = 1 ; for ( var k = 1 ; k < r + 1 ; k ++ ) { var j = 0 , i = k ; while ( j < arr . length ) { var x = gcd ( i , arr [ j ] ) ; if ( x > 1 ) { arr [ j ] /= x ; i /= x ; } if ( i == 1 ) break ; j += 1 ; } } arr . forEach ( function ( i ) { ans = ( ans * i ) % mod ; } ) ; return ans ; } var n = 5 , r = 2 ; document . write ( " " + n + " " + r + " " + nCr ( n , r ) + " " ) ;
Kth character after replacing each character of String by its frequency exactly X times | javascript program for the above approach Function to find the Kth character after X days ; Variable to store the KthChar ; Traverse the string ; Convert char into int ; Calculate characters ; If K is less than sum than ans = str [ i ] ; Return answer ; Given Input ; Function Call
function FindKthChar ( str , K , X ) { var ans = " " ; var sum = 0 ; for ( i = 0 ; i < str . length ; i ++ ) { var digit = parseInt ( str [ i ] ) ; var range = parseInt ( Math . pow ( digit , X ) ) ; sum += range ; if ( K <= sum ) { ans = str [ i ] ; break ; } } return ans ; } var str = " " ; var K = 9 ; var X = 3 ; var ans = FindKthChar ( str , K , X ) ; document . write ( ans ) ;
Total character pairs from two strings , with equal number of set bits in their ascii value | JavaScript implementation of the approach Function to get no of set bits in binary representation of positive integer n ; Function to return the count of valid pairs ; Store frequency of number of set bits for s1 ; Store frequency of number of set bits for s2 ; Calculate total pairs ; Return the count of valid pairs ; Driver code
function countSetBits ( n ) { var count = 0 ; while ( n ) { count += n & 1 ; n >>= 1 ; } return count ; } function totalPairs ( s1 , s2 ) { var count = 0 ; var arr1 = new Array ( 7 ) . fill ( 0 ) ; var arr2 = new Array ( 7 ) . fill ( 0 ) ; for ( let i = 0 ; i < s1 . length ; i ++ ) { set_bits = countSetBits ( s1 [ i ] . charCodeAt ( 0 ) ) ; arr1 [ set_bits ] += 1 ; } for ( let i = 0 ; i < s2 . length ; i ++ ) { set_bits = countSetBits ( s2 [ i ] . charCodeAt ( 0 ) ) ; arr2 [ set_bits ] += 1 ; } for ( let i = 1 ; i < 7 ; i ++ ) { count += arr1 [ i ] * arr2 [ i ] ; } return count ; } var s1 = " " ; var s2 = " " ; document . write ( totalPairs ( s1 , s2 ) ) ;
Count substrings that starts with character X and ends with character Y | function to count substrings starting with character X and ending with character Y ; to store total count of required substrings ; to store count of character ' x ' up to the point the string ' str ' has been traversed so far ; traverse ' str ' form left to right ; if true , increment ' count _ x ' ; if true accumulate ' count _ x ' to ' tot _ count ' ; required count ; Driver code
function countSubstr ( str , n , x , y ) { var tot_count = 0 ; var count_x = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] === x ) count_x ++ ; if ( str [ i ] === y ) tot_count += count_x ; } return tot_count ; } var str = " " ; var n = str . length ; var x = " " , y = " " ; document . write ( " " + countSubstr ( str , n , x , y ) ) ;
Count words in a given string | javascript program to count no of words from given input string . ; returns number of words in str ; word count ; Scan all characters one by one ; If next character is a separator , set the state as OUT ; If next character is not a word separator and state is OUT , then set the state as IN and increment word count ; Move to next character ; Driver program to test above functions
var OUT = 0 ; var IN = 1 ; function countWords ( str ) { var state = OUT ; var wc = 0 ; var i = 0 ; while ( i < str . length ) { if ( str [ i ] == ' ' str [ i ] == ' ' str [ i ] == ' ' ) state = OUT ; else if ( state == OUT ) { state = IN ; ++ wc ; } ++ i ; } return wc ; } var str = " " ; document . write ( " " + countWords ( str ) ) ;
Enneadecagonal number | Function to calculate Enneadecagonal number ; Formula for finding nth Enneadecagonal number ; Driver code
function nthEnneadecagonal ( n ) { return ( 17 * n * n - 15 * n ) / 2 ; } let n = 6 ; document . write ( n + " " ) ; document . write ( nthEnneadecagonal ( n ) ) ;
Area of a Circumscribed Circle of a Square | Utiity function ; Driver code
function areacircumscribed ( a ) { return ( a * a * ( 3.1415 / 2 ) ) ; } let a = 6 ; document . write ( " " , areacircumscribed ( a ) ) ;
Find Nth item distributed from infinite items of infinite types based on given conditions | Function to find the type of the item given out according to the given rules ; Stores the count of item given out at each step ; Iterate to find the Nth day present is given out ; Find the number of presents given on day is day * ( day + 1 ) / 2 ; Iterate over the type ; Return the resultant type ; Driver code
function itemType ( n ) { let count = 0 ; let day = 1 ; while ( count + day * ( day + 1 ) / 2 < n ) { count += day * ( day + 1 ) / 2 ; day ++ ; } for ( let type = day ; type > 0 ; type -- ) { count += type ; if ( count >= n ) { return type ; } } } let N = 10 ; document . write ( itemType ( N ) ) ;