codeparrot/xlcost-text-to-code huggingface dataset:
Minimum sum possible by removing all occurrences of any array element | Function to find minimum sum after deletion ; Stores frequency of array elements ; Traverse the array ; Calculate sum ; Update frequency of the current element ; Stores the minimum sum required ; Traverse map ; Find the minimum sum obtained ; Return minimum sum ; Input array ; Size of array
function minSum ( A , N ) { let mp = new Map ( ) ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; mp [ A [ i ] ] ++ ; if ( mp . has ( A [ i ] ) ) { mp . set ( A [ i ] , mp . get ( A [ i ] ) + 1 ) } else { mp . set ( A [ i ] , 1 ) } } let minSum = Number . MAX_SAFE_INTEGER ; for ( let it of mp ) { minSum = Math . min ( minSum , sum - ( it [ 0 ] * it [ 1 ] ) ) ; } return minSum ; } let arr = [ 4 , 5 , 6 , 6 ] ; let N = arr . length document . write ( minSum ( arr , N ) + " " ) ;
Maximum difference between a pair of adjacent elements by excluding every element once | Function to calculate maximum difference between adjacent elements excluding every array element once ; Traverse the array ; Stores the maximum diff ; Check for maximum adjacent element ; Exclude current element ; Update maximum difference ; Update previous value ; Append the result into a vector ; Print the result ; Driver Code
function maxAdjacent ( arr , N ) { var res = [ ] ; for ( var i = 1 ; i < N - 1 ; i ++ ) { var prev = arr [ 0 ] ; var maxi = Number . MIN_VALUE ; for ( var j = 1 ; j < N ; j ++ ) { if ( i == j ) continue ; maxi = Math . max ( maxi , Math . abs ( arr [ j ] - prev ) ) ; prev = arr [ j ] ; } res . push ( maxi ) ; } for ( var j = 0 ; j < res . length ; j ++ ) document . write ( res [ j ] + " " ) ; document . write ( " " ) ; } var arr = [ 1 , 3 , 4 , 7 , 8 ] ; var N = arr . length ; maxAdjacent ( arr , N ) ;
Count 1 s present in a range of indices [ L , R ] in a given array | Function to find the size of the array if the array initially contains a single element ; Base case ; P / 2 -> findSize ( N / 2 ) P % 2 -> 1 P / 2 -> findSize ( N / 2 ) ; Function to return the count of 1 s in the range [ L , R ] ; Base Case ; PART 1 -> N / 2 [ 1 , Siz_M ] ; Update the right end point of the range to min ( Siz_M , R ) ; PART 2 -> N % 2 [ SizM + 1 , Siz_M + 1 ] ; PART 3 -> N / 2 [ SizM + 2 , 2 * Siz_M - 1 ] Same as PART 1 Property of Symmetricity Shift the coordinates according to PART 1 Subtract ( Siz_M + 1 ) from both L , R ; Input ; Counts the number of 1 's in  the range [L, R]
function findSize ( N ) { if ( N == 0 ) return 1 ; if ( N == 1 ) return 1 ; let Size = 2 * findSize ( parseInt ( N / 2 , 10 ) ) + 1 ; return Size ; } function CountOnes ( N , L , R ) { if ( L > R ) { return 0 ; } if ( N <= 1 ) { return N ; } let ret = 0 ; let M = parseInt ( N / 2 , 10 ) ; let Siz_M = findSize ( M ) ; if ( L <= Siz_M ) { ret += CountOnes ( parseInt ( N / 2 , 10 ) , L , Math . min ( Siz_M , R ) ) ; } if ( L <= Siz_M + 1 && Siz_M + 1 <= R ) { ret += N % 2 ; } if ( Siz_M + 1 < R ) { ret += CountOnes ( parseInt ( N / 2 , 10 ) , Math . max ( 1 , L - Siz_M - 1 ) , R - Siz_M - 1 ) ; } return ret ; } let N = 7 , L = 2 , R = 5 ; document . write ( CountOnes ( N , L , R ) ) ;
Find the pair ( a , b ) with minimum LCM such that their sum is equal to N | Function to check if number is prime or not ; As 1 is neither prime nor composite return false ; Check if it is divided by any number then it is not prime , return false ; Check if n is not divided by any number then it is prime and hence return true ; Function to find the pair ( a , b ) such that sum is N & LCM is minimum ; Check if the number is prime ; Now , if it is not prime then find the least divisior ; Check if divides n then it is a factor ; Required output is a = n / i & b = n / i * ( n - 1 ) ; Driver Code ; Function call
function prime ( n ) { if ( n == 1 ) return false ; for ( i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } function minDivisior ( n ) { if ( prime ( n ) ) { document . write ( 1 + " " + ( n - 1 ) ) ; } else { for ( i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { document . write ( n / i + " " + ( n / i * ( i - 1 ) ) ) ; break ; } } } } var N = 4 ; minDivisior ( N ) ;
Find Landau 's function for a given number N | To store Landau 's function of the number ; Function to return gcd of 2 numbers ; Function to return LCM of two numbers ; Function to find max lcm value among all representations of n ; Calculate Landau 's value ; Recursive function to find different ways in which n can be written as sum of atleast one positive integers ; Check if sum becomes n , consider this representation ; Start from previous element in the representation till n ; Include current element from representation ; Call function again with reduced sum ; Backtrack - remove current element from representation ; Function to find the Landau 's function ; Using recurrence find different ways in which n can be written as a sum of atleast one + ve integers ; Print the result ; Given N ; Function Call
var Landau = - 1000000000 ; function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function lcm ( a , b ) { return ( a * b ) / gcd ( a , b ) ; } function findLCM ( arr ) { var nth_lcm = arr [ 0 ] ; for ( var i = 1 ; i < arr . length ; i ++ ) nth_lcm = lcm ( nth_lcm , arr [ i ] ) ; Landau = Math . max ( Landau , nth_lcm ) ; } function findWays ( arr , i , n ) { if ( n == 0 ) findLCM ( arr ) ; for ( var j = i ; j <= n ; j ++ ) { arr . push ( j ) ; findWays ( arr , j , n - j ) ; arr . pop ( ) ; } } function Landau_function ( n ) { arr = [ ] ; findWays ( arr , 1 , n ) ; document . write ( Landau ) ; } var N = 4 ; Landau_function ( N ) ;
Check if the remainder of N | Function to check if a number holds the condition ( N - 1 ) ! % N = N - 1 ; Corner cases ; Number divisible by 2 or 3 are not prime ; Iterate from 5 and keep checking for prime ; Function to check the expression for the value N ; Driver code
function isPrime ( n ) { if ( n == 1 ) return true ; 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 checkExpression ( n ) { if ( isPrime ( n ) ) document . write ( " " ) ; else document . write ( " " ) ; } let N = 3 ; checkExpression ( N ) ;
Check if it is possible to split given Array into K odd | Function to check if array can be split in required K subsets ; Store count of odd numbers ; Check if element is odd ; Check if split is possible ; Driver code
function checkArray ( n , k , arr ) { var cnt = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) cnt += 1 ; } if ( cnt >= k && cnt % 2 == k % 2 ) return true ; else return false ; } var arr = [ 1 , 3 , 4 , 7 , 5 , 3 , 1 ] ; var n = arr . length ; var k = 4 ; if ( checkArray ( n , k , arr ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sum of division of the possible pairs for the given Array | Function to compute the sum ; Counting frequency of each term and finding maximum among it ; Making cumulative frequency ; Taking the ceil value ; nos . in [ ( n - 0.5 ) X , ( n + 0.5 ) X ) range will add n to the ans ; Return the final result ; Driver Code
function func ( arr , n ) { let ans = 0 ; let maxx = 0 ; let freq = Array . from ( { length : 100005 } , ( _ , i ) => 0 ) ; let temp ; for ( let i = 0 ; i < n ; i ++ ) { temp = arr [ i ] ; freq [ temp ] ++ ; maxx = Math . max ( maxx , temp ) ; } for ( let i = 1 ; i <= maxx ; i ++ ) { freq [ i ] += freq [ i - 1 ] ; } for ( let i = 1 ; i <= maxx ; i ++ ) { if ( freq [ i ] != 0 ) { let j ; let cur = Math . ceil ( 0.5 * i ) - 1.0 ; for ( j = 1.5 ; ; j ++ ) { let val = Math . min ( maxx , ( Math . ceil ( i * j ) - 1.0 ) ) ; let times = ( freq [ i ] - freq [ i - 1 ] ) , con = ( j - 0.5 ) ; ans += times * con * ( freq [ val ] - freq [ cur ] ) ; cur = val ; if ( val == maxx ) break ; } } } return ans ; } let arr = [ 1 , 2 , 3 ] ; let n = arr . length ; document . write ( func ( arr , n ) ) ;
Count of elements to be inserted to make Array sum twice the XOR of Array | Function to find the minimum number of elements that need to be inserted such that the sum of the elements of the array is twice the XOR of the array ; Variable to store the Xor of all the elements ; Variable to store the sum of all elements ; Loop to find the Xor and the sum of the array ; If sum = 2 * Xor ; No need to insert more elements ; We insert one more element which is Sum ; We insert two more elements Sum + Xor and Xor . ; Print the number of elements inserted in the array ; Print the elements that are inserted in the array ; Driver code
function insert_element ( a , n ) { let Xor = 0 ; let Sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { Xor ^= a [ i ] ; Sum += a [ i ] ; } if ( Sum == 2 * Xor ) { document . write ( " " + " " ) ; return ; } if ( Xor == 0 ) { document . write ( " " + " " ) ; document . write ( Sum + " " ) ; return ; } let num1 = Sum + Xor ; let num2 = Xor ; document . write ( " " + " " ) ; document . write ( num1 + " " + num2 + " " ) ; } let a = [ 1 , 2 , 3 ] ; let n = a . length ; insert_element ( a , n ) ;
Check if roots of a Quadratic Equation are reciprocal of each other or not | Function to check if the roots of a quadratic equation are reciprocal of each other or not ; Driver code
function checkSolution ( a , b , c ) { if ( a == c ) document . write ( " " ) ; else document . write ( " " ) ; } let a = 2 , b = 0 , c = 2 ; checkSolution ( a , b , c ) ;
Sunny Number | Function check whether x is a perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; Function to check Sunny Number ; Check if ( N + 1 ) is a perfect square or not ; If ( N + 1 ) is not a perfect square ; Given Number ; Function call
function isPerfectSquare ( x ) { var sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } function checkSunnyNumber ( N ) { if ( isPerfectSquare ( N + 1 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var N = 8 ; checkSunnyNumber ( N ) ;
Count the numbers which can convert N to 1 using given operation | Function to count the numbers which can convert N to 1 using the given operation ; Iterate through all the integers ; Check if N can be converted to 1 ; Incrementing the count if it can be converted ; Driver code
function countValues ( n ) { let answer = 0 ; for ( let i = 2 ; i <= n ; i ++ ) { let k = n ; while ( k >= i ) { if ( k % i == 0 ) k /= i ; else k -= i ; } if ( k == 1 ) answer ++ ; } return answer ; } let N = 6 ; document . write ( countValues ( N ) ) ;
Find K numbers with sum equal to N and sum of their squares maximized | Function that prints the required K numbers ; Print 1 , K - 1 times ; Print ( N - K + 1 ) ;
function printKNumbers ( N , K ) { for ( let i = 0 ; i < K - 1 ; i ++ ) document . write ( 1 + " " ) ; document . write ( N - K + 1 ) ; } let N = 10 , K = 3 ; printKNumbers ( N , K ) ;
Print Nth Stepping or Autobiographical number | Function to find the Nth stepping natural number ; Declare the queue ; Enqueue 1 , 2 , ... , 9 in this order ; Perform K operation on queue ; Get the ith Stepping number ; Perform Dequeue from the Queue ; If x mod 10 is not equal to 0 ; then Enqueue 10 x + ( x mod 10 ) - 1 ; Enqueue 10 x + ( x mod 10 ) ; If x mod 10 is not equal to 9 ; then Enqueue 10 x + ( x mod 10 ) + 1 ; Return the dequeued number of the K - th operation as the Nth stepping number ; initialise K
function NthSmallest ( K ) { var Q = [ ] ; var x ; for ( var i = 1 ; i < 10 ; i ++ ) Q . push ( i ) ; for ( var i = 1 ; i <= K ; i ++ ) { x = Q [ 0 ] ; Q . shift ( ) ; if ( x % 10 != 0 ) { Q . push ( x * 10 + x % 10 - 1 ) ; } Q . push ( x * 10 + x % 10 ) ; if ( x % 10 != 9 ) { Q . push ( x * 10 + x % 10 + 1 ) ; } } return x ; } var N = 16 ; document . write ( NthSmallest ( N ) ) ;
Least number to be added to or subtracted from N to make it a Perfect Square | Function to return the Least number ; Get the perfect square before and after N ; Check which is nearest to N ; return the result ; Driver code
function nearest ( n ) { var prevSquare = parseInt ( Math . sqrt ( n ) ) ; var nextSquare = prevSquare + 1 ; prevSquare = prevSquare * prevSquare ; nextSquare = nextSquare * nextSquare ; if ( ( n - prevSquare ) < ( nextSquare - n ) ) { ans = parseInt ( ( prevSquare - n ) ) ; } else ans = parseInt ( ( nextSquare - n ) ) ; return ans ; } var n = 14 ; document . write ( nearest ( n ) + " " ) ; n = 16 ; document . write ( nearest ( n ) + " " ) ; n = 18 ; document . write ( nearest ( n ) + " " ) ;
Value of Pi ( Î ) up to 50 decimal places | Function that prints the value of pi upto N decimal places ; Find value of pi upto using acos ( ) function ; Print value of pi upto N decimal places ; Driver Code ; Function that prints the value of pi
function printValueOfPi ( N ) { let pi = 2 * Math . acos ( 0.0 ) ; document . write ( pi . toFixed ( 4 ) ) ; } let N = 4 ; printValueOfPi ( N ) ;
Invert the Kth most significant bit of N | Function to convert decimal number n to its binary representation stored as an array arr [ ] ; Function to convert the number represented as a binary array arr [ ] into its decimal equivalent ; Function to return the updated integer after flipping the kth bit ; Number of bits in n ; Find the binary representation of n ; The number of bits in n are less than k ; Flip the kth bit ; Return the decimal equivalent of the number ; Driver code
function decBinary ( arr , n ) { let k = parseInt ( Math . log2 ( n ) , 10 ) ; while ( n > 0 ) { arr [ k -- ] = n % 2 ; n = parseInt ( n / 2 , 10 ) ; } } function binaryDec ( arr , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) ans += arr [ i ] << ( n - i - 1 ) ; return ans ; } function getNum ( n , k ) { let l = parseInt ( Math . log2 ( n ) , 10 ) + 1 ; let a = new Array ( l ) ; a . fill ( 0 ) ; decBinary ( a , n ) ; if ( k > l ) return n ; a [ k - 1 ] = ( a [ k - 1 ] == 0 ) ? 1 : 0 ; return binaryDec ( a , l ) ; } let n = 56 , k = 2 ; document . write ( getNum ( n , k ) ) ;
Queries for the product of first N factorials | Javascript implementation of the approach ; Declare result array globally ; Function to precompute the product of factorials upto MAX ; Initialize base condition if n = 0 then factorial of 0 is equal to 1 and answer for n = 0 is 1 ; Iterate loop from 1 to MAX ; factorial ( i ) = factorial ( i - 1 ) * i ; Result for current n is equal to result [ i - 1 ] multiplied by the factorial of i ; Function to perform the queries ; Precomputing the result till MAX ; Perform queries ; Driver code
let MAX = 1000000 ; let MOD = 10000007 ; let result = new Array ( MAX + 1 ) ; result . fill ( 0 ) ; let fact = new Array ( MAX + 1 ) ; fact . fill ( 0 ) ; function preCompute ( ) { fact [ 0 ] = 1 ; result [ 0 ] = 1 ; for ( let i = 1 ; i <= MAX ; i ++ ) { fact [ i ] = ( ( fact [ i - 1 ] % MOD ) * i ) % MOD ; result [ i ] = ( ( result [ i - 1 ] % MOD ) * ( fact [ i ] % MOD ) ) % MOD ; } } function performQueries ( q , n ) { preCompute ( ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( result [ q [ i ] ] + " " ) ; } let q = [ 4 , 5 ] ; let n = q . length ; performQueries ( q , n ) ;
Nth number in a set of multiples of A , B or C | Function to return gcd of a and b ; Function to return the count of integers from the range [ 1 , num ] which are divisible by either a , b or c ; Calculate the number of terms divisible by a , b and c then remove the terms which are divisible by both ( a , b ) or ( b , c ) or ( c , a ) and then add the numbers which are divisible by a , b and c ; Function for binary search to find the nth term divisible by a , b or c ; Set low to 1 and high to LONG_MAX ; If the current term is less than n then we need to increase low to mid + 1 ; If current term is greater than equal to n then high = mid ; Driver code
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function divTermCount ( a , b , c , num ) { return parseInt ( ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / ( ( a * b ) / gcd ( a , b ) ) ) - ( num / ( ( c * b ) / gcd ( c , b ) ) ) - ( num / ( ( a * c ) / gcd ( a , c ) ) ) + ( num / ( ( ( ( a * b ) / gcd ( a , b ) ) * c ) / gcd ( ( ( a * b ) / gcd ( a , b ) ) , c ) ) ) ) ) ; } function findNthTerm ( a , b , c , n ) { var low = 1 , high = Number . MAX_SAFE_INTEGER , mid ; while ( low < high ) { mid = low + ( high - low ) / 2 ; if ( divTermCount ( a , b , c , mid ) < n ) low = mid + 1 ; else high = mid ; } return low ; } var a = 2 , b = 3 , c = 5 , n = 100 ; document . write ( parseInt ( findNthTerm ( a , b , c , n ) ) ) ;
Angle between 3 given vertices in a n | Function that checks whether given angle can be created using any 3 sides ; Initialize x and y ; Calculate the number of vertices between i and j , j and k ; Calculate the angle subtended at the circumference ; Angle subtended at j can be found using the fact that the sum of angles of a triangle is equal to 180 degrees ; Driver code
function calculate_angle ( n , i , j , k ) { var x , y ; if ( i < j ) x = j - i ; else x = j + n - i ; if ( j < k ) y = k - j ; else y = k + n - j ; var ang1 = ( 180 * x ) / n ; var ang2 = ( 180 * y ) / n ; var ans = 180 - ang1 - ang2 ; return ans ; } var n = 5 ; var a1 = 1 ; var a2 = 2 ; var a3 = 5 ; document . write ( parseInt ( calculate_angle ( n , a1 , a2 , a3 ) ) ) ;
Loss when two items are sold at same price and same percentage profit / loss | Function that will find loss ; Driver Code ; Calling Function
function Loss ( SP , P ) { var loss = 0 ; loss = ( 2 * P * P * SP ) / ( 100 * 100 - P * P ) ; document . write ( " " + loss . toFixed ( 3 ) ) ; } var SP = 2400 , P = 30 ; Loss ( SP , P ) ;
Check for an array element that is co | Javascript implementation of the approach ; Stores smallest prime factor for every number ; Hash to store prime factors count ; Function to calculate SPF ( Smallest Prime Factor ) for every number till MAXN ; Marking smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to store the prime factors after dividing by the smallest prime factor at every step ; Storing the count of prime factors in hash ; Function that returns true if there are no common prime factors between x and other numbers of the array ; Checking whether it common prime factor with other numbers ; Function that returns true if there is an element in the array which is coprime with all the other elements of the array ; Using sieve for generating prime factors ; Checking the common prime factors with other numbers ; Driver code
let MAXN = 1000001 ; let spf = new Array ( MAXN ) ; let hash1 = new Array ( MAXN ) ; function sieve ( ) { spf [ 1 ] = 1 ; for ( let i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( let i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( let i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( let j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } function getFactorization ( x ) { let temp ; while ( x != 1 ) { temp = spf [ x ] ; if ( x % temp == 0 ) { hash1 [ spf [ x ] ] ++ ; x = x / spf [ x ] ; } while ( x % temp == 0 ) x = x / temp ; } } function check ( x ) { let temp ; while ( x != 1 ) { temp = spf [ x ] ; if ( x % temp == 0 && hash1 [ temp ] > 1 ) return false ; while ( x % temp == 0 ) x = x / temp ; } return true ; } function hasValidNum ( arr , n ) { sieve ( ) ; for ( let i = 0 ; i < n ; i ++ ) getFactorization ( arr [ i ] ) ; for ( let i = 0 ; i < n ; i ++ ) if ( check ( arr [ i ] ) ) return true ; return false ; } let arr = [ 2 , 8 , 4 , 10 , 6 , 7 ] ; let n = arr . length ; if ( hasValidNum ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Ways to Remove Edges from a Complete Graph to make Odd Edges | Function to return the number of ways to remove edges from the graph so that odd number of edges are left in the graph ; Total number of edges ; Driver code
function countWays ( N ) { let E = parseInt ( ( N * ( N - 1 ) ) / 2 , 10 ) ; if ( N == 1 ) return 0 ; return Math . pow ( 2 , E - 1 ) ; } let N = 4 ; document . write ( countWays ( N ) ) ;
Calculate nCr using Pascal 's Triangle | Initialize the matrix with 0 ; 0 C0 = 1 ; Set every nCr = 1 where r = 0 ; Value for the current cell of Pascal 's triangle ; Function to return the value of nCr ; Return nCr ; Build the Pascal 's triangle
let l = new Array ( 1001 ) . fill ( 0 ) . map ( ( ) => new Array ( 1001 ) . fill ( 0 ) ) ; function initialize ( ) { l [ 0 ] [ 0 ] = 1 ; for ( let i = 1 ; i < 1001 ; i ++ ) { l [ i ] [ 0 ] = 1 ; for ( let j = 1 ; j < i + 1 ; j ++ ) { l [ i ] [ j ] = ( l [ i - 1 ] [ j - 1 ] + l [ i - 1 ] [ j ] ) ; } } } function nCr ( n , r ) { return l [ n ] [ r ] ; } initialize ( ) ; let n = 8 ; let r = 3 ; document . write ( nCr ( n , r ) ) ;
Closest sum partition ( into two subsets ) of numbers from 1 to n | Function to return the minimum required absolute difference ; Driver code
function minAbsDiff ( n ) { let mod = n % 4 ; if ( mod == 0 mod == 3 ) { return 0 ; } return 1 ; } let n = 5 ; document . write ( minAbsDiff ( n ) ) ;
Check if the Xor of the frequency of all digits of a number N is zero or not | Javascript implementation of the above approach ; creating a frequency array ; Finding the last digit of the number ; Dividing the number by 10 to eliminate last digit ; counting frequency of each digit ; checking if the xor of all frequency is zero or not ; Driver function
function check ( s ) { let freq = new Array ( 10 ) . fill ( 0 ) , r ; while ( s != 0 ) { r = s % 10 ; s = parseInt ( s / 10 ) ; freq [ r ] += 1 ; } let xor__ = 0 ; for ( let i = 0 ; i < 10 ; i ++ ) { xor__ = xor__ ^ freq [ i ] ; if ( xor__ == 0 ) return true ; else return false ; } } let s = 122233 ; if ( check ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K | Function to prvar N lines ; Iterate N times to prvar N lines ; Driver Code
function printLines ( n , k ) { for ( i = 0 ; i < n ; i ++ ) { document . write ( k * ( 6 * i + 1 ) + " " + k * ( 6 * i + 2 ) + " " + k * ( 6 * i + 3 ) + " " + k * ( 6 * i + 5 ) + " " ) ; } } var n = 2 , k = 2 ; printLines ( n , k ) ;
Sum of first n term of Series 3 , 5 , 9 , 17 , 33. ... | Java script program to find sum of first n terms ; Sn = n * ( 4 * n * n + 6 * n - 1 ) / 3 ; number of terms to be included in sum ; find the Sn
function calculateSum ( n ) { return ( Math . pow ( 2 , n + 1 ) + n - 2 ) ; } let n = 4 ; document . write ( " " + calculateSum ( n ) ) ;
Count the number of Special Strings of a given length N | JavaScript Program to Count the number of Special Strings of a given length N ; Function to return count of special strings ; stores the answer for a particular value of n ; for n = 0 we have empty string ; for n = 1 we have 2 special strings ; calculate count of special string of length i ; fib [ n ] stores the count of special strings of length n ; initialise n
var mod = 1000000007 ; function count_special ( n ) { var fib = [ ... Array ( n + 1 ) ] ; fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; for ( var i = 2 ; i <= n ; i ++ ) { fib [ i ] = ( ( fib [ i - 1 ] % mod ) + ( fib [ i - 2 ] % mod ) ) % mod ; } return fib [ n ] ; } var n = 3 ; document . write ( count_special ( n ) + " " ) ;
Counts Path in an Array | Javascript implementation of the above approach ; Find the number of ways to reach the end ; Base case ; Recursive structure ; Driver code
let mod = 1000000000 ; function ways ( i , arr , n ) { if ( i == n - 1 ) return 1 ; let sum = 0 ; for ( let j = 1 ; j + i < n && j <= arr [ i ] ; j ++ ) { sum += ( ways ( i + j , arr , n ) ) % mod ; sum %= mod ; } return sum % mod ; } let arr = [ 5 , 3 , 1 , 4 , 3 ] ; let n = arr . length ; document . write ( ways ( 0 , arr , n ) ) ;