codeparrot/xlcost-text-to-code huggingface dataset:
Number of digits before the decimal point in the division of two numbers | Function to return the number of digits before the decimal in a / b ; Return the required count of digits ; Driver code
function countDigits ( a , b ) { return Math . floor ( ( Math . log ( Math . abs ( a ) ) / Math . log ( 10 ) ) - ( Math . log ( Math . abs ( b ) ) / Math . log ( 10 ) ) ) + 1 ; } var a = 100 ; var b = 10 ; document . write ( countDigits ( a , b ) ) ;
Smallest odd number with N digits | Function to return smallest odd with n digits ; Driver Code
function smallestOdd ( n ) { if ( n == 1 ) return 1 ; return Math . pow ( 10 , n - 1 ) + 1 ; } var n = 4 ; document . write ( smallestOdd ( n ) ) ;
Largest Even and Odd N | Function to print the largest n - digit even and odd numbers ; Driver code
function findNumbers ( n ) { var odd = Math . pow ( 10 , n ) - 1 ; var even = odd - 1 ; document . write ( " " + even + " " ) ; document . write ( " " + odd ) ; } var n = 4 ; findNumbers ( n ) ;
Longest sub | Function to return the length of the longest sub - array whose product of elements is 0 ; Driver code
function longestSubArray ( arr , n ) { var isZeroPresent = false ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { isZeroPresent = true ; break ; } } if ( isZeroPresent ) return n ; return 0 ; } var arr = [ 1 , 2 , 3 , 0 , 1 , 2 , 0 ] ; var n = arr . length ; document . write ( longestSubArray ( arr , n ) ) ;
Smallest Even number with N digits | Function to return smallest even number with n digits ; Driver Code
function smallestEven ( n ) { if ( n == 1 ) return 0 ; return Math . pow ( 10 , n - 1 ) ; } var n = 4 ; document . write ( smallestEven ( n ) ) ;
Maximize profit when divisibility by two numbers have associated profits | JavaScript implementation of the approach ; Function to return the maximum profit ; min ( x , y ) * n / lcm ( a , b ) ; Driver code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function maxProfit ( n , a , b , x , y ) { let res = x * Math . floor ( n / a ) ; res += y * Math . floor ( n / b ) ; res -= Math . min ( x , y ) * ( n / ( ( a * b ) / __gcd ( a , b ) ) ) ; return res ; } let n = 6 , a = 6 , b = 2 , x = 8 , y = 2 ; document . write ( maxProfit ( n , a , b , x , y ) ) ;
Series summation if T ( n ) is given and n is very large | javascript program for the above approach ; Function to return the sum of the given series ; Driver Code
let MOD = 1000000007 ; function sumOfSeries ( n ) { let ans = Math . pow ( n % MOD , 2 ) ; return ( ans % MOD ) ; } let n = 10 ; document . write ( sumOfSeries ( n ) ) ;
Kth odd number in an array | Function to return the kth odd element from the array ; Traverse the array ; If current element is odd ; If kth odd element is found ; Total odd elements in the array are < k ; Driver code
function kthOdd ( arr , n , k ) { for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) k -- ; if ( k == 0 ) return arr [ i ] ; } return - 1 ; } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var n = arr . length ; var k = 2 ; document . write ( kthOdd ( arr , n , k ) ) ;
Find last five digits of a given five digit number raised to power five | Function to find the last five digits of a five digit number raised to power five ; Driver code
function lastFiveDigits ( n ) { n = ( Math . floor ( n / 10000 ) ) * 10000 + ( Math . floor ( n / 100 ) % 10 ) * 1000 + ( n % 10 ) * 100 + ( Math . floor ( n / 10 ) % 10 ) * 10 + Math . floor ( n / 1000 ) % 10 ; let ans = 1 ; for ( let i = 0 ; i < 5 ; i ++ ) { ans *= n ; ans %= 100000 ; } document . write ( ans ) ; } let n = 12345 ; lastFiveDigits ( n ) ;
Sum of ( maximum element | Javascript implementation of the above approach ; Function to return a ^ n % mod ; Compute sum of max ( A ) - min ( A ) for all subsets ; Sort the array . ; Maxs = 2 ^ i - 1 ; Mins = 2 ^ ( n - 1 - i ) - 1 ; Driver code
let mod = 1000000007 ; function power ( a , n ) { if ( n == 0 ) { return 1 ; } let p = power ( a , n / 2 ) % mod ; p = ( p * p ) % mod ; if ( n == 1 ) { p = ( p * a ) % mod ; } return p ; } function computeSum ( arr , n ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let maxs = ( power ( 2 , i ) - 1 + mod ) % mod ; maxs = ( maxs * arr [ i ] ) % mod ; let mins = ( power ( 2 , n - 1 - i ) - 1 + mod ) % mod ; mins = ( mins * arr [ i ] ) % mod ; let V = ( maxs - mins + mod ) % mod ; sum = ( sum + V ) % mod ; } return sum ; } let arr = [ 4 , 3 , 1 ] ; let n = arr . length ; document . write ( computeSum ( arr , n ) ) ;
Count of all N digit numbers such that num + Rev ( num ) = 10 ^ N | Function to return the count of such numbers ; If n is odd ; Driver code
function countNumbers ( n ) { if ( n % 2 == 1 ) return 0 ; return ( 9 * Math . pow ( 10 , parseInt ( n / 2 ) - 1 ) ) ; } var n = 2 ; document . write ( countNumbers ( n ) ) ;
Count of numbers having only 1 set bit in the range [ 0 , n ] | Function to return the required count ; To store the count of numbers ; Every power of 2 contains only 1 set bit ; Driver code
function count ( n ) { var cnt = 0 ; var p = 1 ; while ( p <= n ) { cnt ++ ; p *= 2 ; } return cnt ; } var n = 7 ; document . write ( count ( n ) ) ;
Find the K | Function to find the K - th minimum element from an array concatenated M times ; Sort the elements in ascending order ; Return the K 'th Min element  present at ( (K-1) / M ) index ; Driver Code
function KthMinValAfterMconcatenate ( A , N , M , K ) { A . sort ( ( a , b ) => a - b ) return ( A [ ( ( K - 1 ) / M ) ] ) ; } var A = [ 3 , 1 , 2 ] ; var M = 3 , K = 4 ; var N = A . length ; document . write ( KthMinValAfterMconcatenate ( A , N , M , K ) ) ;
Sum of all i such that ( 2 ^ i + 1 ) % 3 = 0 where i is in range [ 1 , n ] | Function to return the required sum ; Total odd numbers from 1 to n ; Sum of first n odd numbers ; Driver code
function sumN ( n ) { n = parseInt ( ( n + 1 ) / 2 ) ; return ( n * n ) ; } var n = 3 ; document . write ( sumN ( n ) ) ;
Numbers that are not divisible by any number in the range [ 2 , 10 ] | Function to return the count of numbers from 1 to N which are not divisible by any number in the range [ 2 , 10 ] ; Driver code
function countNumbers ( n ) { return n - parseInt ( n / 2 , 10 ) - parseInt ( n / 3 , 10 ) - parseInt ( n / 5 , 10 ) - parseInt ( n / 7 , 10 ) + parseInt ( n / 6 , 10 ) + parseInt ( n / 10 , 10 ) + parseInt ( n / 14 , 10 ) + parseInt ( n / 15 , 10 ) + parseInt ( n / 21 , 10 ) + parseInt ( n / 35 , 10 ) - parseInt ( n / 30 , 10 ) - parseInt ( n / 42 , 10 ) - parseInt ( n / 70 , 10 ) - parseInt ( n / 105 , 10 ) + parseInt ( n / 210 , 10 ) ; } let n = 20 ; document . write ( countNumbers ( n ) ) ;
Maximum Primes whose sum is equal to given N | Function to find max count of primes ; if n is even n / 2 is required answer if n is odd floor ( n / 2 ) = ( int ) ( n / 2 ) is required answer ; Driver Code
function maxPrimes ( n ) { return parseInt ( n / 2 ) ; } var n = 17 ; document . write ( maxPrimes ( n ) ) ;
Sum of the series ( 1 * 2 ) + ( 2 * 3 ) + ( 3 * 4 ) + ... ... upto n terms | Function to return sum ; Driver code
function sum ( n ) { return n * ( n + 1 ) * ( n + 2 ) / 3 ; } var n = 2 ; document . write ( sum ( n ) ) ;
Smallest divisor D of N such that gcd ( D , M ) is greater than 1 | javascript implementation of the above approach ; Function to find the minimum divisor ; Iterate for all factors of N ; Check for gcd > 1 ; Check for gcd > 1 ; If gcd is m itself ; Driver code
function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } function findMinimum ( n , m ) { var mini = m ; for ( var i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { var sec = n / i ; if ( __gcd ( m , i ) > 1 ) { return i ; } else if ( __gcd ( sec , m ) > 1 ) { mini = Math . min ( sec , mini ) ; } } } if ( mini == m ) return - 1 ; else return mini ; } var n = 8 , m = 10 ; document . write ( findMinimum ( n , m ) ) ;
Find Nth term of the series 1 , 5 , 32 , 288 ... | Function to generate a fixed \ number ; Finding nth term ; Driver Method
function nthTerm ( N ) { let nth = 0 , i ; for ( i = N ; i > 0 ; i -- ) { nth += Math . pow ( i , i ) ; } return nth ; } let N = 3 ; document . write ( nthTerm ( N ) ) ;
Find kth smallest number in range [ 1 , n ] when all the odd numbers are deleted | Function to return the kth smallest element from the range [ 1 , n ] after removing all the odd elements ; Driver code
function kthSmallest ( n , k ) { return ( 2 * k ) ; } var n = 8 , k = 4 ; document . write ( kthSmallest ( n , k ) ) ;
Check if a number can be represented as sum of non zero powers of 2 | Function that return true if n can be represented as the sum of powers of 2 without using 2 ^ 0 ; Driver code
function isSumOfPowersOfTwo ( n ) { if ( n % 2 == 1 ) return false ; else return true ; } var n = 10 ; if ( isSumOfPowersOfTwo ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimize sum of adjacent difference with removal of one element from array | Function to find the element ; Value variable for storing the total value ; Declaring maximum value as zero ; If array contains on element ; Storing the maximum value in temp variable ; Adding the adjacent difference modulus values of removed element . Removing adjacent difference modulus value after removing element ; Returning total value - maximum value ; Drivers code
function findMinRemoval ( arr , n ) { var temp , value = 0 ; var maximum = 0 ; if ( n == 1 ) return 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( i != 0 && i != n - 1 ) { value = value + Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = Math . abs ( arr [ i ] - arr [ i + 1 ] ) + Math . abs ( arr [ i ] - arr [ i - 1 ] ) - Math . abs ( arr [ i - 1 ] - arr [ i + 1 ] ) ; } else if ( i == 0 ) { value = value + Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; } else temp = Math . abs ( arr [ i ] - arr [ i - 1 ] ) ; maximum = Math . max ( maximum , temp ) ; } return ( value - maximum ) ; } var arr = [ 1 , 5 , 3 , 2 , 10 ] ; var n = arr . length ; document . write ( findMinRemoval ( arr , n ) + " " ) ;
Time until distance gets equal to X between two objects moving in opposite direction | Function to return the time for which the two policemen can communicate ; time = distance / speed ; Driver code
function getTime ( u , v , x ) { let speed = u + v ; let time = x / speed ; return time ; } let u = 3 , v = 3 , x = 3 ; document . write ( getTime ( u , v , x ) ) ;
Given number of matches played , find number of teams in tournament | Function to return the number of teams ; To store both roots of the equation ; sqrt ( b ^ 2 - 4 ac ) ; First root ( - b + sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Second root ( - b - sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Return the positive root ; Driver code
function number_of_teams ( M ) { var N1 , N2 , sqr ; sqr = parseInt ( Math . sqrt ( 1 + ( 8 * M ) ) ) ; N1 = ( 1 + sqr ) / 2 ; N2 = ( 1 - sqr ) / 2 ; if ( N1 > 0 ) return N1 ; return N2 ; } var M = 45 ; document . write ( number_of_teams ( M ) ) ;
Sum of numbers from 1 to N which are in Lucas Sequence | Function to return the required sum ; Generate lucas number and keep on adding them ; Driver code
function LucasSum ( N ) { var sum = 0 ; var a = 2 , b = 1 , c ; sum += a ; while ( b <= N ) { sum += b ; var c = a + b ; a = b ; b = c ; } return sum ; } var N = 20 ; document . write ( LucasSum ( N ) ) ;
Count of all even numbers in the range [ L , R ] whose sum of digits is divisible by 3 | Function to return the count of required numbers ; Count of numbers in range which are divisible by 6 ; Driver code
function countNumbers ( l , r ) { return ( parseInt ( r / 6 ) - parseInt ( ( l - 1 ) / 6 ) ) ; } var l = 1000 , r = 6000 ; document . write ( countNumbers ( l , r ) ) ;
Sum of minimum element of all sub | Function to find the sum of minimum of all subsequence ; Driver code
function findMinSum ( arr , n ) { var occ = n - 1 , sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { sum += arr [ i ] * Math . pow ( 2 , occ ) ; occ -- ; } return sum ; } var arr = [ 1 , 2 , 4 , 5 ] ; var n = arr . length ; document . write ( findMinSum ( arr , n ) ) ;
Number of trailing zeroes in base B representation of N ! | To find the power of a prime p in factorial N ; calculating floor ( n / r ) and adding to the count ; increasing the power of p from 1 to 2 to 3 and so on ; returns all the prime factors of k ; vector to store all the prime factors along with their number of occurrence in factorization of B ; Returns largest power of B that divides N ! ; calculating minimum power of all the prime factors of B ; Driver code
function findPowerOfP ( N , p ) { var count = 0 ; var r = p ; while ( r <= N ) { count += ( N / r ) ; r = r * p ; } return count ; } function primeFactorsofB ( B ) { var ans = [ ] ; for ( var i = 2 ; B != 1 ; i ++ ) { if ( B % i == 0 ) { var count = 0 ; while ( B % i == 0 ) { B = B / i ; count ++ ; } ans . push ( [ i , count ] ) ; } } return ans ; } function largestPowerOfB ( N , B ) { var vec = [ ] ; vec = primeFactorsofB ( B ) ; var ans = Number . MAX_VALUE ; for ( var i = 0 ; i < vec . length ; i ++ ) ans = Math . min ( ans , Math . floor ( findPowerOfP ( N , vec [ i ] [ 0 ] ) / vec [ i ] [ 1 ] ) ) ; return ans ; } document . write ( largestPowerOfB ( 5 , 2 ) + " " ) ; document . write ( largestPowerOfB ( 6 , 9 ) + " " ) ;
Position of a person diametrically opposite on a circle | Function to return the required position ; Driver code
function getPosition ( n , m ) { if ( m > ( n / 2 ) ) return ( m - parseInt ( n / 2 ) ) ; return ( m + parseInt ( n / 2 ) ) ; } var n = 8 , m = 5 ; document . write ( getPosition ( n , m ) ) ;
Minimum operations required to modify the array such that parity of adjacent elements is different | Function to return the parity of a number ; Function to return the minimum number of operations required ; Operation needs to be performed ; Parity of previous element ; Parity of next element ; Update parity of current element to be other than the parities of the previous and the next number ; Driver Code
function parity ( a ) { return a % 3 ; } function solve ( array , size ) { var operations = 0 ; for ( var i = 0 ; i < size - 1 ; i ++ ) { if ( parity ( array [ i ] ) == parity ( array [ i + 1 ] ) ) { operations ++ ; if ( i + 2 < size ) { var pari1 = parity ( array [ i ] ) ; var pari2 = parity ( array [ i + 2 ] ) ; if ( pari1 == pari2 ) { if ( pari1 == 0 ) array [ i + 1 ] = 1 ; else if ( pari1 == 1 ) array [ i + 1 ] = 0 ; else array [ i + 1 ] = 1 ; } else { if ( ( pari1 == 0 && pari2 == 1 ) || ( pari1 == 1 && pari2 == 0 ) ) array [ i + 1 ] = 2 ; if ( ( pari1 == 1 && pari2 == 2 ) || ( pari1 == 2 && pari2 == 1 ) ) array [ i + 1 ] = 0 ; if ( ( pari1 == 2 && pari2 == 0 ) || ( pari1 == 0 && pari2 == 2 ) ) array [ i + 1 ] = 1 ; } } } } return operations ; } var array = [ 2 , 1 , 3 , 0 ] ; var size = array . length ; document . write ( solve ( array , size ) ) ;
Number of submatrices with OR value 1 | Javascript program to count number of submatrices with OR value 1 ; Function to find required prefix - count for each row from right to left ; Function to find the count of submatrices with OR value 1 ; Array to store prefix count of zeros from right to left for boolean array ; Variable to store the count of submatrices with OR value 0 ; Loop to evaluate each column of the prefix matrix uniquely . For each index of a column we will try to determine the number of sub - matrices starting from that index and has all 1 s ; First part of pair will be the value of inserted element . Second part will be the count of the number of elements pushed before with a greater value ; Variable to store the number of submatrices with all 0 s ; Return the final answer ; Driver Code
var n = 3 function findPrefixCount ( p_arr , arr ) { for ( var i = 0 ; i < n ; i ++ ) for ( var j = n - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] ) continue ; if ( j != n - 1 ) p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] ; p_arr [ i ] [ j ] += ( ! arr [ i ] [ j ] ) ; } } function matrixOrValueOne ( arr ) { var p_arr = Array . from ( Array ( n ) , ( ) => Array ( n ) . fill ( 0 ) ) ; findPrefixCount ( p_arr , arr ) ; var count_zero_submatrices = 0 ; for ( var j = 0 ; j < n ; j ++ ) { var i = n - 1 ; var q = [ ] ; var to_sum = 0 ; while ( i >= 0 ) { var c = 0 ; while ( q . length != 0 && q [ q . length - 1 ] [ 0 ] > p_arr [ i ] [ j ] ) { to_sum -= ( q [ q . length - 1 ] [ 1 ] + 1 ) * ( q [ q . length - 1 ] [ 0 ] - p_arr [ i ] [ j ] ) ; c += q [ q . length - 1 ] [ 1 ] + 1 ; q . pop ( ) ; } to_sum += p_arr [ i ] [ j ] ; count_zero_submatrices += to_sum ; q . push ( [ p_arr [ i ] [ j ] , c ] ) ; i -- ; } } return ( n * ( n + 1 ) * n * ( n + 1 ) ) / 4 - count_zero_submatrices ; } var arr = [ [ 0 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 0 ] ] ; document . write ( matrixOrValueOne ( arr ) ) ;
XOR of XORs of all sub | Javascript program to find the XOR of XOR 's of all submatrices ; Function to find to required XOR value ; Nested loop to find the number of sub - matrix each index belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code
const n = 3 ; function submatrixXor ( arr ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { let top_left = ( i + 1 ) * ( j + 1 ) ; let bottom_right = ( n - i ) * ( n - j ) ; if ( ( top_left % 2 == 1 ) && ( bottom_right % 2 == 1 ) ) ans = ( ans ^ arr [ i ] [ j ] ) ; } } return ans ; } let arr = [ [ 6 , 7 , 13 ] , [ 8 , 3 , 4 ] , [ 9 , 7 , 6 ] ] ; document . write ( submatrixXor ( arr ) ) ;
Find Nth positive number whose digital root is X | Function to find the digital root of a number ; Function to find the Nth number with digital root as X ; Counter variable to keep the count of valid numbers ; Find digital root ; Check if is required answer or not ; Print the answer if you have found it and breakout of the loop ; Driver Code
function findDigitalRoot ( num ) { var sum = Number . MAX_VALUE , tempNum = num ; while ( sum >= 10 ) { sum = 0 ; while ( tempNum > 0 ) { sum += tempNum % 10 ; tempNum = parseInt ( tempNum / 10 ) ; } tempNum = sum ; } return sum ; } function findAnswer ( X , N ) { var counter = 0 ; for ( var i = 1 ; counter < N ; ++ i ) { var digitalRoot = findDigitalRoot ( i ) ; if ( digitalRoot == X ) { counter += 1 ; } if ( counter == N ) { document . write ( i ) ; break ; } } } var X = 1 , N = 3 ; findAnswer ( X , N ) ;
Find Nth positive number whose digital root is X | Function to find the N - th number with digital root as X ; Driver Code
function findAnswer ( X , N ) { return ( N - 1 ) * 9 + X ; } let X = 7 ; let N = 43 ; document . write ( findAnswer ( X , N ) ) ;
Sum of the natural numbers ( up to N ) whose modulo with K yield R | Function to return the sum ; If current number gives R as the remainder on dividing by K ; Update the sum ; Return the sum ; Driver code
function count ( N , K , R ) { var sum = 0 ; for ( i = 1 ; i <= N ; i ++ ) { if ( i % K == R ) sum += i ; } return sum ; } var N = 20 , K = 4 , R = 3 ; document . write ( count ( N , K , R ) ) ;
Longest sub | Function to return the length of the longest required sub - sequence ; Find the maximum element from the array ; push all lucas numbers below max to the set a and b are first two elements of the Lucas sequence ; If current element is a Lucas number , increment count ; Return the count ; Driver code
function LucasSequence ( arr , n ) { var max = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var s = [ ] ; var a = 2 , b = 1 , c ; s . push ( a ) ; s . push ( b ) ; while ( b < max ) { var c = a + b ; a = b ; b = c ; s . push ( b ) ; } s . sort ( ( a , b ) => a - b ) var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( s . includes ( arr [ i ] ) ) { s . pop ( arr [ i ] ) ; count ++ ; } } return count ; } var arr = [ 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 ] ; var n = arr . length ; document . write ( LucasSequence ( arr , n ) ) ;
Find the number of solutions to the given equation | Function to return the count of valid values of X ; Iterate through all possible sum of digits of X ; Get current value of X for sum of digits i ; Find sum of digits of cr ; If cr is a valid choice for X ; Return the count ; driver program
function getCount ( a , b , c ) { let count = 0 ; for ( let i = 1 ; i <= 81 ; i ++ ) { let cr = b * Math . pow ( i , a ) + c ; let tmp = cr ; let sm = 0 ; while ( tmp != 0 ) { sm += tmp % 10 ; tmp = Math . floor ( tmp / 10 ) ; } if ( sm == i && cr < 1e9 ) count ++ ; } return count ; } let a = 3 , b = 2 , c = 8 ; document . write ( getCount ( a , b , c ) ) ;
Check if an array of 1 s and 2 s can be divided into 2 parts with equal sum | Function to check if it is possible to split the array in two parts with equal sum ; Calculate sum of elements and count of 1 's ; If total sum is odd , return False ; If sum of each part is even , return True ; If sum of each part is even but there is atleast one 1 ; driver program
function isSpiltPossible ( n , a ) { let sum = 0 , c1 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; if ( a [ i ] == 1 ) { c1 ++ ; } } if ( sum % 2 != 0 ) return false ; if ( ( sum / 2 ) % 2 == 0 ) return true ; if ( c1 > 0 ) return true ; else return false ; } let n = 3 ; let a = [ 1 , 1 , 2 ] ; if ( isSpiltPossible ( n , a ) ) document . write ( " " ) ; else document . write ( " " ) ;
Make a tree with n vertices , d diameter and at most vertex degree k | Function to Make a tree with n vertices , d as it 's diameter and degree of each vertex is at most k ; If diameter > n - 1 impossible to build tree ; To store the degree of each vertex ; To store the edge between vertices ; To store maximum distance among all the paths from a vertex ; Make diameter of tree equals to d ; Add an edge between them ; Store maximum distance of each vertex from other vertices ; For next ( n - d - 1 ) edges ; If the vertex already has the degree k ; If not possible ; Increase the degree of vertices ; Add an edge between them ; Store the maximum distance of this vertex from other vertices ; Print all the edges of the built tree ; Driver code
function Make_tree ( n , d , k ) { if ( d > n - 1 ) { document . write ( " " ) ; return ; } let deg = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { deg [ i ] = 0 ; } let ans = [ ] ; let q = new Set ( ) ; for ( let i = 0 ; i < d ; ++ i ) { ++ deg [ i ] ; ++ deg [ i + 1 ] ; if ( deg [ i ] > k deg [ i + 1 ] > k ) { document . write ( " " ) ; return ; } ans . push ( [ i , i + 1 ] ) ; } for ( let i = 1 ; i < d ; ++ i ) q . add ( [ Math . max ( i , d - i ) , i ] ) ; for ( let i = d + 1 ; i < n ; ++ i ) { let arr = Array . from ( q ) ; while ( q . size != 0 && deg [ arr [ 0 ] [ 1 ] ] == k ) q . delete ( arr [ 0 ] ) ; if ( q . size == 0 arr [ 0 ] [ 0 ] == d ) { document . write ( " " ) return ; } ++ deg [ i ] ; ++ deg [ arr [ 0 ] [ 1 ] ] ; ans . push ( [ i , arr [ 0 ] [ 1 ] ] ) ; q . add ( [ arr [ 0 ] [ 0 ] + 1 , i ] ) ; } for ( let i = 0 ; i < n - 1 ; ++ i ) document . write ( ( ans [ i ] [ 0 ] + 1 ) + " " + ( ans [ i ] [ 1 ] + 1 ) + " " ) ; } let n = 6 , d = 3 , k = 4 ; Make_tree ( n , d , k ) ;
Sum of all Submatrices of a Given Matrix | JavaScript program to find the sum of all possible submatrices of a given Matrix ; Function to find the sum of all possible submatrices of a given Matrix ; Variable to store the required sum ; Nested loop to find the number of submatrices , each number belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code
let n = 3 ; function matrixSum ( arr ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) { let top_left = ( i + 1 ) * ( j + 1 ) ; let bottom_right = ( n - i ) * ( n - j ) ; sum += ( top_left * bottom_right * arr [ i ] [ j ] ) ; } return sum ; } let arr = [ [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] ] ; document . write ( matrixSum ( arr ) ) ;
Maximum Bitwise AND pair from given range | Function to return the maximum bitwise AND possible among all the possible pairs ; Maximum among all ( i , j ) pairs ; Driver code
function maxAND ( L , R ) { var maximum = L & R ; for ( var i = L ; i < R ; i ++ ) for ( var j = i + 1 ; j <= R ; j ++ ) maximum = Math . max ( maximum , ( i & j ) ) ; return maximum ; } var L = 1 , R = 632 ; document . write ( maxAND ( L , R ) ) ;