codeparrot/xlcost-text-to-code huggingface dataset:
Check whether a number has exactly three distinct factors or not | Utility function to check whether a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check whether given number has three distinct factors or not ; Find square root of number ; Check whether number is perfect square or not ; If number is perfect square , check whether square root is prime or not ; Driver code
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function isThreeDisctFactors ( n ) { let sq = parseInt ( Math . sqrt ( n ) ) ; if ( sq * sq != n ) return false ; return isPrime ( sq ) ? true : false ; } let num = 9 ; if ( isThreeDisctFactors ( num ) ) document . write ( " " ) ; else document . write ( " " ) ; num = 15 ; if ( isThreeDisctFactors ( num ) ) document . write ( " " ) ; else document . write ( " " ) ; num = 12397923568441 ; if ( isThreeDisctFactors ( num ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the last digit when factorial of A divides factorial of B | Function which computes the last digit of resultant of B ! / A ! ; if ( A == B ) If A = B , B ! = A ! and B ! / A ! = 1 ; If difference ( B - A ) >= 5 , answer = 0 ; If none of the conditions are true , we iterate from A + 1 to B and multiply them . We are only concerned for the last digit , thus we take modulus of 10 ; driver function
function computeLastDigit ( A , B ) { let variable = 1 ; return 1 ; else if ( ( B - A ) >= 5 ) return 0 ; else { for ( let i = A + 1 ; i <= B ; i ++ ) variable = ( variable * ( i % 10 ) ) % 10 ; return variable % 10 ; } } document . write ( computeLastDigit ( 2632 , 2634 ) ) ;
Program for sum of arithmetic series | Function to find sum of series . ; Driver function
function sumOfAP ( a , d , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum = sum + a ; a = a + d ; } return sum ; } let n = 20 ; let a = 2.5 , d = 1.5 ; document . write ( sumOfAP ( a , d , n ) ) ;
Product of factors of number | function to product the factors ; If factors are equal , multiply only once ; Otherwise multiply both ; Driver Code
function multiplyFactors ( n ) { let M = 1000000007 ; let i ; prod = 1 ; for ( i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) prod = ( prod * i ) % M ; else { prod = ( prod * i ) % M ; prod = ( prod * n / i ) % M ; } } } return prod ; } n = 12 ; document . write ( multiplyFactors ( n ) ) ;
Product of factors of number | Javascript program to calculate product of factors of number ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; function to count the factors ; If factors are equal , count only once ; Otherwise count both ; Calculate product of factors ; If numFactor is odd return power ( n , numFactor / 2 ) * sqrt ( n ) ; driver function
let M = 1000000007 ; function power ( x , y ) { let res = 1 ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % M ; y = ( y >> 1 ) % M ; x = ( x * x ) % M ; } return res ; } function countFactors ( n ) { let count = 0 ; for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) count ++ ; else count += 2 ; } } return count ; } function multiplyFactors ( n ) { let numFactor = countFactors ( n ) ; let product = power ( n , numFactor / 2 ) ; if ( numFactor % 2 == 1 ) product = ( product * Math . sqrt ( n ) ) % M ; return product ; } let n = 12 ; document . write ( multiplyFactors ( n ) ) ;
Decimal representation of given binary string is divisible by 10 or not | function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's position  from the right ; according to the digit 's  position, obtain the last  digit of the applicable  perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; driver function
function isDivisibleBy10 ( bin ) { let n = bin . length ; if ( bin [ n - 1 ] == ' ' ) return false ; let sum = 0 ; for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( bin [ i ] == ' ' ) { let posFromRight = n - i - 1 ; if ( posFromRight % 4 == 1 ) sum = sum + 2 ; else if ( posFromRight % 4 == 2 ) sum = sum + 4 ; else if ( posFromRight % 4 == 3 ) sum = sum + 8 ; else if ( posFromRight % 4 == 0 ) sum = sum + 6 ; } } if ( sum % 10 == 0 ) return true ; return false ; } let bin = " " ; if ( isDivisibleBy10 ( bin ) ) document . write ( " " ) ; else document . write ( " " ) ;
Tribonacci Numbers | A space optimized based Javascript program to print first n Tribonacci numbers . ; Initialize first three numbers ; Loop to add previous three numbers for each number starting from 3 and then assign first , second , third to second , third , and curr to third respectively ; Driver code
function printTrib ( n ) { if ( n < 1 ) return ; let first = 0 , second = 0 ; let third = 1 ; document . write ( first + " " ) ; if ( n > 1 ) document . write ( second + " " ) ; if ( n > 2 ) document . write ( second + " " ) ; for ( let i = 3 ; i < n ; i ++ ) { let curr = first + second + third ; first = second ; second = third ; third = curr ; document . write ( curr + " " ) ; } } let n = 10 ; printTrib ( n ) ;
Prime Number of Set Bits in Binary Representation | Set 2 | Function to create an array of prime numbers upto number ' n ' ; 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 ; Update all multiples of p ; Append all the prime numbers to the list ; Utility function to count the number of set bits ; Driver program ; Here prime numbers are checked till the maximum number of bits possible because that the maximum bit sum possible is the number of bits itself .
function SieveOfEratosthenes ( n ) { let prime = new Array ( n + 1 ) . fill ( true ) ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) for ( let i = p * 2 ; i < n + 1 ; i += p ) prime [ i ] = false ; } let lis = new Array ( ) ; for ( let p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) lis . push ( p ) ; return lis ; } function setBits ( n ) { let cnt = 0 ; while ( n ) { if ( n & 1 ) cnt ++ ; n >>= 1 ; } ; return cnt ; } let x = 4 ; let y = 8 ; let count = 0 ; let primeArr = SieveOfEratosthenes ( Math . ceil ( Math . log ( y , 2 ) ) ) ; for ( let i = x ; i < y + 1 ; i ++ ) { let temp = setBits ( i ) ; if ( primeArr . includes ( temp ) ) count += 1 ; } document . write ( count ) ;
Count trailing zeroes present in binary representation of a given number using XOR | Function to print count of trailing zeroes present in binary representation of N ; Count set bits in ( N ^ ( N - 1 ) ) ; If res < 0 , return 0 ; Driver Code ; Function call to print the count of trailing zeroes in the binary representation of N
function countTrailingZeroes ( N ) { let res = parseInt ( Math . log ( N ^ ( N - 1 ) ) / Math . log ( 2 ) ) ; return res >= 0 ? res : 0 ; } let N = 12 ; document . write ( countTrailingZeroes ( N ) ) ;
Maximum sum of Bitwise XOR of elements with their respective positions in a permutation of size N | Function to calculate the score ; Stores the possible score for the current permutation ; Traverse the permutation array ; Return the final score ; Function to generate all the possible permutation and get the max score ; If arr [ ] length is equal to N process the permutation ; Generating the permutations ; If the current element is chosen ; Mark the current element as true ; Recursively call for next possible permutation ; Backtracking ; Return the ans ; Driver Code ; Stores the permutation ; To display the result
function calcScr ( arr ) { let ans = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) ans += ( i ^ arr [ i ] ) ; return ans ; } function getMax ( arr , ans , chosen , N ) { if ( arr . length == N ) { ans = Math . max ( ans , calcScr ( arr ) ) ; return ans ; } for ( let i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) continue ; chosen [ i ] = true ; arr . push ( i ) ; ans = getMax ( arr , ans , chosen , N ) ; chosen [ i ] = false ; arr . pop ( ) ; } return ans ; } let N = 2 ; let arr = [ ] ; let ans = - 1 ; let chosen = new Array ( N ) . fill ( false ) ; ans = getMax ( arr , ans , chosen , N ) ; document . write ( ans + " " ) ;
Additive Congruence method for generating Pseudo Random Numbers | Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the additive congruential method ; Seed value ; Modulus parameter ; Increment term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Print the generated random numbers
function additiveCongruentialMethod ( Xo , m , c , randomNums , noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( let i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] + c ) % m ; } } let Xo = 3 ; let m = 15 ; let c = 2 ; let noOfRandomNums = 20 ; let randomNums = new Array ( noOfRandomNums ) . fill ( 0 ) ; additiveCongruentialMethod ( Xo , m , c , randomNums , noOfRandomNums ) ; for ( let i = 0 ; i < noOfRandomNums ; i ++ ) { document . write ( randomNums [ i ] + " " ) ; }
Second decagonal numbers | Function to find N - th term in the series ; Driver Code
function findNthTerm ( n ) { document . write ( n * ( 4 * n + 3 ) ) ; } let N = 4 ; findNthTerm ( N ) ;
65537 | Function to find the nth 65537 - gon Number ; Driver code
function gonNum65537 ( n ) { return ( 65535 * n * n - 65533 * n ) / 2 ; } let n = 3 ; document . write ( gonNum65537 ( n ) ) ;
Hexacontatetragon numbers | Function to find the nth Hexacontatetragon number ; Driver code
function HexacontatetragonNum ( n ) { return ( 62 * n * n - 60 * n ) / 2 ; } let n = 3 ; document . write ( HexacontatetragonNum ( n ) ) ;
Icosikaipentagon Number | Function to find the N - th icosikaipentagon Number ; Driver code
function icosikaipentagonNum ( N ) { return parseInt ( ( 23 * N * N - 21 * N ) / 2 ) ; } let n = 3 ; document . write ( " " + icosikaipentagonNum ( n ) ) ;
Chiliagon Number | Finding the nth chiliagon Number ; Driver code
function chiliagonNum ( n ) { return ( 998 * n * n - 996 * n ) / 2 ; } let n = 3 ; document . write ( " " + chiliagonNum ( n ) ) ;
Pentacontagon number | Finding the nth pentacontagon Number ; Driver code
function pentacontagonNum ( n ) { return ( 48 * n * n - 46 * n ) / 2 ; } let n = 3 ; document . write ( " " + pentacontagonNum ( n ) ) ;
Array value by repeatedly replacing max 2 elements with their absolute difference | function that return last value of array ; Build a binary max_heap . ; For max 2 elements ; Iterate until queue is not empty ; if only 1 element is left ; return the last remaining value ; check that difference is non zero ; finally return 0 ; Driver Code
function lastElement ( arr ) { let pq = [ ] ; for ( let i = 0 ; i < arr . length ; i ++ ) { pq . push ( arr [ i ] ) ; } let m1 , m2 ; while ( pq . length ) { if ( pq . length == 1 ) return pq [ pq . length - 1 ] ; pq . sort ( ( a , b ) => a - b ) m1 = pq [ pq . length - 1 ] ; pq . pop ( ) ; m2 = pq [ pq . length - 1 ] ; pq . pop ( ) ; if ( m1 != m2 ) pq . push ( m1 - m2 ) ; } return 0 ; } let arr = [ 2 , 7 , 4 , 1 , 8 , 1 , 1 ] ; document . write ( lastElement ( arr ) + " " ) ;
Number formed by adding product of its max and min digit K times | Function that returns the product of maximum and minimum digit of N number . ; Finds the last digit . ; Moves to next digit ; Function to find the formed number ; K -- ; M ( 1 ) = N ; Check if minimum digit is 0 ; Driver Code
function prod_of_max_min ( n ) { var largest = 0 ; var smallest = 10 ; while ( n ) { var r = n % 10 ; largest = Math . max ( r , largest ) ; smallest = Math . min ( r , smallest ) ; n = parseInt ( n / 10 ) ; } return largest * smallest ; } function formed_no ( N , K ) { if ( K == 1 ) { return N ; } var answer = N ; while ( K -- ) { var a_current = prod_of_max_min ( answer ) ; if ( a_current == 0 ) break ; answer += a_current ; } return answer ; } var N = 487 , K = 100000000 ; document . write ( formed_no ( N , K ) + " " ) ;
Logarithm tricks for Competitive Programming | Function to count the number of digits in a number ; Driver program to test above
function countDigit ( n ) { return Math . floor ( Math . log10 ( n ) + 1 ) ; } var n = 80 ; document . write ( countDigit ( n ) ) ;
Program to find the sum of the series 1 + x + x ^ 2 + x ^ 3 + . . + x ^ n | Function to find the sum of the series and print N terms of the given series ; First Term ; Loop to print the N terms of the series and find their sum ; Driver code
function sum ( x , n ) { let i , total = 1.0 , multi = x ; document . write ( total + " " ) ; for ( i = 1 ; i < n ; i ++ ) { total = total + multi ; document . write ( multi + " " ) ; multi = multi * x ; } document . write ( " " ) ; return total ; } let x = 2 ; let n = 5 ; document . write ( sum ( x , n ) . toFixed ( 2 ) ) ;
Find the remainder when N is divided by 4 using Bitwise AND operator | Function to find the remainder ; Bitwise AND with 3 ; return x ; Driver Code
function findRemainder ( n ) { let x = n & 3 ; return x ; } let N = 43 ; let ans = findRemainder ( N ) ; document . write ( ans ) ;
Check whether the number can be made palindromic after adding K | Function to check whether a number is a palindrome or not ; Convert num to string ; Comparing kth character from the beginning and N - kth character from the end . If all the characters match , then the number is a palindrome ; If all the above conditions satisfy , it means that the number is a palindrome ; Driver Code
function checkPalindrome ( num ) { let str = num . toString ( ) ; let l = 0 , r = str . length - 1 ; while ( l < r ) { if ( str [ l ] != str [ r ] ) { document . write ( " " ) ; return ; } l ++ ; r -- ; } document . write ( " " ) ; return ; } let n = 19 , k = 3 ; checkPalindrome ( n + k ) ;
Count of subsets with sum equal to X using Recursion | Recursive function to return the count of subsets with sum equal to the given value ; The recursion is stopped at N - th level where all the subsets of the given array have been checked ; Incrementing the count if sum is equal to 0 and returning the count ; Recursively calling the function for two cases Either the element can be counted in the subset If the element is counted , then the remaining sum to be checked is sum - the selected element If the element is not included , then the remaining sum to be checked is the total sum ; Driver code
function subsetSum ( arr , n , i , sum , count ) { if ( i == n ) { if ( sum == 0 ) { count ++ ; } return count ; } count = subsetSum ( arr , n , i + 1 , sum - arr [ i ] , count ) ; count = subsetSum ( arr , n , i + 1 , sum , count ) ; return count ; } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var sum = 10 ; var n = arr . length ; document . write ( subsetSum ( arr , n , 0 , sum , 0 ) ) ;
Distinct Prime Factors of an Array | Function to return an array of prime numbers upto n using Sieve of Eratosthenes ; Function to return distinct prime factors from the given array ; Creating an empty array to store distinct prime factors ; Iterating through all the prime numbers and check if any of the prime numbers is a factor of the given input array ; Finding prime numbers upto 10000 using Sieve of Eratosthenes
function sieve ( n ) { let prime = [ ] ; for ( let i = 0 ; i < n + 1 ; i ++ ) prime . push ( 0 ) ; let p = 2 ; while ( p * p <= n ) { if ( prime [ p ] == 0 ) { for ( let i = 2 * p ; i < n + 1 ; i += p ) prime [ i ] = 1 ; } p += 1 ; } let allPrimes = [ ] ; for ( let i = 2 ; i < n ; i ++ ) { if ( prime [ i ] == 0 ) allPrimes . push ( i ) ; } return allPrimes ; } function distPrime ( arr , allPrimes ) { let list1 = [ ] ; for ( let i = 0 ; i < allPrimes . length ; i ++ ) { for ( let j = 0 ; j < arr . length ; j ++ ) { if ( arr [ j ] % allPrimes [ i ] == 0 ) { list1 . push ( allPrimes [ i ] ) ; break ; } } } return list1 ; } let allPrimes = sieve ( 10000 ) ; let arr = [ ] ; arr . push ( 15 ) ; arr . push ( 30 ) ; arr . push ( 60 ) ; let ans = distPrime ( arr , allPrimes ) ; document . write ( " " ) ; for ( let i = 0 ; i < ans . length ; i ++ ) document . write ( ans [ i ] + " " ) ; document . write ( " " ) ;
Sum of the count of number of adjacent squares in an M X N grid | function to calculate the sum of all cells adjacent value ; Driver program to test above
function sum ( m , n ) { return 8 * m * n - 6 * m - 6 * n + 4 ; } var m = 3 , n = 2 ; document . write ( sum ( m , n ) ) ;
Count pairs in an array such that the absolute difference between them is ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¦¡¬°¥ K | Function to return the count of required pairs ; Sort the given array ; To store the required count ; Update j such that it is always > i ; Find the first element arr [ j ] such that ( arr [ j ] - arr [ i ] ) >= K This is because after this element , all the elements will have absolute difference with arr [ i ] >= k and the count of valid pairs will be ( n - j ) ; Update the count of valid pairs ; Get to the next element to repeat the steps ; Return the count ; Driver code
function count ( arr , n , k ) { arr . sort ( ) ; var cnt = 0 ; var i = 0 ; var j = 1 ; while ( i < n && j < n ) { if ( j <= i ) j = i + 1 else j = j while ( j < n && ( arr [ j ] - arr [ i ] ) < k ) j += 1 ; cnt += ( n - j ) ; i += 1 ; } return cnt ; } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; var k = 2 ; document . write ( count ( arr , n , k ) ) ;
Find the volume of rectangular right wedge | function to return volume of rectangular right wedge ; Driver code
function volumeRec ( a , b , e , h ) { return ( ( ( b * h ) / 6 ) * ( 2 * a + e ) ) ; } let a = 2 ; let b = 5 ; let e = 5 ; let h = 6 ; document . write ( " " + volumeRec ( a , b , e , h ) . toFixed ( 1 ) ) ;
Count squares with odd side length in Chessboard | Function to return the count of odd length squares possible ; To store the required count ; For all odd values of i ; Add the count of possible squares of length i ; Return the required count ; Driver code
function count_square ( n ) { let count = 0 ; for ( let i = 1 ; i <= n ; i = i + 2 ) { let k = n - i + 1 ; count += ( k * k ) ; } return count ; } let N = 8 ; document . write ( count_square ( N ) ) ;
Count of elements whose absolute difference with the sum of all the other elements is greater than k | Function to return the number of anomalies ; To store the count of anomalies ; To store the sum of the array elements ; Find the sum of the array elements ; Count the anomalies ; Driver code
function countAnomalies ( arr , n , k ) { var cnt = 0 ; var i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( Math . abs ( arr [ i ] - ( sum - arr [ i ] ) ) > k ) cnt ++ ; return cnt ; } var arr = [ 1 , 3 , 5 ] ; var n = arr . length ; var k = 1 ; document . write ( countAnomalies ( arr , n , k ) ) ;
Find the number of integers x in range ( 1 , N ) for which x and x + 1 have same number of divisors | Javascript implementation of the approach ; To store number of divisors and Prefix sum of such numbers ; Function to find the number of integers 1 < x < N for which x and x + 1 have the same number of positive divisors ; Count the number of divisors ; Run a loop upto sqrt ( i ) ; If j is divisor of i ; If it is perfect square ; x and x + 1 have same number of positive divisors ; Function call ; Required answer
const N = 100005 ; let d = new Array ( N ) . fill ( 0 ) ; let pre = new Array ( N ) . fill ( 0 ) ; function Positive_Divisors ( ) { for ( let i = 1 ; i < N ; i ++ ) { for ( let j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( j * j == i ) d [ i ] ++ ; else d [ i ] += 2 ; } } } let ans = 0 ; for ( let i = 2 ; i < N ; i ++ ) { if ( d [ i ] == d [ i - 1 ] ) ans ++ ; pre [ i ] = ans ; } } Positive_Divisors ( ) ; let n = 15 ; document . write ( pre [ n ] ) ;
Length of the smallest number which is divisible by K and formed by using 1 's only | Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Generate all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's ; If number is divisible by k then return the length ; Driver code
function numLen ( K ) { if ( K % 2 == 0 K % 5 == 0 ) { return - 1 ; } var number = 0 ; var len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = number * 10 + 1 ; if ( ( number % K == 0 ) ) { return len ; } } return - 1 ; } var K = 7 ; document . write ( numLen ( K ) ) ;
Find if the given number is present in the infinite sequence or not | Function that returns true if the sequence will contain B ; Driver Code
function doesContainB ( a , b , c ) { if ( a == b ) { return true ; } if ( ( b - a ) * c > 0 && ( b - a ) % c == 0 ) { return true ; } return false ; } let a = 1 , b = 7 , c = 3 ; if ( doesContainB ( a , b , c ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Find a permutation of 2 N numbers such that the result of given expression is exactly 2 K | Function to find the required permutation of first 2 * N natural numbers ; Iterate in blocks of 2 ; We need more increments , so print in reverse order ; We have enough increments , so print in same order ; Driver code
function printPermutation ( n , k ) { for ( var i = 1 ; i <= n ; i ++ ) { var x = 2 * i - 1 ; var y = 2 * i ; if ( i <= k ) document . write ( y + " " + x + " " ) ; else document . write ( x + " " + y + " " ) ; } } var n = 2 , k = 1 ; printPermutation ( n , k ) ;
Maximize the sum of products of the degrees between any two vertices of the tree | Function to return the maximum possible sum ; Initialize degree for node u to 2 ; If u is the leaf node or the root node ; Initialize degree for node v to 2 ; If v is the leaf node or the root node ; Update the sum ; Driver code
function maxSum ( N ) { var ans = 0 ; for ( var u = 1 ; u <= N ; u ++ ) { for ( var v = 1 ; v <= N ; v ++ ) { if ( u == v ) continue ; var degreeU = 2 ; if ( u == 1 u == N ) degreeU = 1 ; var degreeV = 2 ; if ( v == 1 v == N ) degreeV = 1 ; ans += ( degreeU * degreeV ) ; } } return ans ; } var N = 6 ; document . write ( maxSum ( N ) ) ;
Find integers that divides maximum number of elements of the array | Function to print the integers that divide the maximum number of elements from the array ; Initialize two lists to store rank and factors ; Start from 2 till the maximum element in arr ; Initialize a variable to count the number of elements it is a factor of ; Maximum rank in the rank list ; Print all the elements with rank m ; Driver code
function maximumFactor ( arr ) { var n = arr . length ; var rank = [ ] ; var factors = [ ] ; var max = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; for ( var i = 2 ; i <= max ; i ++ ) { var count = 0 ; for ( var j = 0 ; j < n ; j ++ ) { if ( arr [ j ] % i == 0 ) count += 1 ; rank . push ( count ) ; factors . push ( i ) ; } } var m = rank . reduce ( ( a , b ) => Math . max ( a , b ) ) ; for ( var i = 0 ; i < rank . length ; i ++ ) { if ( rank [ i ] == m ) document . write ( factors [ i ] + " " ) ; } } var arr = [ 120 , 15 , 24 , 63 , 18 ] ; maximumFactor ( arr ) ;
Natural Numbers | Returns sum of first n natural numbers ; Driver code
function findSum ( n ) { let sum = 0 ; for ( x = 1 ; x <= n ; x ++ ) sum = sum + x ; return sum ; } n = 5 ; document . write ( findSum ( n ) ) ;