codeparrot/xlcost-text-to-code huggingface dataset:
Maximum Prefix Sum possible by merging two given arrays | Javascript Program to implement the above approach ; Stores the maximum prefix sum of the array A [ ] ; Traverse the array A [ ] ; Stores the maximum prefix sum of the array B [ ] ; Traverse the array B [ ] ; Driver code
function maxPresum ( a , b ) { let X = Math . max ( a [ 0 ] , 0 ) ; for ( let i = 1 ; i < a . length ; i ++ ) { a [ i ] += a [ i - 1 ] ; X = Math . max ( X , a [ i ] ) ; } let Y = Math . max ( b [ 0 ] , 0 ) ; for ( let i = 1 ; i < b . length ; i ++ ) { b [ i ] += b [ i - 1 ] ; Y = Math . max ( Y , b [ i ] ) ; } return X + Y ; } let A = [ 2 , - 1 , 4 , - 5 ] ; let B = [ 4 , - 3 , 12 , 4 , - 3 ] ; document . write ( maxPresum ( A , B ) ) ;
Check if a number can be represented as sum of two positive perfect cubes | Function to check if N can be represented as sum of two perfect cubes or not ; if it is same return true ; ; if the curr smaller than n increment the lo ; if the curr is greater than curr decrement the hi ; Driver Code ; Function call to check if N can be represented as sum of two perfect cubes or not
function sumOfTwoCubes ( n ) { var lo = 1 , hi = ( n ) ; while ( lo <= hi ) { var curr = ( lo * lo * lo + hi * hi * hi ) ; if ( curr == n ) return true ; if ( curr < n ) lo ++ ; else hi -- ; } return false ; } var N = 28 ; if ( sumOfTwoCubes ( N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Generate an N | JavaScript program for the above approach ; Function to generate all prime numbers upto 10 ^ 6 ; Initialize sieve [ ] as 1 ; Iterate over the range [ 2 , N ] ; If current element is non - prime ; Make all multiples of i as 0 ; Function to construct an array A [ ] satisfying the given conditions ; Stores the resultant array ; Stores all prime numbers ; Sieve of Erastosthenes ; Append the integer i if it is a prime ; Indicates current position in list of prime numbers ; Traverse the array arr [ ] ; If already filled with another prime number ; If A [ i ] is not filled but A [ ind ] is filled ; Store A [ i ] = A [ ind ] ; If none of them were filled ; To make sure A [ i ] does not affect other values , store next prime number ; Print the resultant array ; Driver Code ; Function Call
var sieve = Array ( 1000000 ) ; function sieveOfPrimes ( ) { sieve = Array ( 1000000 ) . fill ( 1 ) ; var N = 1000000 ; for ( var i = 2 ; i * i <= N ; i ++ ) { if ( sieve [ i ] == 0 ) continue ; for ( var j = i * i ; j <= N ; j += i ) sieve [ j ] = 0 ; } } function getArray ( arr , N ) { var A = Array ( N ) . fill ( 0 ) ; var v = [ ] ; sieveOfPrimes ( ) ; for ( var i = 2 ; i <= 1e5 ; i ++ ) if ( sieve [ i ] ) v . push ( i ) ; var j = 0 ; for ( var i = 0 ; i < N ; i ++ ) { var ind = arr [ i ] ; if ( A [ i ] != 0 ) continue ; else if ( A [ ind ] != 0 ) A [ i ] = A [ ind ] ; else { var prime = v [ j ++ ] ; A [ i ] = prime ; A [ ind ] = A [ i ] ; } } for ( var i = 0 ; i < N ; i ++ ) { document . write ( A [ i ] + " " ) ; } } var arr = [ 4 , 1 , 2 , 3 , 4 ] ; var N = arr . length ; getArray ( arr , N ) ;
Nth natural number after removing all numbers consisting of the digit 9 | Function to find Nth number in base 9 ; Stores the Nth number ; Iterate while N is greater than 0 ; Update result ; Divide N by 9 ; Multiply p by 10 ; Return result ; Driver code
function findNthNumber ( N ) { let result = 0 ; let p = 1 ; while ( N > 0 ) { result += ( p * ( N % 9 ) ) ; N = parseInt ( N / 9 , 10 ) ; p = p * 10 ; } return result ; } let N = 9 ; document . write ( findNthNumber ( N ) ) ;
Check if an integer is rotation of another given integer | Function to check if the integer A is a rotation of the integer B ; Stores the count of digits in A ; Stores the count of digits in B ; If dig1 not equal to dig2 ; Stores position of first digit ; Stores the first digit ; Rotate the digits of the integer ; If A is equal to B ; If A is equal to the initial value of integer A ; Driver Code
function check ( A , B ) { if ( A == B ) { return 1 ; } let dig1 = Math . floor ( Math . log10 ( A ) + 1 ) ; let dig2 = Math . floor ( Math . log10 ( B ) + 1 ) ; if ( dig1 != dig2 ) { return 0 ; } let temp = A ; while ( true ) { let power = Math . pow ( 10 , dig1 - 1 ) ; let firstdigit = parseInt ( A / power , 10 ) ; A = A - firstdigit * power ; A = A * 10 + firstdigit ; if ( A == B ) { return 1 ; } if ( A == temp ) { return 0 ; } } } let A = 967 , B = 679 ; if ( check ( A , B ) == 1 ) document . write ( " " ) ; else document . write ( " " ) ;
Count of quadruples with product of a pair equal to the product of the remaining pair | Function to count the number of unique quadruples from an array that satisfies the given condition ; Hashmap to store the product of pairs ; Store the count of required quadruples ; Traverse the array arr [ ] and generate all possible pairs ; Store their product ; Pair ( a , b ) can be used to generate 8 unique permutations with another pair ( c , d ) ; Increment um [ prod ] by 1 ; Print the result ; Driver Code
function sameProductQuadruples ( nums , N ) { var umap = new Array ( 10000 ) . fill ( 0 ) ; var res = 0 ; for ( var i = 0 ; i < N ; ++ i ) { for ( var j = i + 1 ; j < N ; ++ j ) { var prod = nums [ i ] * nums [ j ] ; res += 8 * umap [ prod ] ; ++ umap [ prod ] ; } } document . write ( res ) ; } var arr = [ 2 , 3 , 4 , 6 ] ; var N = arr . length ; sameProductQuadruples ( arr , N ) ;
Count ways to place M objects in distinct partitions of N boxes | Javascript implementation of the above Approach ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize Result ; Update x if x >= MOD to avoid multiplication overflow ; If y is odd , multiply x with result ; y = y / 2 ; Change x to x ^ 2 ; Utility function to find the Total Number of Ways ; Number of Even Indexed Boxes ; Number of partitions of Even Indexed Boxes ; Number of ways to distribute objects ; N = number of boxes M = number of distinct objects ; Function call to get Total Number of Ways
var MOD = 1000000007 ; function power ( x , y , p = MOD ) { var res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * 1 * x ) % p ; y = y >> 1 ; x = ( x * 1 * x ) % p ; } return res ; } function totalWays ( N , M ) { var X = parseInt ( N / 2 ) ; var S = ( X * 1 * ( X + 1 ) ) % MOD ; document . write ( power ( S , M , MOD ) << " " ) ; } var N = 5 , M = 2 ; totalWays ( N , M ) ;
Check if a graph constructed from an array based on given conditions consists of a cycle or not | Function to check if the graph constructed from given array contains a cycle or not ; Traverse the array ; If arr [ i ] is less than arr [ i - 1 ] and arr [ i ] ; Given array ; Size of the array
function isCycleExists ( arr , N ) { for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } let arr = [ 1 , 3 , 2 , 4 , 5 ] ; let N = arr . length ; isCycleExists ( arr , N ) ;
Maximize first array element by performing given operations at most K times | Function to maximize the first array element ; Traverse the array ; Initialize cur_val to a [ i ] ; If all operations are not over yet ; If current value is greater than zero ; Incrementing first element of array by 1 ; Decrementing current value of array by 1 ; Decrementing number of operations by i ; If current value is zero , then break ; Print first array element ; Given array ; Size of the array ; Given K ; Prints the maximum possible value of the first array element
function getMax ( arr , N , K ) { for ( i = 1 ; i < N ; i ++ ) { var cur_val = arr [ i ] ; while ( K >= i ) { if ( cur_val > 0 ) { arr [ 0 ] = arr [ 0 ] + 1 ; cur_val = cur_val - 1 ; K = K - i ; } else break ; } } document . write ( arr [ 0 ] ) ; } var arr = [ 1 , 0 , 3 , 2 ] ; var N = arr . length ; var K = 5 ; getMax ( arr , N , K ) ;
Count Non | Function to find the gcd of the two numbers ; Function to find distinct elements in the array by repeatidely inserting the absolute difference of all possible pairs ; Stores largest element of the array ; Traverse the array , arr ; Update max_value ; Stores GCD of array ; Traverse the array , arr ; Update GCDArr ; Stores distinct elements in the array by repeatidely inserting absolute difference of all possible pairs ; Given array arr
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function DistinctValues ( arr , N ) { var max_value = Number . MIN_VALUE ; for ( i = 0 ; i < N ; ++ i ) { max_value = Math . max ( max_value , arr [ i ] ) ; } var GCDArr = arr [ 0 ] ; for ( i = 1 ; i < N ; ++ i ) { GCDArr = gcd ( GCDArr , arr [ i ] ) ; } var answer = ( max_value / GCDArr ) + 1 ; return answer ; } var arr = [ 4 , 12 , 16 , 24 ] ; var N = arr . length ; document . write ( DistinctValues ( arr , N ) ) ;
Minimum row or column swaps required to make every pair of adjacent cell of a Binary Matrix distinct | Function to return number of moves to convert matrix into chessboard ; Size of the matrix ; Traverse the matrix ; Initialize rowSum to count 1 s in row ; Initialize colSum to count 1 s in column ; To store no . of rows to be corrected ; To store no . of columns to be corrected ; Traverse in the range [ 0 , N - 1 ] ; Check if rows is either N / 2 or ( N + 1 ) / 2 and return - 1 ; Check if rows is either N / 2 or ( N + 1 ) / 2 and return - 1 ; Check if N is odd ; Check if column required to be corrected is odd and then assign N - colSwap to colSwap ; Check if rows required to be corrected is odd and then assign N - rowSwap to rowSwap ; Take min of colSwap and N - colSwap ; Take min of rowSwap and N - rowSwap ; Finally return answer ; Given matrix ; Function Call ; Prvar answer
function minSwaps ( b ) { var n = b . length ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( ( b [ 0 ] [ 0 ] ^ b [ 0 ] [ j ] ^ b [ i ] [ 0 ] ^ b [ i ] [ j ] ) == 1 ) { return - 1 ; } } } var rowSum = 0 ; var colSum = 0 ; var rowSwap = 0 ; var colSwap = 0 ; for ( i = 0 ; i < n ; i ++ ) { rowSum += b [ i ] [ 0 ] ; colSum += b [ 0 ] [ i ] ; var cond1 = 0 ; var cond2 = 0 ; if ( b [ i ] [ 0 ] == i % 2 ) cond1 = 1 ; if ( b [ 0 ] [ i ] == i % 2 ) cond2 = 1 ; rowSwap += cond1 ; colSwap += cond2 ; } if ( rowSum != n / 2 && rowSum != ( n + 1 ) / 2 ) return - 1 ; if ( colSum != n / 2 && colSum != ( n + 1 ) / 2 ) return - 1 ; if ( n % 2 == 1 ) { if ( ( colSwap % 2 ) == 1 ) colSwap = n - colSwap ; if ( ( rowSwap % 2 ) == 1 ) rowSwap = n - rowSwap ; } else { colSwap = Math . min ( colSwap , n - colSwap ) ; rowSwap = Math . min ( rowSwap , n - rowSwap ) ; } return ( rowSwap + colSwap ) / 2 ; } var M = [ [ 0 , 1 , 1 , 0 ] , [ 0 , 1 , 1 , 0 ] , [ 1 , 0 , 0 , 1 ] , [ 1 , 0 , 0 , 1 ] ] ; var ans = minSwaps ( M ) ; document . write ( ans ) ;
Minimum number of coins having value equal to powers of 2 required to obtain N | Function to count of set bit in N ; Stores count of set bit in N ; Iterate over the range [ 0 , 31 ] ; If current bit is set ; Update result ; Driver Code
function count_setbit ( N ) { let result = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) { if ( ( ( 1 << i ) & N ) > 0 ) { result ++ ; } } document . write ( result ) ; } let N = 43 ; count_setbit ( N ) ;
Evaluate the expression ( N1 * ( N | Javascript program to implement the above approach ; Function to find the value of the expression ( N ^ 1 * ( N 1 ) ^ 2 * ... * 1 ^ N ) % ( 109 + 7 ) . ; factorial [ i ] : Stores factorial of i ; Base Case for factorial ; Precompute the factorial ; dp [ N ] : Stores the value of the expression ( N ^ 1 * ( N 1 ) ^ 2 * ... * 1 ^ N ) % ( 109 + 7 ) . ; Update dp [ i ] ; Return the answer . ; Driver code ; Function call
let mod = 1000000007 ; function ValOfTheExpression ( n ) { let factorial = new Array ( n + 1 ) ; factorial [ 0 ] = factorial [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { factorial [ i ] = ( ( factorial [ i - 1 ] % mod ) * ( i % mod ) ) % mod ; } let dp = new Array ( n + 1 ) ; dp [ 1 ] = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { dp [ i ] = ( ( dp [ i - 1 ] % mod ) * ( factorial [ i ] % mod ) ) % mod ; } return dp [ n ] ; } let n = 4 ; document . write ( ValOfTheExpression ( n ) ) ;
Chocolate Distribution Problem | Set 2 | FUnction to prlet minimum number of candies required ; Distribute 1 chocolate to each ; Traverse from left to right ; Traverse from right to left ; Initialize sum ; Find total sum ; Return sum ; Given array ; Size of the given array
function minChocolates ( A , N ) { let B = new Array ( N ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { B [ i ] = 1 ; } for ( let i = 1 ; i < N ; i ++ ) { if ( A [ i ] > A [ i - 1 ] ) B [ i ] = B [ i - 1 ] + 1 ; else B [ i ] = 1 ; } for ( let i = N - 2 ; i >= 0 ; i -- ) { if ( A [ i ] > A [ i + 1 ] ) B [ i ] = Math . max ( B [ i + 1 ] + 1 , B [ i ] ) ; else B [ i ] = Math . max ( B [ i ] , 1 ) ; } let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += B [ i ] ; } document . write ( sum + " " ) ; } let A = [ 23 , 14 , 15 , 14 , 56 , 29 , 14 ] ; let N = A . length ; minChocolates ( A , N ) ;
Construct longest possible sequence of unique elements with given LCM | Function to construct an array of unique elements whose LCM is N ; Stores array elements whose LCM is N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If N is divisible by i ; Insert i into newArr [ ] ; If N is not perfect square ; Sort the array newArr [ ] ; Print array elements ; Given N ; Function Call
function constructArrayWithGivenLCM ( N ) { let newArr = new Array ( N ) ; newArr . fill ( 0 ) ; let j = 0 ; for ( let i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { newArr [ j ] = i ; j ++ ; if ( parseInt ( N / i , 10 ) != i ) { newArr [ j ] = parseInt ( N / i , 10 ) ; j ++ ; } } } newArr . sort ( function ( a , b ) { return a - b } ) ; for ( let i = j ; i < N ; i ++ ) { document . write ( newArr [ i ] + " " ) ; } } let N = 12 ; constructArrayWithGivenLCM ( N ) ;
Count numbers from given range having odd digits at odd places and even digits at even places | Function to calculate 5 ^ p ; Stores the result ; Multiply 5 p times ; Return the result ; Function to count all numbers upto N having odd digits at odd places and even digits at even places ; Stores the count ; Stores the digits of N ; Insert the digits of N ; Reverse the vector to arrange the digits from first to last ; Stores count of digits of n ; Stores the count of numbers with i digits ; If the last digit is reached , subtract numbers eceeding range ; Iterate over all the places ; Stores the digit in the pth place ; Stores the count of numbers having a digit greater than x in the p - th position ; Calculate the count of numbers exceeding the range if p is even ; Calculate the count of numbers exceeding the range if p is odd ; Subtract the count of numbers exceeding the range from total count ; If the parity of p and the parity of x are not same ; Add count of numbers having i digits and satisfies the given conditions ; Return the total count of numbers till n ; Function to calculate the count of numbers from given range having odd digits places and even digits at even places ; Count of numbers in range [ L , R ] = Count of numbers till R - ; Count of numbers till ( L - 1 ) ; Driver Code
function getPower ( p ) { var res = 1 ; while ( p -- ) { res *= 5 ; } return res ; } function countNumbersUtil ( N ) { var count = 0 ; var digits = [ ] ; while ( N ) { digits . push ( N % 10 ) ; N = parseInt ( N / 10 ) ; } digits . reverse ( ) ; var D = digits . length ; for ( var i = 1 ; i <= D ; i ++ ) { var res = getPower ( i ) ; if ( i == D ) { for ( var p = 1 ; p <= D ; p ++ ) { var x = digits [ p - 1 ] ; var tmp = 0 ; if ( p % 2 == 0 ) { tmp = ( 5 - parseInt ( x / ( 2 + 1 ) ) ) * getPower ( D - p ) ; } else { tmp = ( 5 - parseInt ( ( x + 1 ) / 2 ) ) * getPower ( D - p ) ; } res -= tmp ; if ( p % 2 != x % 2 ) { break ; } } } count += res ; } return count ; } function countNumbers ( L , R ) { document . write ( ( countNumbersUtil ( R ) - countNumbersUtil ( L - 1 ) ) + " " ) ; } var L = 128 , R = 162 ; countNumbers ( L , R ) ;
Sum of first N natural numbers with alternate signs | Function to find the sum of first N natural numbers with alternate signs ; Stores sum of alternate sign of first N natural numbers ; If is an even number ; Update alternateSum ; If i is an odd number ; Update alternateSum ; Driver Code
function alternatingSumOfFirst_N ( N ) { var alternateSum = 0 ; for ( i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) { alternateSum += - i ; } else { alternateSum += i ; } } return alternateSum ; } var N = 6 ; document . write ( alternatingSumOfFirst_N ( N ) ) ;
Sum of all numbers up to N that are co | Function to return gcd of a and b ; Base Case ; Recursive GCD ; Function to calculate the sum of all numbers till N that are coprime with N ; Stores the resultant sum ; Iterate over [ 1 , N ] ; If gcd is 1 ; Update sum ; Return the final sum ; Given N ; Function Call
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function findSum ( N ) { var sum = 0 ; for ( var i = 1 ; i < N ; i ++ ) { if ( gcd ( i , N ) == 1 ) { sum += i ; } } return sum ; } var N = 5 ; document . write ( findSum ( N ) ) ;
Count all distinct pairs of repeating elements from the array for every array element | Function to print the required count of pairs excluding the current element ; Store the frequency ; Find all the count ; Delete the contribution of each element for equal pairs ; Print the answer ; Given array arr [ ] ; Function Call
function solve ( arr , n ) { var mp = new Map ( ) ; for ( var 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 ) ; } var cnt = 0 ; mp . forEach ( ( value , key ) => { cnt += ( ( value ) * ( value - 1 ) / 2 ) ; } ) ; var ans = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { ans [ i ] = cnt - ( mp . get ( arr [ i ] ) - 1 ) ; } for ( var i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } var arr = [ 1 , 1 , 2 , 1 , 2 ] ; var N = arr . length ; solve ( arr , N ) ;
Count of distinct numbers formed by shuffling the digits of a large number N | Recursive function to return the value of ( x ^ n ) % m ; Base Case ; If N is even ; Else N is odd ; Function to find modular inverse of a number x under modulo m ; Using Fermat 's little theorem ; Function to count of numbers formed by shuffling the digits of a large number N ; Modulo value ; Array to store the factorials upto the maximum value of N ; Store factorial of i at index i ; To store count of occurrence of a digit ; Increment the count of digit occured ; Assign the factorial of length of input ; Multiplying result with the modulo multiplicative inverse of factorial of count of i ; Print the result ; Given number as string ; Function call
function modexp ( x , n , m ) { if ( n == 0 ) { return 1 ; } else if ( n % 2 == 0 ) { return modexp ( ( x * x ) % m , parseInt ( n / 2 , 10 ) , m ) ; } else { return ( x * modexp ( ( x * x ) % m , parseInt ( ( n - 1 ) / 2 , 10 ) , m ) % m ) ; } } function modInverse ( x , m ) { return modexp ( x , m - 2 , m ) ; } function countNumbers ( N ) { let m = 1000000007 ; let factorial = new Array ( 100001 ) ; factorial [ 0 ] = 1 ; for ( let i = 1 ; i < 100001 ; i ++ ) { factorial [ i ] = ( factorial [ i - 1 ] * i ) % m ; } let count = new Array ( 10 ) ; for ( let i = 0 ; i < 10 ; i ++ ) { count [ i ] = 0 ; } let length = N . length ; for ( let i = 0 ; i < length ; i ++ ) count [ N [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let result = factorial [ length ] ; for ( let i = 0 ; i < 10 ; i ++ ) { result = 0 * ( result * modInverse ( factorial [ count [ i ] ] , m ) ) % m + 12 ; } document . write ( result ) ; } let N = " " ; countNumbers ( N ) ;
Find prime factors of Array elements whose sum of exponents is divisible by K | To store the smallest prime factor till 10 ^ 5 ; Function to compute smallest prime factor array ; Initialize the spf array first element ; Marking smallest prime factor for every number to be itself ; Separately marking smallest prime factor for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function that finds minimum operation ; Create a spf [ ] array ; Map created to store the unique prime numbers ; To store the result ; To store minimum operations ; To store every unique prime number ; Erase 1 as a key because it is not a prime number ; First Prime Number ; Frequency is divisible by K then insert primeNum in the result [ ] ; Print the elements if it exists ; Given array arr [ ] ; Given K ; Function Call
var spf = Array ( 10001 ) ; function spf_array ( spf ) { spf [ 1 ] = 1 ; for ( var i = 2 ; i < 1000 ; i ++ ) spf [ i ] = i ; for ( var i = 4 ; i < 1000 ; i += 2 ) spf [ i ] = 2 ; for ( var i = 3 ; i * i < 1000 ; i ++ ) { if ( spf [ i ] == i ) { for ( var j = i * i ; j < 1000 ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } function frequent_prime ( arr , N , K ) { spf_array ( spf ) ; var Hmap = new Map ( ) ; var result = [ ] ; var i = 0 ; var c = 0 ; for ( i = 0 ; i < N ; i ++ ) { var x = arr [ i ] ; while ( x != 1 ) { if ( Hmap . has ( spf [ x ] ) ) Hmap . set ( spf [ x ] , Hmap . get ( spf [ x ] ) + 1 ) else Hmap . set ( spf [ x ] , 1 ) ; x = parseInt ( x / spf [ x ] ) ; } } Hmap . delete ( 1 ) ; Hmap . forEach ( ( value , key ) => { var primeNum = key ; var frequency = value ; if ( frequency % K == 0 ) { result . push ( primeNum ) ; } } ) ; if ( result . length > 0 ) { result . forEach ( it => { document . write ( it + " " ) ; } ) ; } else { document . write ( " " ) ; } } var arr = [ 1 , 4 , 6 ] ; var K = 1 ; var N = arr . length ; frequent_prime ( arr , N , K ) ;
Generate first K multiples of N using Bitwise operators | Function to prvar the first K multiples of N ; Prvar the value of N * i ; Iterate each bit of N and add Math . pow ( 2 , pos ) , where pos is the index of each set bit ; Check if current bit at pos j is fixed or not ; For next set bit ; Driver Code
function Kmultiples ( n , k ) { var a = n ; for ( i = 1 ; i <= k ; i ++ ) { document . write ( n + " " + i + " " + a + " " ) ; var j = 0 ; while ( n >= ( 1 << j ) ) { a += n & ( 1 << j ) ; j ++ ; } } } var N = 16 , K = 7 ; Kmultiples ( N , K ) ;
Least Square Regression Line | Function to calculate b ; sum of array x ; sum of array y ; for sum of product of x and y ; sum of square of x ; Function to find the least regression line ; Finding b ; calculating a ; Printing regression line ; statistical data
function calculateB ( x , y ) { let n = x . length ; let sx = x . reduce ( ( a , b ) => a + b , 0 ) ; let sy = y . reduce ( ( a , b ) => a + b , 0 ) let sxsy = 0 ; let sx2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sxsy += x [ i ] * y [ i ] ; sx2 += x [ i ] * x [ i ] ; } let b = ( n * sxsy - sx * sy ) / ( n * sx2 - sx * sx ) ; return b ; } function leastRegLine ( X , Y ) { let b = calculateB ( X , Y ) ; let n = X . length ; let meanX = X . reduce ( ( a , b ) => a + b , 0 ) / n ; let meanY = Y . reduce ( ( a , b ) => a + b , 0 ) / n ; let a = meanY - b * meanX ; document . write ( " " ) ; document . write ( " " ) ; document . write ( a . toFixed ( 3 ) ) ; document . write ( " " ) ; document . write ( b . toFixed ( 3 ) ) ; document . write ( " " ) ; } let X = [ 95 , 85 , 80 , 70 , 60 ] ; let Y = [ 90 , 80 , 70 , 65 , 60 ] ; leastRegLine ( X , Y ) ;
Count of repeating digits in a given Number | Function that returns the count of repeating digits of the given number ; Initialize a variable to store count of Repeating digits ; Initialize cnt array to store digit count ; Iterate through the digits of N ; Retrieve the last digit of N ; Increase the count of digit ; Remove the last digit of N ; Iterate through the cnt array ; If frequency of digit is greater than 1 ; Increment the count of Repeating digits ; Return count of repeating digit ; Given array arr [ ] ; Function Call
function countRepeatingDigits ( N ) { var res = 0 ; var cnt = Array ( 10 ) . fill ( 0 ) ; while ( N > 0 ) { var rem = N % 10 ; cnt [ rem ] ++ ; N = N / 10 ; } for ( var i = 0 ; i < 10 ; i ++ ) { if ( cnt [ i ] > 1 ) { res ++ ; } } return res ; } var N = 12 ; document . write ( countRepeatingDigits ( N ) ) ;