codeparrot/xlcost-text-to-code huggingface dataset:
Count distinct median possible for an Array using given ranges of elements | Function to count the number of distinct medians of an array where each array elements are given by a range ; Loop to store the starting and end range in the array ; Condition to check if the length of the array is odd ; Driver Code ; Function call
function solve ( n , vec ) { let a = [ ] ; let b = [ ] ; for ( let pr = 0 ; pr < vec . length ; pr ++ ) { a . push ( vec [ pr ] [ 0 ] ) ; b . push ( vec [ pr ] [ 1 ] ) ; } a . sort ( function ( c , d ) { return c - d ; } ) ; b . sort ( function ( c , d ) { return c - d ; } ) ; let left , right , ans ; if ( ( n & 1 ) != 0 ) { left = a [ Math . floor ( n / 2 ) ] ; right = b [ Math . floor ( n / 2 ) ] ; ans = right - left + 1 ; } else { left = ( a [ Math . floor ( n / 2 ) ] + a [ Math . floor ( n / 2 ) - 1 ] ) ; right = ( b [ Math . floor ( n / 2 ) ] + b [ Math . floor ( n / 2 ) - 1 ] ) ; ans = right - left + 1 ; } document . write ( ans ) ; } let N = 3 ; let vec = [ ] ; vec . push ( [ 100 , 100 ] ) ; vec . push ( [ 10 , 10000 ] ) ; vec . push ( [ 1 , 1000000000 ] ) ; solve ( N , vec ) ;
Count of pairs from Array with sum equal to twice their bitwise AND | Map to store the occurrence of elements of array ; Function to find the pairs with equal sum and twice the bitwise AND of the pairs ; Loop to find the frequency of elements of array ; Function to find the count such pairs in the array ; If an element occurs more than once then the answer will by incremented by nC2 times ; Driver Code ; Function Call
let mp = new Map ( ) ; function find_pairs ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } for ( let [ key , value ] of mp . entries ( ) ) { let count = value ; if ( count > 1 ) { ans += ( ( count * ( count - 1 ) ) / 2 ) ; } } return ans ; } let arr = [ 1 , 2 , 3 , 3 , 4 , 5 , 5 , 7 , 8 ] ; let arr_size = arr . length ; document . write ( find_pairs ( arr , arr_size ) ) ;
360 | Function to find the nth 360 - gon Number ; Driver Code
function gonNum360 ( n ) { return ( 358 * n * n - 356 * n ) / 2 ; } var n = 3 ; document . write ( gonNum360 ( n ) ) ;
120 | Function to find the nth 120 - gon Number ; Driver Code
function gonNum120 ( n ) { return ( 118 * n * n - 116 * n ) / 2 ; } var n = 3 ; document . write ( gonNum120 ( n ) ) ;
Tetracontaoctagonal Number | Function to find the nth Tetracontaoctagonal Number ; Driver Code
function TetracontaoctagonalNum ( n ) { return ( 46 * n * n - 44 * n ) / 2 ; } var n = 3 ; document . write ( TetracontaoctagonalNum ( n ) ) ;
257 | Function to find the nth 257 - gon Number ; Driver Code
function gonNum257 ( n ) { return ( 255 * n * n - 253 * n ) / 2 ; } var n = 3 ; document . write ( gonNum257 ( n ) ) ;
Tetracontadigonal Number | Function to find the nth Tetracontadigonal Number ; Driver Code
function TetracontadigonalNum ( n ) { return ( 40 * n * n - 38 * n ) / 2 ; } let n = 3 ; document . write ( TetracontadigonalNum ( n ) ) ;
Index of smallest triangular number with N digits | Function to return index of smallest triangular no n digits ; Driver code
function findIndex ( n ) { let x = Math . sqrt ( 2 * Math . pow ( 10 , ( n - 1 ) ) ) ; return Math . round ( x ) ; } let n = 3 ; document . write ( findIndex ( n ) ) ;
Program to find the LCM of two prime numbers | Function to return the LCM of two prime numbers ; If the two numbers are equal then return any one of a and b ; Else return product of numbers ; Given two numbers ; Function Call
function findLCMPrime ( a , b ) { if ( a == b ) { return a ; } return a * b ; } let a = 3 , b = 5 ; document . write ( findLCMPrime ( a , b ) ) ;
Smallest multiple of N with exactly N digits in its Binary number representation | Function to find smallest multiple of n with exactly n digits in Binary number representation . ; Driver code
function smallestNumber ( N ) { document . write ( N * parseInt ( Math . ceil ( Math . pow ( 2 , ( N - 1 ) ) / N ) ) ) ; } let N = 3 ; smallestNumber ( N ) ;
Find the largest N digit multiple of N | Function to find the largest N digit multiple of N ; Driver code
function smallestNumber ( N ) { document . write ( N * Math . floor ( ( Math . pow ( 10 , N ) - 1 ) / N ) ) ; } let N = 2 ; smallestNumber ( N ) ;
Icosikaiheptagonal Number | Function to find the nth icosikaiheptagonal Number ; Driver code
function icosikaiheptagonalNum ( n ) { return ( 25 * n * n - 23 * n ) / 2 ; } let n = 3 ; document . write ( " " + icosikaiheptagonalNum ( n ) ) ;
Program to check if N is a triacontagonal number | Function to check whether a number is an triacontagonal number or not ; Condition to check whether a number is an triacontagonal number or not ; Given number ; Function call
function istriacontagonal ( N ) { let n = ( ( 26 + Math . sqrt ( 224 * N + 676 ) ) / 56 ) ; return ( n - parseInt ( n ) ) == 0 ; } let N = 30 ; if ( istriacontagonal ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Parity of the given mathematical expression using given N numbers | Javascript program to determine the parity of the given mathematical expression ; Iterating through the given integers ; If any odd number is present , then S is even parity ; Else , S is odd parity ; Driver Code
function getParity ( n , A ) { for ( let x in A ) { if ( ( x & 1 ) == 1 ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } let N = 3 ; let A = [ 2 , 3 , 1 ] ; getParity ( N , A ) ;
Count of digits after concatenation of first N positive integers | Function to find the number of digits after concatenating the first N positive integers ; Driver code
function numberOfDigits ( N ) { let nod = Math . floor ( Math . log10 ( N ) + 1 ) ; let toDecrease = ( Math . pow ( 10 , nod ) - 1 ) / 9 ; document . write ( ( N + 1 ) * nod - toDecrease ) ; } let N = 13 ; numberOfDigits ( N ) ;
Length of maximum product subarray | Function that returns the maximum length subarray having non zero product ; zeroindex list to store indexex of zero ; If zeroindex list is empty then Maxlength is as size of array ; If zeroindex list is not empty ; first zero is on index 2 that means two numbers positive , before index 2 so as their product is positive to ; Checking for other indexex ; If the difference is greater than maxlen then maxlen is updated ; To check the length of remaining array after last zeroindex ; Driver Code
function Maxlength ( arr , N ) { let zeroindex = Array . from ( { length : 20000 } , ( _ , i ) => 0 ) ; let maxlen ; let size = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zeroindex [ size ++ ] = i ; } if ( size == 0 ) { maxlen = N ; } else { maxlen = zeroindex [ 0 ] ; for ( let i = 0 ; i < size ; i ++ ) { if ( zeroindex [ i + 1 ] - zeroindex [ i ] - 1 > maxlen ) { maxlen = zeroindex [ i + 1 ] - zeroindex [ i ] - 1 ; } } if ( N - zeroindex [ size - 1 ] - 1 > maxlen ) { maxlen = N - zeroindex [ size - 1 ] - 1 ; } } document . write ( maxlen ) ; } let N = 9 ; let arr = [ 7 , 1 , 0 , 1 , 2 , 0 , 9 , 2 , 1 ] ; Maxlength ( arr , N ) ;
Check if sum of exactly K elements of the Array can be odd or not | Function returns true if it is possible to have odd sum ; Counting number of odd and even elements ; Driver Code
function isPossible ( arr , N , K ) { let oddCount = 0 , evenCount = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { evenCount ++ ; } else { oddCount ++ ; } } if ( evenCount == N || ( oddCount == N && K % 2 == 0 ) || ( K == N && oddCount % 2 == 0 ) ) { return false ; } else { return true ; } } let arr = [ 1 , 2 , 3 , 4 , 5 , 8 ] ; let K = 5 ; let N = arr . length ; if ( isPossible ( arr , N , K ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Find the last two digits of Factorial of a given Number | Function to print the last two digits of N ! ; For N >= 10 , N ! % 100 will always be 0 ; Calculating N ! % 100 ; Driver code
function lastTwoDigits ( N ) { if ( N >= 10 ) { cout << " " ; return ; } let fac = 1 ; for ( let i = 1 ; i <= N ; i ++ ) fac = ( fac * i ) % 100 ; document . write ( fac ) ; } let N = 7 ; lastTwoDigits ( N ) ;
Product of N terms of a given Geometric series | Function to calculate product of geometric series ; Return the final product with the above formula ; Given first term and common ratio ; Number of terms ; Function Call
function productOfGP ( a , r , n ) { return Math . pow ( a , n ) * Math . pow ( r , n * ( n - 1 ) / 2 ) ; } let a = 1 , r = 2 ; let N = 4 ; document . write ( productOfGP ( a , r , N ) ) ;
Product of N terms of a given Geometric series | Function to calculate product of N terms of geometric series ; Find the product of first and the last term ; Return the sqrt of the above expression to find the product ; Given first term and common ratio ; Number of terms ; Function Call
function productOfGP ( a , r , n ) { let an = a * Math . pow ( r , n - 1 ) ; return Math . sqrt ( Math . pow ( a * an , n ) ) ; } let a = 1 , r = 2 ; let N = 4 ; document . write ( productOfGP ( a , r , N ) ) ;
Find two numbers such that difference of their squares equal to N | Function to check and print the required two positive integers ; Iterate till sqrt ( n ) to find factors of N ; Check if x is one of the factors of N ; Store the factor ; Compute the other factor ; Check if the two factors are of the same parity ; Compute a and b ; If no pair exists ; Driver Code
function solve ( n ) { for ( var x = 1 ; x <= Math . sqrt ( n ) ; x ++ ) { if ( n % x == 0 ) { var small = x ; var big = n / x ; if ( small % 2 == big % 2 ) { var a = ( small + big ) / 2 ; var b = ( big - small ) / 2 ; document . write ( a + " " + b ) ; return ; } } } document . write ( - 1 ) ; } var n = 7 ; solve ( n ) ;
Probability that an arbitrary positive divisor of 10 ^ X is an integral multiple of 10 ^ Y | Function to calculate and print the required probability ; Count of potential divisors of X - th power of 10 which are also multiples of Y - th power of 10 ; Count of divisors of X - th power of 10 ; Calculate GCD ; Print the reduced fraction probability ; Driver Code
function prob ( x , y ) { var num = Math . abs ( x - y + 1 ) * Math . abs ( x - y + 1 ) ; var den = ( x + 1 ) * ( x + 1 ) ; var gcd = __gcd ( num , den ) ; document . write ( num / gcd + " " + den / gcd + " " ) ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var X = 2 , Y = 1 ; prob ( X , Y ) ;
Program to check if N is a Chiliagon Number | Function to check that if N is Chiliagon Number or not ; Condition to check if N is a Chiliagon Number ; Given Number ; Function call
function is_Chiliagon ( N ) { let n = ( 996 + Math . sqrt ( 7984 * N + 992016 ) ) / 1996 ; return ( n - Math . floor ( n ) ) == 0 ; } let N = 1000 ; if ( is_Chiliagon ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Count of common subarrays in two different permutations of 1 to N | Javascript implementation of the above approach ; Initialising Map for Index Mapping ; Mapping elements of A ; Modify elements of B according to Map ; Changing B [ i ] as the index of B [ i ] in A ; Count of common subarrays ; Traversing array B ; While consecutive elements are found , we increment K ; Add number of subarrays with length K to total count ; Driver Code
function commonSubarrays ( A , B , N ) { let Map = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N ; i ++ ) Map [ A [ i ] ] = i ; for ( let i = 0 ; i < N ; i ++ ) { B [ i ] = Map [ B [ i ] ] ; } let count = 0 ; let i = 0 , K ; while ( i < N ) { K = 1 ; i += 1 ; while ( i < N && B [ i ] == B [ i - 1 ] + 1 ) { i += 1 ; K += 1 ; } count = count + ( ( K ) * ( K + 1 ) ) / 2 ; } return count ; } let N = 3 ; let A = [ 1 , 2 , 3 ] ; let B = [ 2 , 3 , 1 ] ; document . write ( commonSubarrays ( A , B , N ) ) ; document . write ( " " ) ; N = 5 ; let C = [ 1 , 2 , 3 , 4 , 5 ] ; let D = [ 2 , 3 , 1 , 4 , 5 ] ; document . write ( commonSubarrays ( C , D , N ) ) ;
Represent N as sum of K even or K odd numbers with repetitions allowed | Function to find the array with all the even / odd elements ; First let 's check  kth is odd or even ; If last element is also an odd number then we can choose odd elements for our answer ; Add 1 in the array ( k - 1 ) times ; Add last odd element ; If array of even elements would be the answer then k - 1 elements would be 2 ; If last element is also an even number then we can choose even elements for our answer ; Add 2 in the array ( k - 1 ) times ; Add last even element ; Prleting the array ; Driver Code
function getArrayOfSizeK ( n , k ) { let ans = [ ] ; let odd = n - ( ( k - 1 ) * 1 ) ; if ( odd > 0 && odd % 2 != 0 ) { for ( let i = 0 ; i < k - 1 ; i ++ ) { ans . push ( 1 ) ; } ans . push ( odd ) ; } let even = n - ( ( k - 1 ) * 2 ) ; if ( even > 0 && even % 2 == 0 && ans . length == 0 ) { for ( let i = 0 ; i < k - 1 ; i ++ ) { ans . push ( 2 ) ; } ans . push ( even ) ; } if ( ans . length > 0 ) { for ( let i = 0 ; i < k ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } else { document . write ( " " ) ; } } let n = 10 , k = 3 ; getArrayOfSizeK ( n , k ) ;
Check whether a given number N is a Nude Number or not | Check if all digits of num divide num ; array to store all digits of the given number ; If any of the condition is true for any digit Then N is not a nude number ; Driver code
function checkDivisbility ( num ) { let digit ; let N = num ; while ( num != 0 ) { digit = num % 10 ; num = Math . floor ( num / 10 ) ; if ( digit == 0 N % digit != 0 ) return false ; } return true ; } let N = 128 ; let result = checkDivisbility ( N ) ; if ( result ) document . write ( " " ) ; else document . write ( " " ) ;
Hensel 's Lemma | Function to find the modular inverse of a modulo m ; Apply the Euclidean algorithm , to find the modular inverse ; Function to find the derivative of f ( x ) and f '(x) = 3 * (x ^ 2) ; Function to find the image of x in f ( x ) = x ^ 3 - k . ; Function to find the next power of the number ; Next power of prime for which solution is to be found ; Using Hensel 's recursion to  find the solution (next_a) for  next power of prime ; If next_a < 0 return equivalent positive remainder modulo p ; Return the next power of a ; Function to find the solution of the required exponent of prime ; The lemma does not work for derivative of f ( x ) at a1 ; Looping from 1 to power of prime whose solution is to be found ; Final answer after evaluating all the exponents up till the required exponent ; Driver Code ; Function Call
function inv ( a , m ) { let m0 = m , t , q ; let x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = Math . floor ( a / m ) ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } function derivative ( x ) { return 3 * x * x ; } function Image ( x , k ) { return x * x * x - k ; } function next_power ( a_t , t , a1 , prime , k ) { let power_p = Math . floor ( Math . pow ( prime , t + 1 ) ) ; let next_a = ( a_t - Image ( a_t , k ) * inv ( derivative ( a1 ) , prime ) ) % power_p ; if ( next_a < 0 ) return next_a += power_p ; return next_a ; } function powerOfPrime ( prime , power , k , a1 ) { if ( derivative ( a1 ) != 0 ) { let a_t = a1 ; for ( let p = 1 ; p < power ; p ++ ) { a_t = next_power ( a_t , p , a1 , prime , k ) ; } return a_t ; } return - 1 ; } let prime = 7 , a1 = 3 ; let power = 2 , k = 3 ; document . write ( powerOfPrime ( prime , power , k , a1 ) ) ;
Minimum LCM of all pairs in a given array | function that return pair having minimum LCM ; Find max element in the array as the gcd of two elements from the array can 't greater than max element. ; Created a 2D array to store minimum two multiple of any particular i . ; We already found two smallest multiple ; Iterating over all gcd ; Iterating over its multiple ; If we already found the two smallest multiple of i ; Choosing smallest two multiple ; Calculating lcm ; Return final answer ; Driver code
function minLCM ( arr , n ) { var mx = 0 ; for ( var i = 0 ; i < n ; i ++ ) { mx = Math . max ( mx , arr [ i ] ) ; } var mul = Array . from ( Array ( mx + 1 ) , ( ) => Array ( ) ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( mul [ arr [ i ] ] . length > 1 ) { continue ; } mul [ arr [ i ] ] . push ( arr [ i ] ) ; } for ( var i = 1 ; i <= mx ; i ++ ) { for ( var j = i + i ; j <= mx ; j += i ) { if ( mul [ i ] . length > 1 ) { break ; } mul [ j ] . forEach ( k => { if ( mul [ i ] . length <= 1 ) { mul [ i ] . push ( k ) ; } } ) ; } } var ans = 1000000000 ; for ( var i = 1 ; i <= mx ; i ++ ) { if ( mul [ i ] . length <= 1 ) continue ; var a = mul [ i ] [ 0 ] , b = mul [ i ] [ 1 ] ; var lcm = ( a * b ) / i ; ans = Math . min ( ans , lcm ) ; } return ans ; } var arr = [ 2 , 4 , 3 , 6 , 5 ] ; var n = arr . length ; document . write ( minLCM ( arr , n ) + " " ) ;
Count of triplets of numbers 1 to N such that middle element is always largest | Function to find number of triplets for given number N such that middle element is always greater than left and right side element . ; Check if arrangement is possible or not ; Else return total ways ; Driver Code
function findArrangement ( N ) { if ( N < 3 ) return 0 ; return ( ( N ) * ( N - 1 ) * ( N - 2 ) ) / 3 ; } let N = 10 ; document . write ( findArrangement ( N ) ) ;
Check whether the binary equivalent of a number ends with "001" or not | Function returns true if s1 is suffix of s2 ; Function to check if binary equivalent of a number ends in "001" or not ; To store the binary number ; Count used to store exponent value ; Driver code
function isSuffix ( s1 , s2 ) { var n1 = s1 . length ; var n2 = s2 . length ; if ( n1 > n2 ) return false ; for ( var i = 0 ; i < n1 ; i ++ ) if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) return false ; return true ; } function CheckBinaryEquivalent ( N ) { var B_Number = 0 ; var cnt = 0 ; while ( N != 0 ) { var rem = N % 2 ; var c = Math . pow ( 10 , cnt ) ; B_Number += rem * c ; N = Math . floor ( N / 2 ) ; cnt ++ ; } console . log ( B_Number ) ; var bin = B_Number . toString ( ) ; return isSuffix ( " " , bin ) ; } var N = 9 ; if ( CheckBinaryEquivalent ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check whether the binary equivalent of a number ends with "001" or not | Function to check if binary equivalent of a number ends in "001" or not ; To check if binary equivalent of a number ends in "001" or not ; Driver code
function CheckBinaryEquivalent ( N ) { return ( N - 1 ) % 8 == 0 ; } var N = 9 ; if ( CheckBinaryEquivalent ( N ) ) document . write ( " " ) ; else document . write ( " " ) ;
Panarithmic numbers within a given range | function to compute divisors of a number ; vector to store divisors ; 1 will always be a divisor ; check if i is squareroot of A then only one time insert it in ans ; function to check that a number can be represented as sum of distinct divisor or not ; The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; If sum is 0 , then answer is true ; If sum is not 0 and set is empty , then answer is false ; Fill the subset table in bottom up manner ; return the possibility of given sum ; function to check a number is Practical or not ; vector to store divisors ; if all numbers can be represented as sum of unique divisors ; function to print Practical Numbers in a range ; Driver Function
function get_divisors ( A ) { var ans = [ ] ; ans . push ( 1 ) ; for ( var i = 2 ; i <= Math . sqrt ( A ) ; i ++ ) { if ( A % i == 0 ) { ans . push ( i ) ; if ( ( i * i ) != A ) ans . push ( parseInt ( A / i ) ) ; } } return ans ; } function Sum_Possible ( set , sum ) { var n = set . length ; var subset = Array . from ( Array ( n + 1 ) , ( ) => Array ( sum + 1 ) ) ; for ( var i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( var i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( var i = 1 ; i <= n ; i ++ ) { for ( var j = 1 ; j <= sum ; j ++ ) { if ( j < set [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= set [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - set [ i - 1 ] ] ; } } return subset [ n ] [ sum ] ; } function Is_Practical ( A ) { var divisors = [ ] ; divisors = get_divisors ( A ) ; for ( var i = 2 ; i < A ; i ++ ) { if ( Sum_Possible ( divisors , i ) == false ) return false ; } return true ; } function print_practica_No ( A , B ) { for ( var i = A ; i <= B ; i ++ ) { if ( Is_Practical ( i ) == true ) { document . write ( i + " " ) ; } } } var A = 1 , B = 100 ; print_practica_No ( A , B ) ;
Maximize the division result of Array using given operations | Function to find the max result ; Sort the array in descending order ; Loop to divide in this order arr [ 0 ] / ( arr [ 1 ] / arr [ 2 ] / ... . arr [ n - 2 ] / arr [ n - 1 ] ) ; Return the final result ; Driver Code
function maxDivision ( arr , n ) { arr . sort ( ( a , b ) => b - a ) ; let mxdiv = arr [ 1 ] ; for ( let i = 2 ; i < n ; ++ i ) mxdiv = mxdiv / arr [ i ] ; return arr [ 0 ] / mxdiv ; } let arr = [ 100 , 1000 , 10 , 2 ] ; let n = arr . length ; document . write ( maxDivision ( arr , n ) ) ;
Print all distinct Coprime sets possible from 1 to N | Function to print all co - prime sets ; Check if n is less than 4 then simply print all values till n ; For all the values of n > 3 ; Check if n is even then every set will contain 2 adjacent elements up - to n ; If n is odd then every set will contain 2 adjacent element except the last set which will have last three elements ; Last element for odd case ; Driver Code
function coPrimeSet ( n ) { let firstadj , secadj ; if ( n < 4 ) { document . write ( " " ) ; for ( let i = 1 ; i <= n ; i ++ ) document . write ( i + " " ) ; document . write ( " " + " " ) ; } else { if ( n % 2 == 0 ) { for ( let i = 0 ; i < Math . floor ( n / 2 ) ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; document . write ( " " + firstadj + " " + secadj + " " + " " ) ; } } else { for ( let i = 0 ; i < Math . floor ( n / 2 ) - 1 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; document . write ( " " + firstadj + " " + secadj + " " + " " ) ; } document . write ( " " + ( n - 2 ) + " " + ( n - 1 ) + " " + n + " " + " " ) ; } } } let n = 5 ; coPrimeSet ( n ) ;
Find the sequence number of a triangular number | javascript code to print sequence number of a triangular number ; If N is not tringular number
let N = 21 ; let A = Math . sqrt ( 2 * N + 0.25 ) - 0.5 ; let B = A ; if ( B != A ) document . write ( " " ) ; else document . write ( B ) ;
Count subarrays with sum equal to its XOR value | Function to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Maintain two pointers left and right ; Iterating through the array ; Calculate the window where the above condition is satisfied ; Count will be ( right - left ) ; Remove the previous element as it is already included ; Driver code
function operation ( arr , N ) { let right = 0 , ans = 0 , num = 0 ; for ( let left = 0 ; left < N ; left ++ ) { while ( right < N && num + arr [ right ] == ( num ^ arr [ right ] ) ) { num += arr [ right ] ; right ++ ; } ans += right - left ; if ( left == right ) right ++ ; else num -= arr [ left ] ; } return ans ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; document . write ( operation ( arr , N ) ) ;
Count of pairs in an Array whose sum is Prime | Function for Sieve Of Eratosthenes ; Function to count total number of pairs of elements whose sum is prime ; Driver Code
function sieveOfEratosthenes ( N ) { let isPrime = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N + 1 ; i ++ ) { isPrime [ i ] = true ; } isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( let i = 2 ; i * i <= N ; i ++ ) { if ( isPrime [ i ] == true ) { let j = 2 ; while ( i * j <= N ) { isPrime [ i * j ] = false ; j ++ ; } } } return isPrime ; } function numPairsWithPrimeSum ( arr , n ) { let N = 2 * 1000000 ; let isPrime = sieveOfEratosthenes ( N ) ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { let sum = arr [ i ] + arr [ j ] ; if ( isPrime [ sum ] ) { count ++ ; } } } return count ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; document . write ( numPairsWithPrimeSum ( arr , n ) ) ;