codeparrot/xlcost-text-to-code huggingface dataset:
Cake Distribution Problem | Function to return the remaining count of cakes ; Sum for 1 cycle ; no . of full cycle and remainder ; Driver Code
function cntCakes ( n , m ) { let sum = ( n * ( n + 1 ) ) / 2 ; let quo = m / sum ; let rem = m % sum ; let ans = m - quo * sum + 6 ; let x = ( - 1 + Math . pow ( ( 8 * rem ) + 1 , 0.5 ) ) ; ans = ans - x * ( x + 1 ) / 2 ; return parseInt ( ans , 10 ) ; } let n = 3 ; let m = 8 ; let ans = cntCakes ( n , m ) ; document . write ( ans ) ;
Find the number of squares inside the given square grid | Function to return the number of squares inside an n * n grid ; Driver code
function cntSquares ( n ) { return n * ( n + 1 ) * ( 2 * n + 1 ) / 6 ; } document . write ( cntSquares ( 4 ) ) ;
Find all palindrome numbers of given digits | Function to return the reverse of num ; Function that returns true if num is palindrome ; If the number is equal to the reverse of it then it is a palindrome ; Function to print all the d - digit palindrome numbers ; Smallest and the largest d - digit numbers ; Starting from the smallest d - digit number till the largest ; If the current number is palindrome ; Driver code
function reverse ( num ) { let rev = 0 ; while ( num > 0 ) { rev = rev * 10 + num % 10 ; num = parseInt ( num / 10 , 10 ) ; } return rev ; } function isPalindrome ( num ) { if ( num == reverse ( num ) ) return true ; return false ; } function printPalindromes ( d ) { if ( d <= 0 ) return ; let smallest = Math . pow ( 10 , d - 1 ) ; let largest = Math . pow ( 10 , d ) - 1 ; for ( let i = smallest ; i <= largest ; i ++ ) { if ( isPalindrome ( i ) ) document . write ( i + " " ) ; } } let d = 2 ; printPalindromes ( d ) ;
Find X and Y intercepts of a line passing through the given points | Function to find the X and Y intercepts of the line passing through the given points ; if line is parallel to y axis ; x - intercept will be p [ 0 ] ; y - intercept will be infinity ; if line is parallel to x axis ; x - intercept will be infinity ; y - intercept will be p [ 1 ] ; Slope of the line ; y = mx + c in where c is unknown Use any of the given point to find c ; For finding the x - intercept put y = 0 ; For finding the y - intercept put x = 0 ; Driver code
function getXandYintercept ( P , Q ) { let a = P [ 1 ] - Q [ 1 ] ; let b = P [ 0 ] - Q [ 0 ] ; if ( b == 0 ) { document . write ( P [ 0 ] + " " ) ; document . write ( " " + " " ) ; return ; } if ( a == 0 ) { document . write ( " " + " " ) ; document . write ( P [ 1 ] + " " ) ; return ; } let m = a / ( b * 1.0 ) ; let x = P [ 0 ] ; let y = P [ 1 ] ; let c = y - m * x ; y = 0 ; let r = ( y - c ) / ( m * 1.0 ) ; document . write ( r + " " ) ; x = 0 ; y = parseInt ( m * x + c , 10 ) ; document . write ( c . toFixed ( 11 ) + " " ) ; } let p1 = [ 5 , 2 ] ; let p2 = [ 2 , 7 ] ; getXandYintercept ( p1 , p2 ) ;
Minimum number of moves to reach N starting from ( 1 , 1 ) | Function to return the minimum number of moves required to reach the cell containing N starting from ( 1 , 1 ) ; To store the required answer ; For all possible values of divisors ; If i is a divisor of n ; Get the moves to reach n ; Return the required answer ; Driver code
function min_moves ( n ) { let ans = Number . MAX_VALUE ; for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { ans = Math . min ( ans , i + parseInt ( n / i , 10 ) - 2 ) ; } } return ans ; } let n = 10 ; document . write ( min_moves ( n ) ) ;
Minimum possible value of ( i * j ) % 2019 | JavaScript implementation of the approach ; Function to return the minimum possible value of ( i * j ) % 2019 ; If we can get a number divisible by 2019 ; Find the minimum value by running nested loops ; Driver code
let MOD = 2019 ; function min_modulo ( l , r ) { if ( r - l >= MOD ) return 0 ; else { let ans = MOD - 1 ; for ( let i = l ; i <= r ; i ++ ) { for ( let j = i + 1 ; j <= r ; j ++ ) { ans = Math . min ( ans , ( i * j ) % MOD ) ; } } return ans ; } } let l = 2020 , r = 2040 ; document . write ( min_modulo ( l , r ) ) ;
Represent ( 2 / N ) as the sum of three distinct positive integers of the form ( 1 / m ) | Function to find the required fractions ; Base condition ; For N > 1 ; Driver code
function find_numbers ( N ) { if ( N == 1 ) { document . write ( - 1 ) ; } else { document . write ( N + " " + ( N + 1 ) + " " + ( N * ( N + 1 ) ) ) ; } } var N = 5 ; find_numbers ( N ) ;
Count the pairs in an array such that the difference between them and their indices is equal | Function to return the count of all valid pairs ; To store the frequencies of ( arr [ i ] - i ) ; To store the required count ; If cnt is the number of elements whose difference with their index is same then ( ( cnt * ( cnt - 1 ) ) / 2 ) such pairs are possible ; Driver code
function countPairs ( arr , n ) { var map = { } ; for ( var i = 0 ; i < n ; i ++ ) map [ arr [ i ] - i ] = 0 ; for ( var i = 0 ; i < n ; i ++ ) map [ arr [ i ] - i ] ++ ; var res = 0 ; for ( const [ key , value ] of Object . entries ( map ) ) { var cnt = value ; res += ( cnt * ( cnt - 1 ) ) / 2 ; } return res ; } var arr = [ 1 , 5 , 6 , 7 , 9 ] ; var n = arr . length ; document . write ( countPairs ( arr , n ) ) ;
Minimum possible number with the given operation | Function to return the minimum possible integer that can be obtained from the given integer after performing the given operations ; For every digit ; Digits less than 5 need not to be changed as changing them will lead to a larger number ; The resulting integer cannot have leading zero ; Driver code
function minInt ( str ) { for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] . charCodeAt ( ) >= ( ' ' ) . charCodeAt ( ) ) { str [ i ] = String . fromCharCode ( ( ( ' ' ) . charCodeAt ( ) - ( str [ i ] ) . charCodeAt ( ) ) + ( ' ' ) . charCodeAt ( ) ) ; } } if ( str [ 0 ] == ' ' ) str [ 0 ] = ' ' ; let s = str . join ( " " ) ; return s ; } let str = " " ; document . write ( minInt ( str . split ( ' ' ) ) ) ;
Reduce N to 1 with minimum number of given operations | Function to return the minimum number of given operations required to reduce n to 1 ; To store the count of operations ; To store the digit ; If n is already then no operation is required ; Extract all the digits except the first digit ; Store the maximum of that digits ; for each digit ; First digit ; Add the value to count ; Driver code
function minOperations ( n ) { var count = 0 ; var d = 0 ; if ( n == 1 ) return 0 ; while ( n > 9 ) { d = Math . max ( n % 10 , d ) ; n /= 10 ; count += 10 ; } d = Math . max ( d , n - 1 ) ; count += Math . abs ( d ) ; return count - 1 ; } var n = 240 ; document . write ( minOperations ( n ) ) ;
Find the largest number that can be formed by changing at most K digits | Function to return the maximum number that can be formed by changing at most k digits in str ; For every digit of the number ; If no more digits can be replaced ; If current digit is not already 9 ; Replace it with 9 ; One digit has been used ; Driver code
function findMaximumNum ( str , n , k ) { for ( var i = 0 ; i < n ; i ++ ) { if ( k < 1 ) break ; if ( str [ i ] !== " " ) { str [ i ] = " " ; k -- ; } } return str . join ( " " ) ; } var str = " " ; var n = str . length ; var k = 3 ; document . write ( findMaximumNum ( str . split ( " " ) , n , k ) ) ;
Number of occurrences of a given angle formed using 3 vertices of a n | Function that calculates occurrences of given angle that can be created using any 3 sides ; Maximum angle in a regular n - gon is equal to the interior angle If the given angle is greater than the interior angle then the given angle cannot be created ; The given angle times n should be divisible by 180 else it cannot be created ; Initialise answer ; Calculate the frequency of given angle for each vertex ; Multiply answer by frequency . ; Multiply answer by the number of vertices . ; Driver code
function solve ( ang , n ) { if ( ( ang * n ) > ( 180 * ( n - 2 ) ) ) { return 0 ; } else if ( ( ang * n ) % 180 != 0 ) { return 0 ; } var ans = 1 ; var freq = ( ang * n ) / 180 ; ans = ans * ( n - 1 - freq ) ; ans = ans * n ; return ans ; } var ang = 90 , n = 4 ; document . write ( solve ( ang , n ) ) ;
Find third number such that sum of all three number becomes prime | Function that will check whether number is prime or not ; Function to print the 3 rd number ; If the sum is odd ; If sum is not prime ; Driver code
function prime ( n ) { for ( let i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } function thirdNumber ( a , b ) { let sum = 0 , temp = 0 ; sum = a + b ; temp = 1 ; if ( ( sum & 1 ) != 0 ) { temp = 2 ; } while ( ! prime ( sum + temp ) ) { temp += 2 ; } document . write ( temp ) ; } let a = 3 , b = 5 ; thirdNumber ( a , b ) ;
Total ways of selecting a group of X men from N men with or without including a particular man | Function to return the value of nCr ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the count of ways ; Driver code
function nCr ( n , r ) { let ans = 1 ; for ( let i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } function total_ways ( N , X ) { return ( nCr ( N - 1 , X - 1 ) + nCr ( N - 1 , X ) ) ; } let N = 5 , X = 3 ; document . write ( total_ways ( N , X ) ) ;
Compute the maximum power with a given condition | Function to return the largest power ; If n is greater than given M ; If n == m ; Checking for the next power ; Driver Code
function calculate ( n , k , m , power ) { if ( n > m ) { if ( power == 0 ) return 0 ; else return power - 1 ; } else if ( n == m ) return power ; else return calculate ( n * k , k , m , power + 1 ) ; } var N = 1 , K = 2 , M = 5 ; document . write ( calculate ( N , K , M , 0 ) ) ;
Program to find the number from given holes | Function that will find out the number ; If number of holes equal 0 then return 1 ; If number of holes equal 0 then return 0 ; If number of holes is more than 0 or 1. ; If number of holes is odd ; Driver code ; Calling Function
function printNumber ( holes ) { if ( holes == 0 ) document . write ( " " ) ; else if ( holes == 1 ) document . write ( " " ) ; else { let rem = 0 , quo = 0 ; rem = holes % 2 ; quo = parseInt ( holes / 2 , 10 ) ; if ( rem == 1 ) document . write ( " " ) ; for ( let i = 0 ; i < quo ; i ++ ) document . write ( " " ) ; } } let holes = 3 ; printNumber ( holes ) ;
Minimum cost to make all array elements equal | Function to return the minimum cost to make each array element equal ; To store the count of even numbers present in the array ; To store the count of odd numbers present in the array ; Iterate through the array and find the count of even numbers and odd numbers ; Driver code
function minCost ( arr , n ) { var count_even = 0 ; var count_odd = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) count_even ++ ; else count_odd ++ ; } return Math . min ( count_even , count_odd ) ; } var arr = [ 2 , 4 , 3 , 1 , 5 ] ; var n = arr . length ; document . write ( minCost ( arr , n ) ) ;
Number of Subarrays with positive product | Function to return the count of subarrays with negative product ; Replace current element with 1 if it is positive else replace it with - 1 instead ; Take product with previous element to form the prefix product ; Count positive and negative elements in the prefix product array ; Return the required count of subarrays ; Function to return the count of subarrays with positive product ; Total subarrays possible ; Count to subarrays with negative product ; Return the count of subarrays with positive product ; Driver code
function negProdSubArr ( arr , n ) { let positive = 1 , negative = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; if ( i > 0 ) arr [ i ] *= arr [ i - 1 ] ; if ( arr [ i ] == 1 ) positive ++ ; else negative ++ ; } return ( positive * negative ) ; } function posProdSubArr ( arr , n ) { let total = parseInt ( ( n * ( n + 1 ) ) / 2 ) ; let cntNeg = negProdSubArr ( arr , n ) ; return ( total - cntNeg ) ; } let arr = [ 5 , - 4 , - 3 , 2 , - 5 ] ; let n = arr . length ; document . write ( posProdSubArr ( arr , n ) ) ;
Find the XOR of first N Prime Numbers | Javascript implementation of the approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Set all multiples of p to non - prime ; Function to return the xor of 1 st N prime numbers ; Count of prime numbers ; XOR of prime numbers ; If the number is prime xor it ; Increment the count ; Get to the next number ; Create the sieve ; Find the xor of 1 st n prime numbers
let MAX = 10000 ; let prime = new Array ( MAX + 1 ) ; function SieveOfEratosthenes ( ) { let i ; for ( i = 0 ; i < MAX + 1 ; i ++ ) { prime [ i ] = true ; } prime [ 1 ] = false ; for ( let p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } function xorFirstNPrime ( n ) { let count = 0 , num = 1 ; let xorVal = 0 ; while ( count < n ) { if ( prime [ num ] ) { xorVal ^= num ; count ++ ; } num ++ ; } return xorVal ; } SieveOfEratosthenes ( ) ; let n = 4 ; document . write ( xorFirstNPrime ( n ) ) ;
Sum of all natural numbers from L to R ( for large values of L and R ) | Javascript implementation of the approach ; Value of inverse modulo 2 with 10 ^ 9 + 7 ; Function to return num % 1000000007 where num is a large number ; Initialize result ; One by one process all the digits of string ' num ' ; Function to return the sum of the longegers from the given range modulo 1000000007 ; a stores the value of L modulo 10 ^ 9 + 7 ; b stores the value of R modulo 10 ^ 9 + 7 ; l stores the sum of natural numbers from 1 to ( a - 1 ) ; r stores the sum of natural numbers from 1 to b ; If the result is negative ; Driver code
let mod = 1000000007 ; let inv2 = 500000004 ; function modulo ( num ) { let res = 0 ; for ( let i = 0 ; i < num . length ; i ++ ) res = ( res * 10 + num [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) % mod ; return res ; } function findSum ( L , R ) { let a , b , l , r , ret ; a = modulo ( L ) ; b = modulo ( R ) ; l = ( ( a * ( a - 1 ) ) % mod * inv2 ) % mod ; r = ( ( b * ( b + 1 ) ) % mod * inv2 ) % mod ; ret = ( r % mod - l % mod ) ; if ( ret < 0 ) ret = ret + mod ; else ret = ret % mod - 6 ; return ret ; } let L = " " ; let R = " " ; document . write ( findSum ( L , R ) ) ;
Maximum subsequence sum such that all elements are K distance apart | Function to return the maximum subarray sum for the array { a [ i ] , a [ i + k ] , a [ i + 2 k ] , ... } ; Function to return the sum of the maximum required subsequence ; To store the result ; Run a loop from 0 to k ; Find the maximum subarray sum for the array { a [ i ] , a [ i + k ] , a [ i + 2 k ] , ... } ; Return the maximum value ; Driver code
function maxSubArraySum ( a , n , k , i ) { let max_so_far = Number . MIN_VALUE , max_ending_here = 0 ; while ( i < n ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; i += k ; } return max_so_far ; } function find ( arr , n , k ) { let maxSum = 0 ; for ( let i = 0 ; i <= Math . min ( n , k ) ; i ++ ) { let sum = 0 ; maxSum = Math . max ( maxSum , maxSubArraySum ( arr , n , k , i ) ) ; } return maxSum ; } let arr = [ 2 , - 3 , - 1 , - 1 , 2 ] ; let n = arr . length ; let k = 2 ; document . write ( find ( arr , n , k ) ) ;
Generate N integers satisfying the given conditions | Javascript implementation of the approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Set all multiples of p to non - prime ; Function to find the first n odd prime numbers ; To store the current count of prime numbers ; Starting with 3 as 2 is an even prime number ; If i is prime ; Print i and increment count ; Create the sieve
const MAX = 1000000 ; let prime = new Array ( MAX + 1 ) . fill ( true ) ; function SieveOfEratosthenes ( ) { prime [ 1 ] = false ; for ( let p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } function solve ( n ) { let count = 0 ; for ( let i = 3 ; count < n ; i ++ ) { if ( prime [ i ] ) { document . write ( i + " " ) ; count ++ ; } } } SieveOfEratosthenes ( ) ; let n = 6 ; solve ( n ) ;
Probability that a N digit number is palindrome | Find the probability that a n digit number is palindrome ; Denominator ; Assign 10 ^ ( floor ( n / 2 ) ) to denominator ; Display the answer ; Driver code
function solve ( n ) { let n_2 = parseInt ( n / 2 , 10 ) ; let den ; den = " " ; while ( n_2 -- > 0 ) den += ' ' ; document . write ( 1 + " " + den + " " ) ; } let N = 5 ; solve ( N ) ;
Ways to choose balls such that at least one ball is chosen | javascript implementation of the approach ; Function to return the count of ways to choose the balls ; Calculate ( 2 ^ n ) % MOD ; Subtract the only where no ball was chosen ; Driver code
MOD = 1000000007 ; function countWays ( n ) { var ans = 1 ; for ( i = 0 ; i < n ; i ++ ) { ans *= 2 ; ans %= MOD ; } return ( ( ans - 1 + MOD ) % MOD ) ; } var n = 3 ; document . write ( countWays ( n ) ) ;
Minimize the sum of the array according the given condition | Function to return the minimum sum ; sort the array to find the minimum element ; finding the number to divide ; Checking to what instance the sum has decreased ; getting the max difference ; Driver Code
function findMin ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; arr . sort ( ) ; let min = arr [ 0 ] ; let max = 0 ; for ( let i = n - 1 ; i >= 1 ; i -- ) { let num = arr [ i ] ; let total = num + min ; let j ; for ( j = 2 ; j <= num ; j ++ ) { if ( num % j == 0 ) { let d = j ; let now = parseInt ( num / d ) + ( min * d ) ; let reduce = total - now ; if ( reduce > max ) max = reduce ; } } } document . write ( sum - max ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; findMin ( arr , n ) ;
Print all the permutation of length L using the elements of an array | Iterative | Convert the number to Lth base and print the sequence ; Sequence is of length L ; Print the ith element of sequence ; Print all the permuataions ; There can be ( len ) ^ l permutations ; Convert i to len th base ; Driver code ; function call
function convert_To_Len_th_base ( n , arr , len , L ) { for ( i = 0 ; i < L ; i ++ ) { document . write ( parseInt ( arr [ n % len ] ) ) ; n = parseInt ( n / len ) ; } document . write ( " " ) ; } function print ( arr , len , L ) { for ( var i = 0 ; i < parseInt ( Math . pow ( len , L ) ) ; i ++ ) { convert_To_Len_th_base ( i , arr , len , L ) ; } } var arr = [ 1 , 2 , 3 ] ; var len = arr . length ; var L = 2 ; print ( arr , len , L ) ;
Number of possible permutations when absolute difference between number of elements to the right and left are given | Function to find the number of permutations possible of the original array to satisfy the given absolute differences ; To store the count of each a [ i ] in a map ; if n is odd ; check the count of each whether it satisfy the given criteria or not ; there is only 1 way for middle element . ; for others there are 2 ways . ; now find total ways ; When n is even . ; there will be no middle element so for each a [ i ] there will be 2 ways ; Driver Code
function totalways ( arr , n ) { var cnt = { } ; for ( var i = 0 ; i < n ; i ++ ) { if ( cnt . hasOwnProperty ( arr [ i ] ) ) { cnt [ arr [ i ] ] = cnt [ arr [ i ] ] + 1 ; } else { cnt [ arr [ i ] ] = 1 ; } } if ( n % 2 === 1 ) { var start = 0 , endd = n - 1 ; for ( var i = start ; i <= endd ; i = i + 2 ) { if ( i === 0 ) { if ( cnt [ i ] !== 1 ) { return 0 ; } } else { if ( cnt [ i ] !== 2 ) { return 0 ; } } } var ways = 1 ; start = 2 ; endd = n - 1 ; for ( var i = start ; i <= endd ; i = i + 2 ) { ways = ways * 2 ; } return ways ; } else if ( n % 2 === 0 ) { var start = 1 , endd = n - 1 ; for ( var i = 1 ; i <= endd ; i = i + 2 ) { if ( cnt [ i ] !== 2 ) return 0 ; } var ways = 1 ; for ( var i = start ; i <= endd ; i = i + 2 ) { ways = ways * 2 ; } return ways ; } return - 2147483648 ; } var N = 5 ; var arr = [ 2 , 4 , 4 , 0 , 2 ] ; document . write ( totalways ( arr , N ) ) ;
Proizvolov 's Identity | Function to implement proizvolov 's identity ; According to proizvolov 's identity ; Driver code ; Function call
function proizvolov ( a , b , n ) { return n * n ; } let a = [ 1 , 5 , 6 , 8 , 10 ] , b = [ 9 , 7 , 4 , 3 , 2 ] ; let n = a . length ; document . write ( proizvolov ( a , b , n ) ) ;
Find the ln ( X ) and log10X with the help of expansion | Function to calculate ln x using expansion ; Terminating value of the loop can be increased to improve the precision ; Function to calculate log10 x ; Driver Code ; setprecision ( 3 ) is used to display the output up to 3 decimal places
function calculateLnx ( n ) { let num , mul , cal , sum = 0 ; num = ( n - 1 ) / ( n + 1 ) ; for ( let i = 1 ; i <= 1000 ; i ++ ) { mul = ( 2 * i ) - 1 ; cal = Math . pow ( num , mul ) ; cal = cal / mul ; sum = sum + cal ; } sum = 2 * sum ; return sum ; } function calculateLogx ( lnx ) { return ( lnx / 2.303 ) ; } let lnx , logx , n = 5 ; lnx = calculateLnx ( n ) ; logx = calculateLogx ( lnx ) ; document . write ( " " + n + " " + lnx + " " ) ; document . write ( " " + n + " " + logx + " " ) ;
Find the sum of elements of the Matrix generated by the given rules | Function to return the required sum ; To store the sum ; For every row ; Update the sum as A appears i number of times in the current row ; Update A for the next row ; Return the sum ; Driver code
function sum ( A , B , R ) { let sum = 0 ; for ( let i = 1 ; i <= R ; i ++ ) { sum = sum + ( i * A ) ; A = A + B ; } return sum ; } let A = 5 , B = 3 , R = 3 ; document . write ( sum ( A , B , R ) ) ;
Count total set bits in all numbers from 1 to n | Set 2 | Function to return the sum of the count of set bits in the integers from 1 to n ; Ignore 0 as all the bits are unset ; To store the powers of 2 ; To store the result , it is initialized with n / 2 because the count of set least significant bits in the integers from 1 to n is n / 2 ; Loop for every bit required to represent n ; Total count of pairs of 0 s and 1 s ; totalPairs / 2 gives the complete count of the pairs of 1 s Multiplying it with the current power of 2 will give the count of 1 s in the current bit ; If the count of pairs was odd then add the remaining 1 s which could not be groupped together ; Next power of 2 ; Return the result ; Driver code
function countSetBits ( n ) { n ++ ; var powerOf2 = 2 ; var cnt = n / 2 ; while ( powerOf2 <= n ) { var totalPairs = n / powerOf2 ; cnt += ( totalPairs / 2 ) * powerOf2 ; cnt += ( totalPairs % 2 == 1 ) ? ( n % powerOf2 ) : 0 ; powerOf2 <<= 1 ; } return cnt ; } var n = 14 ; document . write ( countSetBits ( n ) ) ;
Find the height of a right | Function to return the height of the right - angled triangle whose area is X times its base ; Driver code
function getHeight ( X ) { return ( 2 * X ) ; } var X = 35 ; document . write ( getHeight ( X ) ) ;
Find sum of inverse of the divisors when sum of divisors and the number is given | Function to return the sum of inverse of divisors ; Calculating the answer ; Return the answer ; Driver code ; Function call
function SumofInverseDivisors ( N , Sum ) { let ans = ( Sum ) * 1.0 / ( N ) ; return ans ; } let N = 9 ; let Sum = 13 ; document . write ( SumofInverseDivisors ( N , Sum ) . toFixed ( 2 ) ) ;
Number of triplets such that each value is less than N and each pair sum is a multiple of K | Function to return the number of triplets ; Initializing the count array ; Storing the frequency of each modulo class ; If K is odd ; If K is even ; Driver Code ; Function Call
function NoofTriplets ( N , K ) { let cnt = Array ( K ) ; for ( let i = 0 ; i < K ; i ++ ) cnt [ i ] = 0 ; for ( let i = 1 ; i <= N ; i += 1 ) { cnt [ i % K ] += 1 ; } if ( K & 1 ) return cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] ; else { return ( cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] + cnt [ K / 2 ] * cnt [ K / 2 ] * cnt [ K / 2 ] ) ; } } let N = 3 ; let K = 2 ; document . write ( NoofTriplets ( N , K ) ) ;