codeparrot/xlcost-text-to-code huggingface dataset:
Print steps to make a number in form of 2 ^ X | Function to find the leftmost unset bit in a number . ; Function that perform the step ; Find the leftmost unset bit ; If the number has no bit unset , it means it is in form 2 ^ x - 1 ; Count the steps ; Iterate till number is of form 2 ^ x - 1 ; At even step increase by 1 ; Odd step xor with any 2 ^ m - 1 ; Find the leftmost unset bit ; 2 ^ m - 1 ; Perform the step ; Increase the steps ; Driver code
function find_leftmost_unsetbit ( n ) { let ind = - 1 ; let i = 1 ; while ( n ) { if ( ! ( n & 1 ) ) ind = i ; i ++ ; n >>= 1 ; } return ind ; } function perform_steps ( n ) { let left = find_leftmost_unsetbit ( n ) ; if ( left == - 1 ) { document . write ( " " ) ; return ; } let step = 1 ; while ( find_leftmost_unsetbit ( n ) != - 1 ) { if ( step % 2 == 0 ) { n += 1 ; document . write ( " " + step + " " ) ; } else { let m = find_leftmost_unsetbit ( n ) ; let num = Math . pow ( 2 , m ) - 1 ; n = n ^ num ; document . write ( " " + step + " " + num + " " ) ; } step += 1 ; } } let n = 39 ; perform_steps ( n ) ;
Determine the position of the third person on regular N sided polygon | Function to find out the number of that vertices ; Another person can 't stand on  vertex on which 2 children stand. ; calculating minimum jumps from each vertex . ; Calculate sum of jumps . ; Driver code ; Calling function
function vertices ( N , A , B ) { var position = 0 ; var minisum = Number . MAX_VALUE ; var sum = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { if ( i == A i == B ) continue ; else { var x = Math . abs ( i - A ) ; var y = Math . abs ( i - B ) ; sum = x + y ; if ( sum < minisum ) { minisum = sum ; position = i ; } } } return position ; } var N = 3 , A = 1 , B = 2 ; document . write ( " " + vertices ( N , A , B ) ) ;
Find sum of factorials in an array | Function to return the factorial of n ; Function to return the sum of factorials of the array elements ; To store the required sum ; Add factorial of all the elements ; Driver code
function factorial ( n ) { let f = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { f *= i ; } return f ; } function sumFactorial ( arr , n ) { let s = 0 ; for ( let i = 0 ; i < n ; i ++ ) { s += factorial ( arr [ i ] ) ; } return s ; } let arr = [ 7 , 3 , 5 , 4 , 8 ] ; let n = arr . length ; document . write ( sumFactorial ( arr , n ) ) ;
Minimum steps to color the tree with given colors | To store the required answer ; To store the graph ; Function to add edges ; Dfs function ; When there is difference in colors ; For all it 's child nodes ; Driver code ; Here zero is for parent of node 1 ; Adding edges in the graph ; Dfs call ; Required answer
let ans = 0 ; let gr = [ ] ; function Add_Edge ( u , v ) { gr [ u ] . push ( v ) ; gr [ v ] . push ( u ) ; } function dfs ( child , par , color ) { if ( color [ child ] != color [ par ] ) ans ++ ; for ( let i = 0 ; i < gr [ child ] . length ; i ++ ) { if ( gr [ child ] [ i ] == par ) continue ; dfs ( gr [ child ] [ i ] , child , color ) ; } } for ( let i = 0 ; i <= 10 ; i ++ ) gr . push ( [ ] ) ; let color = [ 0 , 1 , 2 , 3 , 2 , 2 , 3 ] ; Add_Edge ( 1 , 2 ) ; Add_Edge ( 1 , 3 ) ; Add_Edge ( 2 , 4 ) ; Add_Edge ( 2 , 5 ) ; Add_Edge ( 3 , 6 ) ; dfs ( 1 , 0 , color ) ; document . write ( ans ) ;
Highest power of 2 that divides a number represented in binary | Function to return the highest power of 2 which divides the given binary number ; To store the highest required power of 2 ; Counting number of consecutive zeros from the end in the given binary string ; Driver code
function highestPower ( str , len ) { let ans = 0 ; for ( let i = len - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) ans ++ ; else break ; } return ans ; } let str = " " ; let len = str . length ; document . write ( highestPower ( str , len ) ) ;
Number of ways to arrange K different objects taking N objects at a time | javascript implementation of the approach ; Function to return n ! % p ; var res = 1 ; Initialize result ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Returns n ^ ( - 1 ) mod p ; Returns nCr % p using Fermat 's little theorem. ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function to return the number of ways to arrange K different objects taking N objects at a time ; Driver Code ; Function call
var mod = parseInt ( 1e4 + 7 ) ; function factorial ( n , p ) { for ( var i = 2 ; i <= n ; i ++ ) res = ( res * i ) % p ; return res ; } function power ( x , y , p ) { while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function modInverse ( n , p ) { return power ( n , p - 2 , p ) ; } function nCrModP ( n , r , p ) { if ( r == 0 ) return 1 ; var fac = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; ; fac [ 0 ] = 1 ; for ( var i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ parseInt ( n ) ] * modInverse ( fac [ parseInt ( r ) ] , p ) % p * modInverse ( fac [ parseInt ( n ) - parseInt ( r ) ] , p ) % p ) % p ; } function countArrangements ( n , k , p ) { return ( factorial ( n , p ) * nCrModP ( k , n , p ) ) % p ; } var N = 5 , K = 8 ; document . write ( countArrangements ( N , K , mod ) ) ;
Find maximum product of digits among numbers less than or equal to N | Function that returns the maximum product of digits among numbers less than or equal to N ; Driver code
function maxProd ( N ) { if ( N == 0 ) return 1 ; if ( N < 10 ) return N ; return Math . max ( maxProd ( parseInt ( N / 10 ) ) * ( N % 10 ) , maxProd ( parseInt ( N / 10 ) - 1 ) * 9 ) ; } let N = 390 ; document . write ( maxProd ( N ) ) ;
Check if a number from every row can be selected such that xor of the numbers is greater than zero | Javascript program to implement the above approach ; Function to check if a number from every row can be selected such that xor of the numbers is greater than zero ; Find the xor of first column for every row ; If Xorr is 0 ; Traverse in the matrix ; Check is atleast 2 distinct elements ; Driver code
let N = 2 ; let M = 3 ; function check ( mat ) { let xorr = 0 ; for ( let i = 0 ; i < N ; i ++ ) { xorr ^= mat [ i ] [ 0 ] ; } if ( xorr != 0 ) return true ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 1 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] != mat [ i ] [ 0 ] ) return true ; } } return false ; } let mat = [ [ 7 , 7 , 7 ] , [ 10 , 10 , 7 ] ] ; if ( check ( mat ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of the series 1 , 2 , 4 , 3 , 5 , 7 , 9 , 6 , 8 , 10 , 11 , 13. . till N | Function to find the sum of first N odd numbers ; Function to find the sum of first N even numbers ; Function to overall find the sum of series ; Initial odd numbers ; Initial even numbers ; First power of 2 ; Check for parity for odd / even ; Counts the sum ; Get the minimum out of remaining num or power of 2 ; Decrease that much numbers from num ; If the segment has odd numbers ; Summate the odd numbers By exclusion ; Increase number of odd numbers ; If the segment has even numbers ; Summate the even numbers By exclusion ; Increase number of even numbers ; Next set of numbers ; Change parity for odd / even ; Driver code
function sumodd ( n ) { return ( n * n ) ; } function sumeven ( n ) { return ( n * ( n + 1 ) ) ; } function findSum ( num ) { let sumo = 0 ; let sume = 0 ; let x = 1 ; let cur = 0 ; let ans = 0 ; while ( num > 0 ) { let inc = Math . min ( x , num ) ; num -= inc ; if ( cur == 0 ) { ans = ans + sumodd ( sumo + inc ) - sumodd ( sumo ) ; sumo += inc ; } else { ans = ans + sumeven ( sume + inc ) - sumeven ( sume ) ; sume += inc ; } x *= 2 ; cur ^= 1 ; } return ans ; } let n = 4 ; document . write ( findSum ( n ) ) ;
Find the nth term of the given series | Function to return the nth term of the given series ; Driver code
function oddTriangularNumber ( N ) { return ( N * ( ( 2 * N ) - 1 ) ) ; } let N = 3 ; document . write ( oddTriangularNumber ( N ) ) ;
Check if given two straight lines are identical or not | Function to check if they are identical ; Driver Code
function idstrt ( a1 , b1 , c1 , a2 , b2 , c2 ) { if ( ( a1 / a2 == b1 / b2 ) && ( a1 / a2 == c1 / c2 ) && ( b1 / b2 == c1 / c2 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } var a1 = - 2 , b1 = 4 , c1 = 3 , a2 = - 6 , b2 = 12 , c2 = 9 ; idstrt ( a1 , b1 , c1 , a2 , b2 , c2 ) ;
Equation of straight line passing through a given point which bisects it into two equal line segments | Function to prvar the equation of the required line ; Driver code
function line ( x0 , y0 ) { var c = parseInt ( 2 * y0 * x0 ) ; document . write ( y0 + " " + " " + x0 + " " + c ) ; } var x0 = 4 , y0 = 3 ; line ( x0 , y0 ) ;
Find the original matrix when largest element in a row and a column are given | Javascript implementation of the approach ; Function that prints the original matrix ; Iterate in the row ; Iterate in the column ; If previously existed an element ; Driver code
let N = 3 ; let M = 7 ; function printOriginalMatrix ( a , b , mat ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) document . write ( Math . min ( a [ i ] , b [ j ] ) + " " ) ; else document . write ( " " + " " ) ; } document . write ( " " ) ; } } let a = [ 2 , 1 , 3 ] ; let b = [ 2 , 3 , 0 , 0 , 2 , 0 , 1 ] ; let mat = [ [ 1 , 0 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ 1 , 1 , 0 , 0 , 0 , 0 , 0 ] ] ; printOriginalMatrix ( a , b , mat ) ;
Calculate the loss incurred in selling the given items at discounted price | Function to return the x % of n ; Function to return the total loss ; To store the total loss ; Original price of the item ; The price at which the item will be sold ; The discounted price of the item ; Loss incurred ; Driver code ; Total items
function percent ( n , x ) { let p = n * x ; p = Math . floor ( p / 100 ) ; return p ; } function getLoss ( price , quantity , X , n ) { let loss = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let originalPrice = price [ i ] ; let sellingPrice = originalPrice + percent ( originalPrice , X [ i ] ) ; let afterDiscount = sellingPrice - percent ( sellingPrice , X [ i ] ) ; loss += ( ( originalPrice - afterDiscount ) * quantity [ i ] ) ; } return loss ; } let price = [ 20 , 48 , 200 , 100 ] ; let quantity = [ 20 , 48 , 1 , 1 ] ; let X = [ 0 , 48 , 200 , 5 ] ; let n = X . length ; document . write ( getLoss ( price , quantity , X , n ) ) ;
Maximum difference between two elements in an Array | Function to return the maximum absolute difference between any two elements of the array ; To store the minimum and the maximum elements from the array ; Driver code
function maxAbsDiff ( arr , n ) { let minEle = arr [ 0 ] ; let maxEle = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { minEle = Math . min ( minEle , arr [ i ] ) ; maxEle = Math . max ( maxEle , arr [ i ] ) ; } return ( maxEle - minEle ) ; } let arr = [ 2 , 1 , 5 , 3 ] ; let n = arr . length ; document . write ( maxAbsDiff ( arr , n ) ) ;
Maximize the maximum subarray sum after removing atmost one element | Function to return the maximum sub - array sum ; Initialized ; Traverse in the array ; Increase the sum ; If sub - array sum is more than the previous ; If sum is negative ; Function that returns the maximum sub - array sum after removing an element from the same sub - array ; Maximum sub - array sum using Kadane 's Algorithm ; Re - apply Kadane 's with minor changes ; Increase the sum ; If sub - array sum is greater than the previous ; If elements are 0 , no removal ; If elements are more , then store the minimum value in the sub - array obtained till now ; If sum is negative ; Re - initialize everything ; Driver code
function maxSubArraySum ( a , size ) { let max_so_far = Number . MIN_VALUE , max_ending_here = 0 ; for ( let i = 0 ; i < size ; i ++ ) { 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 ; } return max_so_far ; } function maximizeSum ( a , n ) { let cnt = 0 ; let mini = Number . MAX_VALUE ; let minSubarray = Number . MAX_VALUE ; let sum = maxSubArraySum ( a , n ) ; let max_so_far = Number . MIN_VALUE , max_ending_here = 0 ; for ( let i = 0 ; i < n ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; cnt ++ ; minSubarray = Math . min ( a [ i ] , minSubarray ) ; if ( sum == max_ending_here ) { if ( cnt == 1 ) mini = Math . min ( mini , 0 ) ; else mini = Math . min ( mini , minSubarray ) ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; cnt = 0 ; minSubarray = Integer . MAX_VALUE ; } } return sum - mini ; } let a = [ 1 , 2 , 3 , - 2 , 3 ] ; let n = a . length ; document . write ( maximizeSum ( a , n ) ) ;
3 | Function that returns true if n is an Osiris number ; 3 rd digit ; 2 nd digit ; 1 st digit ; Check the required condition ; Driver code
function isOsiris ( n ) { let a = n % 10 ; let b = parseInt ( ( n / 10 ) % 10 ) ; let c = parseInt ( n / 100 ) ; let digit_sum = a + b + c ; if ( n == ( 2 * ( digit_sum ) * 11 ) ) { return true ; } return false ; } let n = 132 ; if ( isOsiris ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Dudeney Numbers | Function that returns true if n is a Dudeney number ; If n is not a perfect cube ; Last digit ; Update the digit sum ; Remove the last digit ; If cube root of n is not equal to the sum of its digits ; Driver code
function isDudeney ( n ) { let cube_rt = parseInt ( Math . round ( ( Math . pow ( n , 1.0 / 3.0 ) ) ) ) ; if ( cube_rt * cube_rt * cube_rt != n ) return false ; let dig_sum = 0 ; let temp = n ; while ( temp > 0 ) { let rem = temp % 10 ; dig_sum += rem ; temp = parseInt ( temp / 10 ) ; } if ( cube_rt != dig_sum ) return false ; return true ; } let n = 17576 ; if ( isDudeney ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Number of triangles possible with given lengths of sticks which are powers of 2 | Function to return the number of positive area triangles ; To store the count of total triangles ; To store the count of pairs of sticks with equal lengths ; Back - traverse and count the number of triangles ; Count the number of pairs ; If we have one remaining stick and we have a pair ; Count 1 triangle ; Reduce one pair ; Count the remaining triangles that can be formed ; Driver code
function countTriangles ( a , n ) { var cnt = 0 ; var pairs = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { pairs += a [ i ] / 2 ; if ( a [ i ] % 2 == 1 && pairs > 0 ) { cnt += 1 ; pairs -= 1 ; } } cnt += parseInt ( ( 2 * pairs ) / 3 ) ; return cnt ; } var a = [ 1 , 2 , 2 , 2 , 2 ] ; var n = a . length ; document . write ( countTriangles ( a , n ) ) ;
Smallest N digit number which is a multiple of 5 | Function to return the smallest n digit number which is a multiple of 5 ; Driver code
function smallestMultiple ( n ) { if ( n == 1 ) return 5 ; return Math . pow ( 10 , n - 1 ) ; } var n = 4 ; document . write ( smallestMultiple ( n ) ) ;
Find HCF of two numbers without using recursion or Euclidean algorithm | Function to return the HCF of x and y ; Minimum of the two numbers ; If both the numbers are divisible by the minimum of these two then the HCF is equal to the minimum ; Highest number between 2 and minimum / 2 which can divide both the numbers is the required HCF ; If both the numbers are divisible by i ; 1 divides every number ; Driver code
function getHCF ( x , y ) { var minimum = Math . min ( x , y ) ; if ( x % minimum == 0 && y % minimum == 0 ) return minimum ; for ( var i = minimum / 2 ; i >= 2 ; i -- ) { if ( x % i == 0 && y % i == 0 ) return i ; } return 1 ; } var x = 16 , y = 32 ; document . write ( getHCF ( x , y ) ) ;
Check if product of first N natural numbers is divisible by their sum | Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that return true if the product of the first n natural numbers is divisible by the sum of first n natural numbers ; 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 ( i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function isDivisible ( n ) { if ( isPrime ( n + 1 ) ) return false ; return true ; } var n = 6 ; if ( isDivisible ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum sum of cocktail glass in a 2D matrix | Javascript implementation of the approach ; Function to return the maximum sum of a cocktail glass ; If no cocktail glass is possible ; Initialize max_sum with the mini ; Here loop runs ( R - 2 ) * ( C - 2 ) times considering different top left cells of cocktail glasses ; Considering mat [ i ] [ j ] as the top left cell of the cocktail glass ; Update the max_sum ; Driver code
var R = 5 ; var C = 5 ; function findMaxCock ( ar ) { if ( R < 3 C < 3 ) return - 1 ; var max_sum = - 1000000000 ; for ( var i = 0 ; i < R - 2 ; i ++ ) { for ( var j = 0 ; j < C - 2 ; j ++ ) { var sum = ( ar [ i ] [ j ] + ar [ i ] [ j + 2 ] ) + ( ar [ i + 1 ] [ j + 1 ] ) + ( ar [ i + 2 ] [ j ] + ar [ i + 2 ] [ j + 1 ] + ar [ i + 2 ] [ j + 2 ] ) ; max_sum = Math . max ( max_sum , sum ) ; } } return max_sum ; } ar = [ [ 0 , 3 , 0 , 6 , 0 ] , [ 0 , 1 , 1 , 0 , 0 ] , [ 1 , 1 , 1 , 0 , 0 ] , [ 0 , 0 , 2 , 0 , 1 ] , [ 0 , 2 , 0 , 1 , 3 ] ] ; document . write ( findMaxCock ( ar ) ) ;
Find the number of sub arrays in the permutation of first N natural numbers such that their median is M | Function to return the count of sub - arrays in the given permutation of first n natural numbers such that their median is m ; If element is less than m ; If element greater than m ; If m is found ; Count the answer ; Increment sum ; Driver code
function segments ( n , p , m ) { var c = new Map ( ) ; c . set ( 0 , 1 ) ; var hs = false ; var sum = 0 ; var ans = 0 ; var r ; for ( r = 0 ; r < n ; r ++ ) { if ( p [ r ] < m ) sum -- ; else if ( p [ r ] > m ) sum ++ ; if ( p [ r ] == m ) hs = true ; if ( hs ) { if ( c . has ( sum ) && c . has ( sum - 1 ) ) ans += c . get ( sum ) + c . get ( sum - 1 ) ; else if ( c . has ( sum ) ) ans += c . get ( sum ) ; else if ( c . has ( sum - 1 ) ) ans += c . get ( sum - 1 ) ; } else { if ( c . has ( sum ) ) c . set ( sum , c . get ( sum ) + 1 ) ; else c . set ( sum , 1 ) ; } } return ans ; } var a = [ 2 , 4 , 5 , 3 , 1 ] ; var n = a . length ; var m = 4 ; document . write ( segments ( n , a , m ) ) ;
Program to calculate the number of odd days in given number of years | Function to return the count of odd days ; Count of years divisible by 100 and 400 ; Every 4 th year is a leap year ; Every 100 th year is divisible by 4 but is not a leap year ; Every 400 th year is divisible by 100 but is a leap year ; Total number of extra days ; modulo ( 7 ) for final answer ; Number of days
function oddDays ( N ) { var hund1 = N / 100 ; var hund4 = N / 400 ; var leap = N >> 2 ; var ord = N - leap ; if ( hund1 > 0 ) { ord += hund1 ; leap -= hund1 ; } if ( hund4 > 0 ) { ord -= hund4 ; leap += hund4 ; } var days = ord + leap * 2 ; var odd = days % 7 ; return odd ; } var N = 100 ; document . write ( oddDays ( N ) . toFixed ( ) ) ;
Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle | Function to find the area of the biggest ellipse ; the radius cannot be negative ; area of the ellipse ; Driver code
function ellipsearea ( r ) { if ( r < 0 ) return - 1 ; var a = ( ( 3.14 * r * r ) / 4 ) ; return a ; } var r = 5 ; document . write ( ellipsearea ( r ) ) ;
Find the minimum number of operations required to make all array elements equal | Function to return the minimum operations required to make all array elements equal ; To store the frequency of all the array elements ; Traverse through array elements and update frequencies ; To store the maximum frequency of an element from the array ; Traverse through the map and find the maximum frequency for any element ; Return the minimum operations required ; Driver code
function minOperations ( arr , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) } else { mp . set ( arr [ i ] , 1 ) } } let maxFreq = Number . MIN_SAFE_INTEGER ; for ( let x of mp ) maxFreq = Math . max ( maxFreq , x [ 1 ] ) ; return ( n - maxFreq ) ; } let arr = [ 2 , 4 , 6 ] ; let n = arr . length ; document . write ( minOperations ( arr , n ) ) ;
Count all prefixes of the given binary array which are divisible by x | Function to return the count of total binary prefix which are divisible by x ; Initialize with zero ; Convert all prefixes to decimal ; If number is divisible by x then increase count ; Driver code
function CntDivbyX ( arr , n , x ) { let number = 0 ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { number = number * 2 + arr [ i ] ; if ( ( number % x == 0 ) ) count += 1 ; } return count ; } let arr = [ 1 , 0 , 1 , 0 , 1 , 1 , 0 ] ; let n = arr . length ; let x = 2 ; document . write ( CntDivbyX ( arr , n , x ) ) ;
Count consecutive pairs of same elements | Function to return the count of consecutive elements in the array which are equal ; If consecutive elements are same ; Driver Code
function countCon ( ar , n ) { let cnt = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( ar [ i ] == ar [ i + 1 ] ) cnt ++ ; } return cnt ; } let ar = [ 1 , 2 , 2 , 3 , 4 , 4 , 5 , 5 , 5 , 5 ] ; let n = ar . length ; document . write ( countCon ( ar , n ) ) ;
Reduce the fraction to its lowest form | Function to reduce a fraction to its lowest form ; Driver Code
function reduceFraction ( x , y ) { let d ; d = __gcd ( x , y ) ; x = parseInt ( x / d ) ; y = parseInt ( y / d ) ; document . write ( " " + x + " " + y ) ; } function __gcd ( a , b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } let x = 16 ; let y = 10 ; reduceFraction ( x , y ) ;
Number of ways of choosing K equal substrings of any length for every query | JavaScript implementation of the approach ; Function to generate all the sub - strings ; Length of the string ; Generate all sub - strings ; Count the occurrence of every sub - string ; Compute the Binomial Coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the result for a query ; Iterate for every unique sub - string ; Count the combinations ; Driver code ; Get all the sub - strings Store the occurrence of all the sub - strings ; Pre - computation ; Queries ; Perform queries
let maxlen = 100 ; function generateSubStrings ( s , mpp ) { let l = s . length ; for ( let i = 0 ; i < l ; i ++ ) { let temp = " " ; for ( let j = i ; j < l ; j ++ ) { temp += s [ j ] ; if ( mpp . has ( temp ) ) { let x = mpp . get ( temp ) ; mpp . set ( temp , ++ x ) ; } else mpp . set ( temp , 1 ) ; } } } function binomialCoeff ( C ) { let i , j ; for ( i = 1 ; i < 100 ; i ++ ) { for ( j = 0 ; j < 100 ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } } function answerQuery ( mpp , C , k ) { let ans = 0 ; for ( let [ key , value ] of mpp . entries ( ) ) { if ( value >= k ) ans += C [ value ] [ k ] ; } return ans ; } let s = " " ; let mpp = new Map ( ) ; generateSubStrings ( s , mpp ) ; let C = new Array ( maxlen ) ; for ( let i = 0 ; i < maxlen ; i ++ ) { C [ i ] = new Array ( maxlen ) ; for ( let j = 0 ; j < maxlen ; j ++ ) C [ i ] [ j ] = 0 ; } binomialCoeff ( C ) ; let queries = [ 2 , 3 , 4 ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) document . write ( answerQuery ( mpp , C , queries [ i ] ) + " " ) ;
Times required by Simple interest for the Principal to become Y times itself | Function to return the no . of years ; Driver code
function noOfYears ( t1 , n1 , t2 ) { var years = ( ( t2 - 1 ) * n1 / ( t1 - 1 ) ) ; return years ; } var T1 = 3 , N1 = 5 , T2 = 6 ; document . write ( noOfYears ( T1 , N1 , T2 ) ) ;