codeparrot/xlcost-text-to-code huggingface dataset:
Largest number less than or equal to Z that leaves a remainder X when divided by Y | Function to get the number ; remainder can ' t ▁ be ▁ larger ▁ ▁ than ▁ the ▁ largest ▁ number , ▁ ▁ if ▁ so ▁ then ▁ answer ▁ doesn ' t exist . ; reduce number by x ; finding the possible number that is divisible by y ; this number is always <= x as we calculated over z - x ; initialise the three integers
function get ( x , y , z ) { if ( x > z ) return - 1 ; let val = z - x ; let div = Math . floor ( ( z - x ) / y ) ; let ans = div * y + x ; return ans ; } let x = 1 , y = 5 , z = 8 ; document . write ( get ( x , y , z ) + " " ) ;
Find the conjugate of a Complex number | Function to find conjugate of a complex number ; store index of ' + ' ; store index of ' - ' ; print the result ; Array of points
function solve ( s ) { let z = s ; var l = s . length ; var i ; if ( s . indexOf ( ' ' ) != - 1 ) { i = s . indexOf ( ' ' ) ; s = s . replace ( ' ' , ' ' ) ; } else { i = s . indexOf ( ' ' ) ; s = s . replace ( ' ' , ' ' ) ; } document . write ( " " + z + " " + s + " " ) ; } let s = " " ; solve ( s ) ;
Minimum operations required to make two numbers equal | Function to return the minimum operations required ; Keeping B always greater ; Reduce B such that gcd ( A , B ) becomes 1. ; Driver code
function minOperations ( A , B ) { if ( A > B ) { A = A + B ; B = A - B ; A = A - B ; } B = B / __gcd ( A , B ) ; return B - 1 ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var A = 7 , B = 15 ; document . write ( minOperations ( A , B ) + " " ) ;
Program to determine the Quadrant of a Complex number | Function to determine the quadrant of a complex number ; Storing the index of ' + ' ; Storing the index of ' - ' ; Finding the real part of the complex number ; Finding the imaginary part of the complex number ; Driver code
function quadrant ( s ) { var l = s . length ; var i = 0 ; if ( s . indexOf ( " " ) != - 1 ) { i = s . indexOf ( " " ) ; } else { i = s . indexOf ( " " ) ; } var real = s . substr ( 0 , i ) ; var imaginary = s . substr ( i + 1 , l - 1 ) ; var x = parseInt ( real ) ; var y = parseInt ( imaginary ) ; if ( x > 0 && y > 0 ) document . write ( " " ) ; else if ( x < 0 && y > 0 ) document . write ( " " ) ; else if ( x < 0 && y < 0 ) document . write ( " " ) ; else if ( x > 0 && y < 0 ) document . write ( " " ) ; else if ( x == 0 && y > 0 ) document . write ( " " + " " ) ; else if ( x == 0 && y < 0 ) document . write ( " " + " " ) ; else if ( y == 0 && x < 0 ) document . write ( " " + " " ) ; else if ( y == 0 && x > 0 ) document . write ( " " + " " ) ; else document . write ( " " ) ; } var s = " " ; quadrant ( s ) ;
Sum and Product of all Fibonacci Nodes of a Singly Linked List | Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Allocate new node ; Insert the data ; Link the old list to the new node ; Move the head to povar the new node ; Function that returns the largest element from the linked list . ; Declare a max variable and initialize with Number . MIN_VALUE ; Check loop while head not equal to null ; If max is less then head . data then assign value of head . data to max otherwise node points to next node . ; Function to create a hash table to check Fibonacci numbers ; Inserting the first two numbers in the hash ; Loop to add Fibonacci numbers upto the maximum element present in the linked list ; Function to find the required sum and product ; Find the largest node value in Singly Linked List ; Creating a set containing all the fibonacci numbers upto the maximum data value in the Singly Linked List ; Traverse the linked list ; If current node is fibonacci ; Find the sum and the product ; Driver code ; Create the linked list 15.16 . 8.6 .13
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } function largestElement ( head_ref ) { var max = Number . MIN_VALUE ; var head = head_ref ; while ( head != null ) { if ( max < head . data ) max = head . data ; head = head . next ; } return max ; } function createHash ( hash , maxElement ) { var prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { var temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } function sumAndProduct ( head_ref ) { var maxEle = largestElement ( head_ref ) ; var hash = new Set ( ) ; createHash ( hash , maxEle ) ; var prod = 1 ; var sum = 0 ; var ptr = head_ref ; while ( ptr != null ) { if ( hash . has ( ptr . data ) ) { prod *= ptr . data ; sum += ptr . data ; } ptr = ptr . next ; } document . write ( " " + sum + " " ) ; document . write ( " " + prod ) ; } var head = null ; head = push ( head , 13 ) ; head = push ( head , 6 ) ; head = push ( head , 8 ) ; head = push ( head , 16 ) ; head = push ( head , 15 ) ; sumAndProduct ( head ) ;
Product of all Subarrays of an Array | Function to find product of all subarrays ; Variable to store the product ; Compute the product while traversing for subarrays ; Printing product of all subarray ; Driver code ; Function call
function product_subarrays ( arr , n ) { let product = 1 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { for ( let k = i ; k <= j ; k ++ ) product *= arr [ k ] ; } } document . write ( product + " " ) ; } let arr = [ 10 , 3 , 7 ] ; let n = arr . length ; product_subarrays ( arr , n ) ;
Check if a N base number is Even or Odd | To return value of a char . ; Function to convert a number from N base to decimal ; power of base ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 1 ] * base + str [ len - 1 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Returns true if n is even , else odd ; Driver Code
function val ( c ) { if ( c >= ' ' && c <= ' ' ) return c . charCodeAt ( ) - ' ' . charCodeAt ( ) ; else return c . charCodeAt ( ) - ' ' . charCodeAt ( ) + 10 ; } function toDeci ( str , base ) { let len = str . length ; let power = 1 ; let num = 0 ; let i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= base ) { document . write ( " " ) ; return - 1 ; } num += val ( str [ i ] ) * power ; power = power * base ; } return num ; } function isEven ( num , N ) { let deci = toDeci ( num , N ) ; return ( deci % 2 == 0 ) ; } let num = " " ; let N = 16 ; if ( isEven ( num , N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Find the next Factorial greater than N | Array that stores the factorial till 20 ; Function to pre - compute the factorial till 20 ; Precomputing factorials ; Function to return the next factorial number greater than N ; Traverse the factorial array ; Find the next just greater factorial than N ; Function to precalculate the factorial till 20 ; Function call
fact = Array ( 21 ) . fill ( 0 ) ; function preCompute ( ) { fact [ 0 ] = 1 ; for ( var i = 1 ; i < 18 ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) ; } function nextFactorial ( N ) { for ( var i = 0 ; i < 21 ; i ++ ) { if ( N < fact [ i ] ) { document . write ( fact [ i ] ) ; break ; } } } preCompute ( ) ; var N = 120 ; nextFactorial ( N ) ;
Find K distinct positive odd integers with sum N | Function to find K odd positive integers such that their sum is N ; Condition to check if there are enough values to check ; Driver Code
function findDistinctOddSum ( n , k ) { if ( ( k * k ) <= n && ( n + k ) % 2 == 0 ) { var val = 1 ; var sum = 0 ; for ( var i = 1 ; i < k ; i ++ ) { document . write ( val + " " ) ; sum += val ; val += 2 ; } document . write ( n - sum ) ; } else document . write ( " " ) ; } var n = 100 ; var k = 4 ; findDistinctOddSum ( n , k ) ;
Minimum number of operations to convert array A to array B by adding an integer into a subarray | Function to find the minimum number of operations in which array A can be converted to array B ; Loop to iterate over the array ; if both elements are equal then move to next element ; Calculate the difference between two elements ; loop while the next pair of elements have same difference ; Increase the number of operations by 1 ; Prvar the number of operations required ; Driver Code
function checkArray ( a , b , n ) { var operations = 0 ; var i = 0 ; while ( i < n ) { if ( a [ i ] - b [ i ] == 0 ) { i ++ ; continue ; } var diff = a [ i ] - b [ i ] ; i ++ ; while ( i < n && a [ i ] - b [ i ] == diff ) { i ++ ; } operations ++ ; } document . write ( operations ) ; } var a = [ 3 , 7 , 1 , 4 , 1 , 2 ] ; var b = [ 3 , 7 , 3 , 6 , 3 , 2 ] ; var size = a . length ; checkArray ( a , b , size ) ;
Perfect Cube | Inserts the prime factor in HashMap if not present if present updates it 's frequency ; A utility function to find all prime factors of a given number N ; Insert the number of 2 s that divide n ; n must be odd at this point So we can skip one element ; while i divides n , insert i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to check if a number is perfect cube ; Iteration in Map ; Driver Code ; Function to check if N is perfect cube or not
function insertPF ( primeFact , fact ) { if ( primeFact . has ( fact ) ) { primeFact . set ( fact , primeFact . get ( fact ) + 1 ) ; } else { primeFact . set ( fact , 1 ) ; } return primeFact ; } function primeFactors ( n ) { var primeFact = new Map ( ) ; while ( n % 2 == 0 ) { primeFact = insertPF ( primeFact , 2 ) ; n = parseInt ( n / 2 ) ; } for ( var i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { primeFact = insertPF ( primeFact , i ) ; n = parseInt ( n / i ) ; } } if ( n > 2 ) primeFact = insertPF ( primeFact , n ) ; return primeFact ; } function perfectCube ( n ) { var primeFact = new Map ( ) ; primeFact = primeFactors ( n ) ; primeFact . forEach ( ( value , key ) => { if ( value % 3 != 0 ) return " " ; } ) ; return " " ; } var N = 216 ; document . write ( perfectCube ( N ) ) ;
Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3 | JavaScript implementation to find the number the number of ways to reach Nth stair by taking 1 or 2 steps at a time and 3 rd step exactly once ; Single line to find factorial ; Function to find the number of ways ; Base Case ; Count of 2 - steps ; Count of 1 - steps ; Initial length of sequence ; Expected count of 2 - steps ; Loop to find the ways for every possible sequence ; Driver code
function factorial ( n ) { return ( n == 1 n == 0 ) ? 1 : n * factorial ( n - 1 ) ; } function ways ( n ) { if ( n < 3 ) { return 0 ; } let c2 = 0 ; let c1 = n - 3 ; let l = c1 + 1 ; let s = 0 ; let exp_c2 = Math . floor ( c1 / 2 ) ; while ( exp_c2 >= c2 ) { let f1 = factorial ( l ) ; let f2 = factorial ( c1 ) ; let f3 = factorial ( c2 ) ; let f4 = ( f2 * f3 ) ; s += Math . floor ( f1 / f4 ) ; c2 += 1 ; c1 -= 2 ; l -= 1 ; } return s ; } let n = 7 ; let ans = ways ( n ) ; document . write ( ans ) ;
Find N from the value of N ! | Map to precompute and store the factorials of the numbers ; Function to precompute factorial ; Calculating the factorial for each i and storing in a map ; Driver code
var m = { } ; function precompute ( ) { var fact = 1 ; for ( var i = 1 ; i <= 18 ; i ++ ) { fact = fact * i ; m [ fact ] = i ; } } precompute ( ) ; var K = 120 ; document . write ( m [ K ] + " " ) ; K = 6 ; document . write ( m [ K ] ) ;
Count of Leap Years in a given year range | Function to calculate the number of leap years in range of ( 1 , year ) ; Function to calculate the number of leap years in given range ; Driver Code
function calNum ( year ) { return parseInt ( year / 4 , 10 ) - parseInt ( year / 100 , 10 ) + parseInt ( year / 400 , 10 ) ; } function leapNum ( l , r ) { l -- ; let num1 = calNum ( r ) ; let num2 = calNum ( l ) ; document . write ( ( num1 - num2 ) + " " ) ; } let l1 = 1 , r1 = 400 ; leapNum ( l1 , r1 ) ; let l2 = 400 , r2 = 2000 ; leapNum ( l2 , r2 ) ;
Ternary representation of Cantor set | The Linked List Structure for the Cantor Set ; Function to initialize the Cantor Set List ; Function to propogate the list by adding new nodes for the next levels ; Modifying the start and end values for the next level ; Changing the pointers to the next node ; Recursively call the function to generate the Cantor Set for the entire level ; Function to print a level of the Set ; Function to build and display the Cantor Set for each level ; Driver code
class Cantor { constructor ( ) { this . start = 0 ; this . end = 0 ; this . next = null ; } } ; var cantor = null ; function startList ( head , start_num , end_num ) { if ( head == null ) { head = new Cantor ( ) ; head . start = start_num ; head . end = end_num ; head . next = null ; } return head ; } function propagate ( head ) { var temp = head ; if ( temp != null ) { var newNode = new Cantor ( ) ; var diff = ( ( ( temp . end ) - ( temp . start ) ) / 3 ) ; newNode . end = temp . end ; temp . end = ( ( temp . start ) + diff ) ; newNode . start = ( newNode . end ) - diff ; newNode . next = temp . next ; temp . next = newNode ; propagate ( temp . next . next ) ; } return head ; } function print ( temp ) { while ( temp != null ) { document . write ( " " + temp . start . toFixed ( 6 ) + " " + temp . end . toFixed ( 6 ) + " " ) ; temp = temp . next ; } document . write ( " " ) ; } function buildCantorSet ( A , B , L ) { var head = null ; head = startList ( head , A , B ) ; for ( var i = 0 ; i < L ; i ++ ) { document . write ( " " + i + " " ) ; print ( head ) ; propagate ( head ) ; } document . write ( " " + L + " " ) ; print ( head ) ; } var A = 0 ; var B = 9 ; var L = 2 ; buildCantorSet ( A , B , L ) ;
Length of Smallest subarray in range 1 to N with sum greater than a given value | Function to return the count of minimum elements such that the sum of those elements is > S . ; Initialize currentSum = 0 ; Loop from N to 1 to add the numbers and check the condition . ; Driver code
function countNumber ( N , S ) { let countElements = 0 ; let currSum = 0 ; while ( currSum <= S ) { currSum += N ; N -- ; countElements ++ ; } return countElements ; } let N , S ; N = 5 ; S = 11 ; let count = countNumber ( N , S ) ; document . write ( count + " " ) ;
Next Number with distinct digits | Javascript program to find next consecutive Number with all distinct digits ; Function to count distinct digits in a number ; To count the occurrence of digits in number from 0 to 9 ; Iterate over the digits of the number Flag those digits as found in the array ; Traverse the array arr and count the distinct digits in the array ; Function to return the total number of digits in the number ; Iterate over the digits of the number ; Function to return the next number with distinct digits ; Count the distinct digits in N + 1 ; Count the total number of digits in N + 1 ; Return the next consecutive number ; Increment Number by 1 ; Driver code
let INT_MAX = Number . MAX_VALUE ; function countDistinct ( n ) { let arr = new Array ( 10 ) ; arr . fill ( 0 ) ; let count = 0 ; while ( n != 0 ) { let r = n % 10 ; arr [ r ] = 1 ; n = parseInt ( n / 10 , 10 ) ; } for ( let i = 0 ; i < 10 ; i ++ ) { if ( arr [ i ] != 0 ) count ++ ; } return count ; } function countDigit ( n ) { let c = 0 ; while ( n != 0 ) { let r = n % 10 ; c ++ ; n = parseInt ( n / 10 , 10 ) ; } return c ; } function nextNumberDistinctDigit ( n ) { while ( n < INT_MAX ) { let distinct_digits = countDistinct ( n + 1 ) ; let total_digits = countDigit ( n + 1 ) ; if ( distinct_digits == total_digits ) { return n + 1 ; } else n ++ ; } return - 1 ; } let n = 2019 ; document . write ( nextNumberDistinctDigit ( n ) ) ;
Find Number of Even cells in a Zero Matrix after Q queries | Function to find the number of even cell in a 2D matrix ; Maintain two arrays , one for rows operation and one for column operation ; Increment operation on row [ i ] ; Increment operation on col [ i ] ; Count odd and even values in both arrays and multiply them ; Count of rows having even numbers ; Count of rows having odd numbers ; Count of columns having even numbers ; Count of columns having odd numbers ; Driver code
function findNumberOfEvenCells ( n , q , size ) { let row = new Array ( n ) ; row . fill ( 0 ) ; let col = new Array ( n ) ; col . fill ( 0 ) ; for ( let i = 0 ; i < size ; i ++ ) { let x = q [ i ] [ 0 ] ; let y = q [ i ] [ 1 ] ; row [ x - 1 ] ++ ; col [ y - 1 ] ++ ; } let r1 = 0 , r2 = 0 ; let c1 = 0 , c2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( row [ i ] % 2 == 0 ) { r1 ++ ; } if ( row [ i ] % 2 == 1 ) { r2 ++ ; } if ( col [ i ] % 2 == 0 ) { c1 ++ ; } if ( col [ i ] % 2 == 1 ) { c2 ++ ; } } let count = r1 * c1 + r2 * c2 ; return count ; } let n = 2 ; let q = [ [ 1 , 1 ] , [ 1 , 2 ] , [ 2 , 1 ] ] ; let size = q . length ; document . write ( findNumberOfEvenCells ( n , q , size ) ) ;
Find maximum unreachable height using two ladders | Function to return the maximum height which can 't be reached ; Driver code
function maxHeight ( h1 , h2 ) { return ( ( h1 * h2 ) - h1 - h2 ) ; } var h1 = 7 , h2 = 5 ; document . write ( Math . max ( 0 , maxHeight ( h1 , h2 ) ) ) ;
Fermat 's Factorization Method | This function finds the value of a and b and returns a + b and a - b ; since fermat 's factorization applicable  for odd positive integers only ; check if n is a even number ; if n is a perfect root , then both its square roots are its factors ; Driver Code
function FermatFactors ( n ) { if ( n <= 0 ) { document . write ( " " + n + " " ) ; return ; } if ( ( n & 1 ) == 0 ) { document . write ( " " + ( n / 2.0 ) + " " + ( 2 ) + " " ) ; return ; } let a = Math . ceil ( Math . sqrt ( n ) ) ; if ( a * a == n ) { document . write ( " " + a + " " + a + " " ) ; return ; } let b ; while ( true ) { let b1 = a * a - n ; b = parseInt ( Math . sqrt ( b1 ) , 10 ) ; if ( b * b == b1 ) break ; else a += 1 ; } document . write ( " " + ( a - b ) + " " + ( a + b ) + " " ) ; return ; } FermatFactors ( 6557 ) ;
Append two elements to make the array satisfy the given condition | Function to find the required numbers ; Find the sum and xor ; Prvar the required elements ; Driver code
function findNums ( arr , n ) { var S = 0 , X = 0 ; for ( i = 0 ; i < n ; i ++ ) { S += arr [ i ] ; X ^= arr [ i ] ; } document . write ( X + " " + ( X + S ) ) ; } var arr = [ 1 , 7 ] ; var n = arr . length ; findNums ( arr , n ) ;
Satisfy the parabola when point ( A , B ) and the equation is given | Function to find the required values ; Driver code
function solve ( A , B ) { let p = B / 2.0 ; let M = Math . ceil ( 4 * p ) ; let N = 1 ; let O = - 2 * A ; let Q = Math . ceil ( A * A + 4 * p * p ) ; document . write ( M + " " + N + " " + O + " " + Q ) ; } let a = 1 ; let b = 1 ; solve ( a , b ) ;
Largest number dividing maximum number of elements in the array | Function to return the largest number that divides the maximum elements from the given array ; Finding gcd of all the numbers in the array ; Driver code
function findLargest ( arr , n ) { var gcd = 0 ; for ( i = 0 ; i < n ; i ++ ) gcd = __gcd ( arr [ i ] , gcd ) ; return gcd ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var arr = [ 3 , 6 , 9 ] ; var n = arr . length ; document . write ( findLargest ( arr , n ) ) ;
Check if the sum of digits of N is palindrome | Function to return the sum of digits of n ; Function that returns true if n is palindrome ; Find the appropriate divisor to extract the leading digit ; If first and last digit not same return false ; Removing the leading and trailing digit from number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Function that returns true if the digit sum of n is palindrome ; Sum of the digits of n ; If the digit sum is palindrome ; Driver code
function digitSum ( n ) { let sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n = parseInt ( n / 10 , 10 ) ; } return sum ; } function isPalindrome ( n ) { let divisor = 1 ; while ( parseInt ( n / divisor , 10 ) >= 10 ) divisor *= 10 ; while ( n != 0 ) { let leading = parseInt ( n / divisor , 10 ) ; let trailing = n % 10 ; if ( leading != trailing ) return false ; n = parseInt ( ( n % divisor ) / 10 , 10 ) ; divisor = parseInt ( divisor / 100 , 10 ) ; } return true ; } function isDigitSumPalindrome ( n ) { let sum = digitSum ( n ) ; if ( isPalindrome ( sum ) ) return true ; return false ; } let n = 56 ; if ( isDigitSumPalindrome ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the value of N XOR 'ed to itself K times | Function to return n ^ n ^ ... k times ; If k is odd the answer is the number itself ; Else the answer is 0 ; Driver code
function xorK ( n , k ) { if ( k % 2 == 1 ) return n ; return 0 ; } let n = 123 , k = 3 ; document . write ( xorK ( n , k ) ) ;
Find the sum of the costs of all possible arrangements of the cells | JavaScript implementation of the approach ; To store the factorials and factorial mod inverse of the numbers ; Function to return ( a ^ m1 ) % mod ; Function to find the factorials of all the numbers ; Function to find factorial mod inverse of all the numbers ; Function to return nCr ; Function to return the sum of the costs of all the possible arrangements of the cells ; For all possible X 's ; For all possible Y 's ; Driver code
let N = 20 ; let mod = 1000000007 ; let factorial = new Array ( N ) ; factorial . fill ( 0 ) ; let modinverse = new Array ( N ) ; modinverse . fill ( 0 ) ; function power ( a , m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( a * a ) % mod ; else if ( ( m1 & 1 ) != 0 ) return ( a * power ( power ( a , parseInt ( m1 / 2 , 10 ) ) , 2 ) ) % mod ; else return power ( power ( a , parseInt ( m1 / 2 , 10 ) ) , 2 ) % mod ; } function factorialfun ( ) { factorial [ 0 ] = 1 ; for ( let i = 1 ; i < N ; i ++ ) factorial [ i ] = ( factorial [ i - 1 ] * i ) % mod ; } function modinversefun ( ) { modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; for ( let i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } function binomial ( n , r ) { if ( r > n ) return 0 ; let a = ( factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( a * modinverse [ r ] ) % mod ; return a ; } function arrange ( n , m , k ) { factorialfun ( ) ; modinversefun ( ) ; let ans = 0 ; for ( let i = 1 ; i < n ; i ++ ) ans += ( i * ( n - i ) * m * m ) % mod ; ans = 8 ; for ( let i = 1 ; i < m ; i ++ ) ans += ( i * ( m - i ) * n * n ) % mod ; ans = ( ans * binomial ( n * m - 2 , k - 2 ) * 0 ) % mod + 8 ; return ans ; } let n = 2 , m = 2 , k = 2 ; document . write ( arrange ( n , m , k ) ) ;
Find the Nth digit in the proper fraction of two numbers | Function to print the Nth digit in the fraction ( p / q ) ; To store the resultant digit ; While N > 0 compute the Nth digit by dividing p and q and store the result into variable res and go to next digit ; Driver code
function findNthDigit ( p , q , N ) { var res ; while ( N > 0 ) { N -- ; p *= 10 ; res = parseInt ( p / q ) ; p %= q ; } return res ; } var p = 1 , q = 2 , N = 1 ; document . write ( findNthDigit ( p , q , N ) ) ;
Sum of the updated array after performing the given operation | Utility function to return the sum of the array ; Function to return the sum of the modified array ; Subtract the subarray sum ; Sum of subarray arr [ i ... n - 1 ] ; Return the sum of the modified array ; Driver code
function sumArr ( arr , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } function sumModArr ( arr , n ) { var subSum = arr [ n - 1 ] ; for ( var i = n - 2 ; i >= 0 ; i -- ) { var curr = arr [ i ] ; arr [ i ] -= subSum ; subSum += curr ; } return sumArr ( arr , n ) ; } var arr = [ 40 , 25 , 12 , 10 ] ; var n = arr . length ; document . write ( sumModArr ( arr , n ) ) ;
Find closest integer with the same weight | Javascript implementation of the approach ; Function to return the number closest to x which has equal number of set bits as x ; Loop for each bit in x and compare with the next bit ; Driver code
let NumUnsignBits = 64 ; function findNum ( x ) { for ( let i = 0 ; i < NumUnsignBits - 1 ; i ++ ) { if ( ( ( x >> i ) & 1 ) != ( ( x >> ( i + 1 ) ) & 1 ) ) { x ^= ( 1 << i ) | ( 1 << ( i + 1 ) ) ; return x ; } } return Number . MIN_VALUE ; } let n = 92 ; document . write ( findNum ( n ) ) ;