codeparrot/xlcost-text-to-code huggingface dataset:
Tetranacci Numbers | Function to print the N - th tetranacci number ; base cases ; Driver code
function printTetra ( n ) { let dp = new Array ( n + 5 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = dp [ 2 ] = 1 ; dp [ 3 ] = 2 ; for ( let i = 4 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + dp [ i - 3 ] + dp [ i - 4 ] ; document . write ( dp [ n ] ) ; } let n = 10 ; printTetra ( n ) ;
Maximum sum in circular array such that no two elements are adjacent | Function to calculate the sum from 0 th position to ( n - 2 ) th position ; copy the element of original array to dp [ ] ; find the maximum element in the array ; start from 2 nd to n - 1 th pos ; traverse for all pairs bottom - up approach ; dp - condition ; find maximum sum ; return the maximum ; Function to find the maximum sum from 1 st position to n - 1 - th position ; Traverse from third to n - th pos ; bootom - up approach ; dp condition ; find max sum ; return max ; Driver Code
function maxSum1 ( arr , n ) { let dp = new Array ( n ) ; let maxi = 0 ; for ( i = 0 ; i < n - 1 ; i ++ ) { dp [ i ] = arr [ i ] ; if ( maxi < arr [ i ] ) maxi = arr [ i ] ; } for ( i = 2 ; i < n - 1 ; i ++ ) { for ( j = 0 ; j < i - 1 ; j ++ ) { if ( dp [ i ] < dp [ j ] + arr [ i ] ) { dp [ i ] = dp [ j ] + arr [ i ] ; if ( maxi < dp [ i ] ) maxi = dp [ i ] ; } } } return maxi ; } function maxSum2 ( arr , n ) { let dp = new Array ( n ) ; let maxi = 0 ; for ( i = 1 ; i < n ; i ++ ) { dp [ i ] = arr [ i ] ; if ( maxi < arr [ i ] ) maxi = arr [ i ] ; } for ( i = 3 ; i < n ; i ++ ) { for ( j = 1 ; j < i - 1 ; j ++ ) { if ( dp [ i ] < arr [ i ] + dp [ j ] ) { dp [ i ] = arr [ i ] + dp [ j ] ; if ( maxi < dp [ i ] ) maxi = dp [ i ] ; } } } return maxi ; } function findMaxSum ( arr , n ) { let t = Math . max ( maxSum1 ( arr , n ) , maxSum2 ( arr , n ) ) ; return t ; } let arr = [ 1 , 2 , 3 , 1 ] ; let n = arr . length ; document . write ( findMaxSum ( arr , n ) ) ;
Permutation Coefficient | Returns value of Permutation Coefficient P ( n , k ) ; Calculate value of Permutation Coefficient in bottom up manner ; Base Cases ; Calculate value using previosly stored values ; This step is important as P ( i , j ) = 0 for j > i ; Driver Code
function permutationCoeff ( n , k ) { let P = new Array ( n + 2 ) ; for ( let i = 0 ; i < n + 2 ; i ++ ) { P [ i ] = new Array ( k + 2 ) ; } for ( let i = 0 ; i <= n ; i ++ ) { for ( let j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 ) P [ i ] [ j ] = 1 ; else P [ i ] [ j ] = P [ i - 1 ] [ j ] + ( j * P [ i - 1 ] [ j - 1 ] ) ; P [ i ] [ j + 1 ] = 0 ; } } return P [ n ] [ k ] ; } let n = 10 , k = 2 ; document . write ( " " + n + " " + k + " " + " " + permutationCoeff ( n , k ) ) ;
Permutation Coefficient | Returns value of Permutation Coefficient P ( n , k ) ; base case ; Calculate value factorials up to n ; P ( n , k ) = n ! / ( n - k ) ! ; Driver Code
function permutationCoeff ( n , k ) { let fact = new Array ( n + 1 ) ; fact [ 0 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) fact [ i ] = i * fact [ i - 1 ] ; return parseInt ( fact [ n ] / fact [ n - k ] , 10 ) ; } let n = 10 , k = 2 ; document . write ( " " + " " + n + " " + k + " " + permutationCoeff ( n , k ) ) ;
Dynamic Programming | Returns true if there is a subset of set [ ] with sum equal to given sum ; Base Cases ; If last element is greater than sum , then ignore it ; else , check if sum can be obtained by any of the following ( a ) including the last element ( b ) excluding the last element ; Driver Code
function isSubsetSum ( set , n , sum ) { if ( sum == 0 ) return true ; if ( n == 0 ) return false ; if ( set [ n - 1 ] > sum ) return isSubsetSum ( set , n - 1 , sum ) ; return isSubsetSum ( set , n - 1 , sum ) || isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ; } let set = [ 3 , 34 , 4 , 12 , 5 , 2 ] ; let sum = 9 ; let n = set . length ; if ( isSubsetSum ( set , n , sum ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Count of cyclic permutations having XOR with other binary string as 0 | Implementation of Z - algorithm for linear time pattern searching ; Function to get the count of the cyclic permutations of b that given 0 when XORed with a ; concatenate b with b ; new b now contains all the cyclic permutations of old b as it 's sub-strings ; concatenate pattern with text ; Fill z array used in Z algorithm ; pattern occurs at index i since z value of i equals pattern length ; Driver Code
function compute_z ( s , z ) { var l = 0 , r = 0 ; var n = s . length ; for ( var i = 1 ; i <= n - 1 ; i ++ ) { if ( i > r ) { l = i ; r = i ; while ( r < n && s [ r - l ] === s [ r ] ) { r ++ ; } z [ i ] = r - l ; r -- ; } else { var k = i - l ; if ( z [ k ] < r - i + 1 ) { z [ i ] = z [ k ] ; } else { l = i ; while ( r < n && s [ r - l ] === s [ r ] ) { r ++ ; } z [ i ] = r - l ; r -- ; } } } } function countPermutation ( a , b ) { b = b + b ; b = b . substring ( 0 , b . length - 1 ) ; var ans = 0 ; var s = a + " " + b ; var n = s . length ; var z = new Array ( n ) . fill ( 0 ) ; compute_z ( s , z ) ; for ( var i = 1 ; i <= n - 1 ; i ++ ) { if ( z [ i ] === a . length ) { ans ++ ; } } return ans ; } var a = " " ; var b = " " ; document . write ( countPermutation ( a , b ) ) ;
Lexicographically smallest K | function for reverse ; Function to find lexicographically smallest subsequence of size K ; Length of string ; Stores the minimum subsequence ; Traverse the string S ; If the stack is empty ; Iterate till the current character is less than the the character at the top of stack ; Check if there are enough characters remaining to obtain length K ; If stack size is < K ; Push the current character into it ; Stores the resultant string ; Iterate until stack is empty ; Reverse the string ; Print the string
function reverse ( input ) { a = input ; var l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { var temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a ; } function smallestSubsequence ( S , K ) { var N = S . length ; answer = [ ] ; for ( var i = 0 ; i < N ; ++ i ) { if ( answer . length == 0 ) { answer . push ( S [ i ] ) ; } else { while ( ( answer . length != 0 ) && ( S [ i ] < answer [ answer . length - 1 ] ) && ( answer . length - 1 + N - i >= K ) ) { answer . pop ( ) ; } if ( answer . length == 0 answer . length < K ) { answer . push ( S [ i ] ) ; } } } var ret = [ ] ; while ( answer . length != 0 ) { ret += answer [ answer . length - 1 ] ; answer . pop ( ) ; } reverse ( ret ) ; document . write ( ret ) ; } var S = " " ; var K = 3 ; smallestSubsequence ( S , K ) ;
Check if string is right to left diagonal or not | Function to check if the given string is right to left diagonal or not ; Iterate over string ; If character is not same as the first character then return false ; Given String str ; Function call
function is_rtol ( s ) { let tmp = ( Math . sqrt ( s . length ) ) - 1 ; let first = s [ tmp ] ; for ( let pos = tmp ; pos < s . length - 1 ; pos += tmp ) { if ( s [ pos ] != first ) { return false ; } } return true ; } let str = " " ; if ( is_rtol ( str ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Split a given string into substrings of length K with equal sum of ASCII values | Function for checking string ; Check if the string can be split into substrings of K length only ; Compute the sum of first substring of length K ; Compute the sum of remaining substrings ; Check if sum is equal to that of the first substring ; Since all sums are not equal , return false ; All sums are equal , Return true ; All substrings cannot be of size K ; Driver code
function check ( str , K ) { if ( str . length % K === 0 ) { var sum = 0 , i ; for ( i = 0 ; i < K ; i ++ ) { sum += str [ i ] . charCodeAt ( 0 ) ; } for ( var j = i ; j < str . length ; j += K ) { var s_comp = 0 ; for ( var p = j ; p < j + K ; p ++ ) s_comp += str [ p ] . charCodeAt ( 0 ) ; if ( s_comp !== sum ) return false ; } return true ; } return false ; } var K = 3 ; var str = " " ; if ( check ( str , K ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximize count of 0 s in left and 1 s in right substring by splitting given Binary string | Function to maximize the sum of the count of zeros and ones in the left and right substring ; To store the total ones ; Count the total numbers of ones in string str ; To store the count of zeros and ones while traversing string ; Iterate the given string and update the maximum sum ; Update the maximum Sum ; Given binary string ; Function call
function maxSum ( str ) { var maximumSum = 0 ; var totalOnes = 0 ; str . split ( ' ' ) . forEach ( c => { if ( c == ' ' ) totalOnes ++ ; } ) ; var zero = 0 , ones = 0 ; for ( var i = 0 ; str [ i ] ; i ++ ) { if ( str [ i ] == ' ' ) { zero ++ ; } else { ones ++ ; } maximumSum = Math . max ( maximumSum , zero + ( totalOnes - ones ) ) ; } return maximumSum ; } var str = " " ; document . write ( maxSum ( str ) ) ;
Longest substring such that no three consecutive characters are same | Function to return the length of the longest substring such that no three consecutive characters are same ; If the length of the given string is less than 3 ; Initialize temporary and final ans to 2 as this is the minimum length of substring when length of the given string is greater than 2 ; Traverse the string from the third character to the last ; If no three consecutive characters are same then increment temporary count ; Else update the final ans and reset the temporary count ; Driver code
function maxLenSubStr ( s ) { if ( s . length < 3 ) return s . length ; let temp = 2 ; let ans = 2 ; for ( let i = 2 ; i < s . length ; i ++ ) { if ( s [ i ] != s [ i - 1 ] s [ i ] != s [ i - 2 ] ) temp ++ ; else { ans = Math . max ( temp , ans ) ; temp = 2 ; } } ans = Math . max ( temp , ans ) ; return ans ; } let s = " " ; document . write ( maxLenSubStr ( s ) ) ;
Number of ways to remove a sub | Function to return the number of ways of removing a sub - string from s such that all the remaining characters are same ; To store the count of prefix and suffix ; Loop to count prefix ; Loop to count suffix ; First and last characters of the string are same ; Otherwise ; Driver Code
function no_of_ways ( s ) { let n = s . length ; let count_left = 0 , count_right = 0 ; for ( let i = 0 ; i < n ; ++ i ) { if ( s [ i ] == s [ 0 ] ) { ++ count_left ; } else break ; } for ( let i = n - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == s [ n - 1 ] ) { ++ count_right ; } else break ; } if ( s [ 0 ] == s [ n - 1 ] ) return ( ( count_left + 1 ) * ( count_right + 1 ) ) ; else return ( count_left + count_right + 1 ) ; } let s = " " ; document . write ( no_of_ways ( s ) ) ;
Count number of indices such that s [ i ] = s [ i + 1 ] : Range queries | Function to create prefix array ; Function to return the result of the query ; Driver Code ; Query 1 ; Query 2
function preCompute ( n , s , pref ) { pref [ 0 ] = 0 ; for ( let i = 1 ; i < n ; i ++ ) { pref [ i ] = pref [ i - 1 ] ; if ( s [ i - 1 ] == s [ i ] ) pref [ i ] ++ ; } } function query ( pref , l , r ) { return pref [ r ] - pref [ l ] ; } let s = " " ; let n = s . length ; let pref = new Array ( n ) ; preCompute ( n , s , pref ) ; let l = 1 ; let r = 2 ; document . write ( query ( pref , l , r ) + " " ) ; l = 1 ; r = 5 ; document . write ( query ( pref , l , r ) + " " ) ;
Find the direction from given string | Function to find the final direction ; if count is positive that implies resultant is clockwise direction ; if count is negative that implies resultant is anti - clockwise direction ; Driver code
function findDirection ( s ) { let count = 0 ; let d = " " ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ 0 ] == ' ' ) return null ; if ( s [ i ] == ' ' ) count -- ; else { if ( s [ i ] == ' ' ) count ++ ; } } if ( count > 0 ) { if ( count % 4 == 0 ) d = " " ; else if ( count % 4 == 1 ) d = " " ; else if ( count % 4 == 2 ) d = " " ; else if ( count % 4 == 3 ) d = " " ; } if ( count < 0 ) { if ( count % 4 == 0 ) d = " " ; else if ( count % 4 == - 1 ) d = " " ; else if ( count % 4 == - 2 ) d = " " ; else if ( count % 4 == - 3 ) d = " " ; } return d ; } let s = " " ; document . write ( findDirection ( s ) + " " ) ; s = " " ; document . write ( findDirection ( s ) ) ;
Check if lowercase and uppercase characters are in same order | Function to check if both the case follow the same order ; Traverse the string ; Store both lowercase and uppercase in two different strings ; transform lowerStr1 to upper ; Driver code
function isCheck ( str ) { var len = str . length ; var lowerStr = " " , upperStr = " " ; for ( var i = 0 ; i < len ; i ++ ) { if ( str [ i ] >= ' ' && str [ i ] < ' ' ) upperStr = upperStr + str [ i ] ; else lowerStr = lowerStr + str [ i ] ; } lowerStr = lowerStr . toUpperCase ( ) ; console . log ( lowerStr ) ; return lowerStr === upperStr ; } var str = " " ; isCheck ( str ) ? document . write ( " " ) : document . write ( " " ) ;
Replace every character of string by character whose ASCII value is K times more than it | Function to move string character ; changed string ; iterate for every characters ; ASCII value ; store the duplicate ; if k - th ahead character exceed ' z ' ; print the new string ; Driver Code ; function call
function encode ( s , k ) { let newS = " " ; for ( let i = 0 ; i < s . length ; ++ i ) { let val = s [ i ] . charCodeAt ( 0 ) ; let dup = k ; if ( val + k > 122 ) { k -= ( 122 - val ) ; k = k % 26 ; newS += String . fromCharCode ( 96 + k ) ; } else { newS += String . fromCharCode ( val + k ) ; } k = dup ; } document . write ( newS ) ; } let str = " " ; let k = 28 ; encode ( str , k ) ;
Modify string by removing vowels in between two consonants | Function to check if the character x is a vowel or not ; Returns the updated string formed after removing all the Sandwiched Vowels from the given string ; string to store the Updated String after removing the Sandwiched Vowels ; traverse the string from left to right ; if the current character is the first or the last character of the string then , this needs to be appended to the updatedString , since the corner alphabet irrespective of it being a vowel or a consonant , is never ' Sandwiched ' ; Check if the current character of the string is a vowel and both the previous and the next characters are consonants , if so then this is a sandwiched vowel , thus is ignored and not appended to the updated string ; if this character is not a sandwiched Vowel append it to the updated String ; Driver Code ; Remove all the Sandwitched Vowels
function isVowel ( x ) { if ( x === " " x === " " x === " " x === " " x === " " ) return true ; else return false ; } function updateSandwichedVowels ( a ) { var n = a . length ; var updatedString = " " ; for ( var i = 0 ; i < n ; i ++ ) { if ( i === 0 i === n - 1 ) { updatedString += a [ i ] ; continue ; } if ( isVowel ( a [ i ] ) === true && isVowel ( a [ i - 1 ] ) === false && isVowel ( a [ i + 1 ] ) === false ) { continue ; } updatedString += a [ i ] ; } return updatedString ; } var str = " " ; var updatedString = updateSandwichedVowels ( str ) ; document . write ( updatedString ) ;
Print all paths of the Binary Tree with maximum element in each path greater than or equal to K | A Binary Tree node ; A utility function to create a new node ; A recursive function to print the paths whose maximum element is greater than or equal to K . ; If the current node value is greater than or equal to k , then all the subtrees following that node will get printed , flag = 1 indicates to print the required path ; If the leaf node is encountered , then the path is printed if the size of the path vector is greater than 0 ; Append the node to the path vector ; Recur left and right subtrees ; Backtracking to return the vector and print the path if the flag is 1 ; Function to initialize the variables and call the utility function to print the paths with maximum values greater than or equal to K ; Initialize flag ; ans is used to check empty condition ; Call function that print path ; If the path doesn 't exist ; Driver code ; Constructing the following tree : 10 / \ 5 8 / \ / \ 29 2 1 98 / \ 20 50
class Node { constructor ( ) { this . data = 0 ; this . left = null ; this . right = null ; } } ; var ans = 0 ; function newNode ( data ) { var newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return ( newNode ) ; } function findPathUtil ( root , k , path , flag ) { if ( root == null ) return ; if ( root . data >= k ) flag = 1 ; if ( root . left == null && root . right == null ) { if ( flag == 1 ) { ans = 1 ; document . write ( " " ) ; for ( var i = 0 ; i < path . length ; i ++ ) { document . write ( path [ i ] + " " ) ; } document . write ( root . data + " " ) ; } return ; } path . push ( root . data ) ; findPathUtil ( root . left , k , path , flag ) ; findPathUtil ( root . right , k , path , flag ) ; path . pop ( ) ; } function findPath ( root , k ) { var flag = 0 ; ans = 0 ; var v = [ ] ; findPathUtil ( root , k , v , flag ) ; if ( ans == 0 ) document . write ( " " ) ; } var K = 25 ; var root = newNode ( 10 ) ; root . left = newNode ( 5 ) ; root . right = newNode ( 8 ) ; root . left . left = newNode ( 29 ) ; root . left . right = newNode ( 2 ) ; root . right . right = newNode ( 98 ) ; root . right . left = newNode ( 1 ) ; root . right . right . right = newNode ( 50 ) ; root . left . left . left = newNode ( 20 ) ; findPath ( root , K ) ;
Tridecagonal Number | Function to find N - th Tridecagonal number ; Formula to calculate nth Tridecagonal number ; Driver Code
function Tridecagonal_num ( n ) { return ( 11 * n * n - 9 * n ) / 2 ; } let n = 3 ; document . write ( Tridecagonal_num ( n ) + " " ) ; n = 10 ; document . write ( Tridecagonal_num ( n ) ) ;
Number of N digit integers with weight W | Function to find total possible numbers with n digits and weight w ; When Weight of an integer is Positive ; Subtract the weight from 9 ; When weight of an integer is negative ; add the weight to 10 to make it positive ; Driver code ; number of digits in an integer and w as weight ; print the total possible numbers with n digits and weight w
function findNumbers ( n , w ) { let x = 0 , sum = 0 ; if ( w >= 0 && w <= 8 ) { x = 9 - w ; } else if ( w >= - 9 && w <= - 1 ) { x = 10 + w ; } sum = Math . pow ( 10 , n - 2 ) ; sum = ( x * sum ) ; return sum ; } let n , w ; n = 3 ; w = 4 ; document . write ( findNumbers ( n , w ) ) ;
Maximum height of triangular arrangement of array values | Javascript program to find the maximum height of Pyramidal Arrangement of array values ; Just checking whether ith level is possible or not if possible then we must have atleast ( i * ( i + 1 ) ) / 2 elements in the array ; updating the result value each time ; otherwise we have exceeded n value ; Driver Code
function MaximumHeight ( a , n ) { let result = 1 ; for ( i = 1 ; i <= n ; ++ i ) { let y = ( i * ( i + 1 ) ) / 2 ; if ( y < n ) result = i ; else break ; } return result ; } let arr = [ 40 , 100 , 20 , 30 ] ; let n = arr . length ; document . write ( MaximumHeight ( arr , n ) ) ;
k | Javascript program to find k - th element in the Odd - Even sequence . ; insert all the odd numbers from 1 to n . ; insert all the even numbers from 1 to n . ; Driver code
function findK ( n , k ) { let a = [ ] ; for ( let i = 1 ; i < n ; i ++ ) if ( i % 2 == 1 ) a . push ( i ) ; for ( let i = 1 ; i < n ; i ++ ) if ( i % 2 == 0 ) a . push ( i ) ; return ( a [ k - 1 ] ) ; } let n = 10 , k = 3 ; document . write ( findK ( n , k ) ) ;
One line function for factorial of a number | Javascript program to find factorial of given number ; single line to find factorial ; Driver Code
function factorial ( n ) { return ( n == 1 n == 0 ) ? 1 : n * factorial ( n - 1 ) ; } let num = 5 ; document . write ( " " , num , " " , factorial ( num ) ) ;
Pell Number | calculate nth pell number ; Driver function
function pell ( n ) { if ( n <= 2 ) return n ; let a = 1 ; let b = 2 ; let c ; for ( let i = 3 ; i <= n ; i ++ ) { c = 2 * b + a ; a = b ; b = c ; } return b ; } let n = 4 ; document . write ( pell ( n ) ) ;
An efficient way to check whether n | Returns true if n - th Fibonacci number is multiple of 10. ; Driver Code
function isMultipleOf10 ( n ) { return ( n % 15 == 0 ) ; } let n = 30 ; if ( isMultipleOf10 ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find politeness of a number | A function to count all odd prime factors of a given number n ; Eliminate all even prime factor of number of n ; n must be odd at this point , so iterate for only odd numbers till sqrt ( n ) ; if i divides n , then start counting of Odd divisors ; If n odd prime still remains then count it ; Driver Code
function countOddPrimeFactors ( n ) { let result = 1 ; while ( n % 2 == 0 ) n /= 2 ; for ( let i = 3 ; i * i <= n ; i += 2 ) { let divCount = 0 ; while ( n % i == 0 ) { n /= i ; ++ divCount ; } result *= divCount + 1 ; } if ( n > 2 ) result *= 2 ; return result ; } function politness ( n ) { return countOddPrimeFactors ( n ) - 1 ; } let n = 90 ; document . write ( " " + n + " " + politness ( n ) + " " ) ; n = 15 ; document . write ( " " + n + " " + politness ( n ) ) ;
Nearest prime less than given number n | array to store all primes less than 10 ^ 6 ; Utility function of Sieve of Sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x ; This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; eliminate indexes which does not produce primes ; Since 2 is a prime number ; Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; modified binary search to find nearest prime less than N ; base condition is , if we are reaching at left corner or right corner of primes [ ] array then return that corner element because before or after that we don 't have any prime number in  primes array ; now if n is itself a prime so it will be present in primes array and here we have to find nearest prime less than n so we will return primes [ mid - 1 ] ; now if primes [ mid ] < n and primes [ mid + 1 ] > n that mean we reached at nearest prime ; Driver program to run the case
var primes = [ ] ; var MAX = 1000000 ; function Sieve ( ) { let n = MAX ; let nNew = parseInt ( Math . sqrt ( n ) ) ; var marked = new Array ( n / 2 + 500 ) . fill ( 0 ) ; for ( let i = 1 ; i <= parseInt ( ( nNew - 1 ) / 2 ) ; i ++ ) for ( let j = ( i * ( i + 1 ) ) << 1 ; j <= parseInt ( n / 2 ) ; j = j + 2 * i + 1 ) marked [ j ] = 1 ; primes . push ( 2 ) ; for ( let i = 1 ; i <= parseInt ( n / 2 ) ; i ++ ) if ( marked [ i ] == 0 ) primes . push ( 2 * i + 1 ) ; } function binarySearch ( left , right , n ) { if ( left <= right ) { let mid = parseInt ( ( left + right ) / 2 ) ; if ( mid == 0 mid == primes . length - 1 ) return primes [ mid ] ; if ( primes [ mid ] == n ) return primes [ mid - 1 ] ; if ( primes [ mid ] < n && primes [ mid + 1 ] > n ) return primes [ mid ] ; if ( n < primes [ mid ] ) return binarySearch ( left , mid - 1 , n ) ; else return binarySearch ( mid + 1 , right , n ) ; } return 0 ; } Sieve ( ) ; let n = 17 ; document . write ( binarySearch ( 0 , primes . length - 1 , n ) ) ;
Program for factorial of a number | function to find factorial of given number ; Driver Code
function factorial ( n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } let num = 5 ; document . write ( " " + num + " " + factorial ( num ) ) ;
Turn off the rightmost set bit | Set 2 | Unsets the rightmost set bit of n and returns the result ; Driver Code
function FlipBits ( n ) { return n -= ( n & ( - n ) ) ; } let N = 12 ; document . write ( " " ) ; document . write ( " " + FlipBits ( N ) ) ;
Maximum value of XOR among all triplets of an array | function to count maximum XOR value for a triplet ; set is used to avoid repetitions ; store all possible unique XOR value of pairs ; store maximum value ; Driver code
function Maximum_xor_Triplet ( n , a ) { let s = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { s . add ( a [ i ] ^ a [ j ] ) ; } } let ans = 0 ; for ( let i of s . values ( ) ) { for ( let j = 0 ; j < n ; j ++ ) { ans = Math . max ( ans , i ^ a [ j ] ) ; } } document . write ( ans , " " ) ; } let a = [ 1 , 3 , 8 , 15 ] ; let n = a . length ; Maximum_xor_Triplet ( n , a ) ;