codeparrot/xlcost-text-to-code huggingface dataset:
Coordinates of the last cell in a Matrix on which performing given operations exits from the Matrix | Function to check if the indices ( i , j ) are valid indices in a Matrix or not ; Cases for invalid cells ; Return true if valid ; Function to find indices of cells of a matrix from which traversal leads to out of the matrix ; Starting from cell ( 0 , 0 ) , traverse in right direction ; Stores direction changes ; Iterate until the current cell exceeds beyond the matrix ; Current index ; If the current cell is 1 ; Update arr [ i ] [ j ] = 0 ; Update indices according to the direction ; Otherwise ; Update indices according to the direction ; The exit cooridnates ; Number of rdows ; Number of columns ; Given matrix arr [ ] [ ]
function issafe ( m , n , i , j ) { if ( i < 0 ) return false ; if ( j < 0 ) return false ; if ( i >= m ) return false ; if ( j >= n ) return false ; return true ; } function endpoints ( arr , m , n ) { let i = 0 ; let j = 0 ; let current_i = 0 ; let current_j = 0 ; let current_d = ' ' ; let rcd = new Map ( ) ; rcd . set ( ' ' , ' ' ) ; rcd . set ( ' ' , ' ' ) ; rcd . set ( ' ' , ' ' ) ; rcd . set ( ' ' , ' ' ) ; while ( issafe ( m , n , i , j ) ) { current_i = i ; current_j = j ; if ( arr [ i ] [ j ] == 1 ) { let move_in = rcd . get ( current_d ) ; arr [ i ] [ j ] = 0 ; if ( move_in == ' ' ) i -= 1 ; else if ( move_in == ' ' ) i += 1 ; else if ( move_in == ' ' ) j -= 1 ; else if ( move_in == ' ' ) j += 1 ; current_d = move_in ; } else { if ( current_d == ' ' ) i -= 1 ; else if ( current_d == ' ' ) i += 1 ; else if ( current_d == ' ' ) j -= 1 ; else if ( current_d == ' ' ) j += 1 ; } } return [ current_i , current_j ] ; } let M = 3 ; let N = 5 ; let arr = [ [ 0 , 1 , 1 , 1 , 0 ] , [ 1 , 0 , 1 , 0 , 1 ] , [ 1 , 1 , 1 , 0 , 0 ] ] ; let p = endpoints ( arr , M , N ) ; document . write ( " " + p [ 0 ] + " " + p [ 1 ] + " " + " " ) ;
Check if a number can be represented as sum of two positive perfect cubes | Function to check if N can be represented as sum of two perfect cubes or not ; Stores the perfect cubes of first N natural numbers ; Traverse the map ; Stores first number ; Stores second number ; Search the pair for the first number to obtain sum N from the Map ; If N cannot be represented as sum of two positive perfect cubes ; Driver Code ; Function call to check if N can be represented as sum of two perfect cubes or not
function sumOfTwoPerfectCubes ( N ) { var cubes = new Map ( ) ; for ( var i = 1 ; i * i * i <= N ; i ++ ) cubes . set ( i * i * i , i ) ; var ans = false ; cubes . forEach ( ( value , key ) => { var firstNumber = key ; var secondNumber = N - value ; if ( cubes . has ( secondNumber ) ) { document . write ( " " ) ; ans = true ; return ; } } ) ; if ( ans ) { return ; } document . write ( " " ) ; } var N = 28 ; sumOfTwoPerfectCubes ( N ) ;
Count subsets consisting of each element as a factor of the next element in that subset | JavaScript Program to implement the above approach ; Function to find number of subsets satisfying the given condition ; Stores number of required sets ; Stores maximum element of arr [ ] that defines the size of sieve ; Iterate through the arr [ ] ; If current element > maxE , then update maxE ; Declare an array sieve of size N + 1 ; Mark all elements corresponding in the array , by one as there will always exists a singleton set ; Iterate from range [ 1 , N ] ; If element is present in array ; Traverse through all its multiples <= n ; Update them if they are present in array ; Iterate from the range [ 1 , N ] ; Update the value of cnt ; Print the result ; Driver Code ; Function Call
let mod = 1000000007 ; function countSets ( arr , n ) { let cnt = 0 ; let maxE = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( maxE < arr [ i ] ) maxE = arr [ i ] ; } let sieve = new Array ( maxE + 1 ) ; for ( let i = 0 ; i < maxE + 1 ; i ++ ) { sieve [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) sieve [ arr [ i ] ] = 1 ; for ( let i = 1 ; i <= maxE ; i ++ ) { if ( sieve [ i ] != 0 ) { for ( let j = i * 2 ; j <= maxE ; j += i ) { if ( sieve [ j ] != 0 ) sieve [ j ] = ( sieve [ j ] + sieve [ i ] ) % mod ; } } } for ( let i = 0 ; i <= maxE ; i ++ ) cnt = ( cnt % mod + sieve [ i ] % mod ) % mod ; document . write ( cnt % mod + " " ) ; } let arr = [ 16 , 18 , 6 , 7 , 2 , 19 , 20 , 9 ] ; let N = arr . length ; countSets ( arr , N ) ;
Count distinct prime factors for each element of an array | JavaScript program for the above approach ; Stores smallest prime factor for every number ; Stores distinct prime factors ; Function to find the smallest prime factor of every number ; Mark the smallest prime factor of every number to itself ; Separately mark all the smallest prime factor of every even number to be 2 ; If i is prime ; Mark spf for all numbers divisible by i ; Mark spf [ j ] if it is not previously marked ; Function to find the distinct prime factors ; Push all distinct of x prime factor in v [ x ] ; Pushback into v [ i ] ; Increment the idx ; Update x = ( x / spf [ x ] ) ; Function to get the distinct factor count of arr [ ] ; initialization ; Precompute the smallest Prime Factors ; For distinct prime factors Fill the v [ ] vector ; Count of Distinct Prime Factors of each array element ; Driver code
let MAX = 100001 ; let spf ; let v ; function sieve ( ) { for ( let i = 1 ; i < MAX ; i ++ ) spf [ i ] = i ; for ( let i = 4 ; i < MAX ; i = i + 2 ) spf [ i ] = 2 ; for ( let i = 3 ; i * i < MAX ; i ++ ) if ( spf [ i ] == i ) { for ( let j = i * i ; j < MAX ; j = j + i ) { if ( spf [ j ] == j ) spf [ j ] = i ; } } } function DistPrime ( ) { for ( let i = 1 ; i < MAX ; i ++ ) { let idx = 1 ; let x = i ; if ( x != 1 ) v [ i ] . push ( spf [ x ] ) ; x = parseInt ( x / spf [ x ] , 10 ) ; while ( x != 1 ) { if ( v [ i ] [ idx - 1 ] != spf [ x ] ) { v [ i ] . push ( spf [ x ] ) ; idx += 1 ; } x = parseInt ( x / spf [ x ] , 10 ) ; } } } function getFactorCount ( arr , N ) { spf = new Array ( MAX ) ; v = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) v [ i ] = [ ] ; sieve ( ) ; DistPrime ( ) ; for ( let i = 0 ; i < N ; i ++ ) { document . write ( v [ arr [ i ] ] . length + " " ) ; } } let arr = [ 6 , 9 , 12 ] ; let N = arr . length ; getFactorCount ( arr , N ) ;
Find the first day of a given year from a base year having first day as Monday | Function to find the day of 1 st January of Y year ; Count years between years Y and B ; Count leap years ; Non leap years ; Total number of days in the years lying between the years Y and B ; Actual day ; Driver Code
function findDay ( Y , B ) { let lyear , rest , totaldays , day ; Y = ( Y - 1 ) - B ; lyear = Math . floor ( Y / 4 ) ; rest = Y - lyear ; totaldays = ( rest * 365 ) + ( lyear * 366 ) + 1 ; day = ( totaldays % 7 ) ; if ( day == 0 ) document . write ( " " ) ; else if ( day == 1 ) document . write ( " " ) ; else if ( day == 2 ) document . write ( " " ) ; else if ( day == 3 ) document . write ( " " ) ; else if ( day == 4 ) document . write ( " " ) ; else if ( day == 5 ) document . write ( " " ) ; else if ( day == 6 ) document . write ( " " ) ; else document . write ( " " ) ; } let Y = 2020 , B = 1900 ; findDay ( Y , B ) ;
Queries to update array elements in a range [ L , R ] to satisfy given conditions | Function to prlet the array ; Function to perform the query in range [ L , R ] such that arr [ i ] += i - L + 1 ; Initialize array ; Traverse the query array ; Stores range in 1 - based index ; Update arr1 [ L ] ; Update arr1 [ R + 1 ] ; Update arr2 [ R + 1 ] ; Calculate prefix sum ; Traverse the array , arr2 [ ] ; Copy arr2 [ ] leto arr [ ] ; Given array ; Size of the array ; Stores count of query ; Function Call
function prletArray ( arr , N ) { for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function modifyArray ( arr , N , Q , cntQuery ) { let arr1 = new Array ( N + 2 ) . fill ( 0 ) ; let arr2 = new Array ( N + 2 ) . fill ( 0 ) ; for ( let i = 0 ; i < cntQuery ; i ++ ) { let L = Q [ i ] [ 0 ] + 1 , R = Q [ i ] [ 1 ] + 1 ; arr1 [ L ] ++ ; arr1 [ R + 1 ] -- ; arr2 [ R + 1 ] -= R - L + 1 ; } for ( let i = 1 ; i <= N ; i ++ ) arr1 [ i ] += arr1 [ i - 1 ] ; for ( let i = 1 ; i <= N ; i ++ ) arr2 [ i ] += arr2 [ i - 1 ] + arr1 [ i ] ; for ( let i = 1 ; i <= N ; i ++ ) arr [ i - 1 ] = arr2 [ i ] ; prletArray ( arr , N ) ; } let arr = [ 0 , 0 , 0 , 0 ] ; let N = arr . length ; let Q = [ [ 1 , 3 ] , [ 0 , 1 ] ] ; let cntQuery = Q . length ; modifyArray ( arr , N , Q , cntQuery ) ;
Maximize product obtained by taking one element from each array of a given list | Function to return the product of 2 numbers ; If any of the two numbers is None ; Otherwise , return the product ; Function to calculate maximum product by taking only one element from each array present in the list ; Find the maximum and minimum present in the current array ; If last index is reached , then return the highest ( positive ) and lowest ( negative ) values ; Store the positive and negative products returned by calculating for the remaining arrays ; Store highest positive product ; Store product of highest with negative ; Store product of lowest with positive ; Store lowest negative product ; Return the maximum positive and minimum negative product ; Driver Code Count of given arrays ; Given list of N arrays ; Store the maximum positive and minimum negative product possible ; Print the maximum product
function findProduct ( number_1 , number_2 ) { if ( number_1 == Number . MIN_VALUE number_2 == Number . MIN_VALUE ) { return 0 ; } else return number_1 * number_2 ; } function calculateProduct ( List , index ) { let highest = Math . max ( ... List [ index ] ) ; let lowest = Math . min ( ... List [ index ] ) ; if ( index + 1 == List . length ) { if ( lowest < 0 && highest >= 0 ) { return ( [ highest , lowest ] ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( [ Number . MIN_VALUE , lowest ] ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( [ highest , Number . MIN_VALUE ] ) ; } } let temp = calculateProduct ( List , index + 1 ) ; let positive = temp [ 0 ] ; let negative = temp [ 1 ] ; let highPos = findProduct ( highest , positive ) ; let highNeg = findProduct ( highest , negative ) ; let lowPos = findProduct ( lowest , positive ) ; let lowNeg = findProduct ( lowest , negative ) ; if ( lowest < 0 && highest >= 0 ) { return ( [ Math . max ( highPos , lowNeg ) , Math . min ( highNeg , lowPos ) ] ) ; } else if ( lowest <= 0 && highest <= 0 ) { return ( [ lowNeg , lowPos ] ) ; } else if ( lowest >= 0 && highest >= 0 ) { return ( [ Math . max ( lowPos , highPos ) , Math . min ( lowNeg , highNeg ) ] ) ; } return ( [ 0 , 0 ] ) ; } let N = 2 ; let arr = [ [ - 3 , - 4 ] , [ 1 , 2 , - 3 ] ] ; let ans = calculateProduct ( arr , 0 ) ; document . write ( ans [ 0 ] ) ;
Reduce a number N by at most D to maximize count of trailing nines | Function to find a number with maximum count of trailing nine ; Stores count of digits in n ; Stores power of 10 ; If last i digits greater than or equal to d ; Update res ; Update p10 ; Driver Code ; Function Call
function maxNumTrailNine ( n , d ) { let res = n ; let cntDigits = parseInt ( Math . log ( n ) / Math . log ( 10 ) ) + 1 ; let p10 = 10 ; for ( let i = 1 ; i <= cntDigits ; i ++ ) { if ( n % p10 >= d ) { break ; } else { res = n - n % p10 - 1 ; } p10 = p10 * 10 ; } document . write ( res ) ; } let n = 1025 , d = 6 ; maxNumTrailNine ( n , d ) ;
Split array into minimum number of subsets such that elements of all pairs are present in different subsets at least once | Function to find minimum count of ways to split the array into two subset such that elements of each pair occurs in two different subset ; Stores minimum count of ways to split array into two subset such that elements of each pair occurs in two different subset ; If N is odd ; Driver code
function MinimumNoOfWays ( arr , n ) { var mini_no_of_ways ; if ( n % 2 == 0 ) { mini_no_of_ways = n / 2 ; } else { mini_no_of_ways = n / 2 + 1 ; } return parseInt ( mini_no_of_ways ) ; } var arr = [ 3 , 4 , 2 , 1 , 5 ] ; var N = arr . length ; document . write ( MinimumNoOfWays ( arr , N ) ) ;
Chocolate Distribution Problem | Set 2 | Function to return minimum number of chocolates ; decreasing sequence ; add the chocolates received by that person ; end point of decreasing sequence ; val = 1 ; reset value at that index ; increasing sequence ; flat sequence ; add value of chocolates at position n - 1 ; helper function to get sum of decreasing sequence ; value obtained from decreasing sequence also the count of values in the sequence ; assigning max of values obtained from increasing and decreasing sequences ; sum of count - 1 values & peak value sum of natural numbers : ( n * ( n + 1 ) ) / 2 ; Driver code
function minChocolates ( a , n ) { let i = 0 , j = 0 ; let res = 0 , val = 1 ; while ( j < n - 1 ) { if ( a [ j ] > a [ j + 1 ] ) { j += 1 ; continue ; } if ( i == j ) res += val ; else { res += get_sum ( val , i , j ) ; } if ( a [ j ] < a [ j + 1 ] ) val += 1 ; else val = 1 ; j += 1 ; i = j ; } if ( i == j ) res += val ; else res += get_sum ( val , i , j ) ; return res ; } function get_sum ( peak , start , end ) { let count = end - start + 1 ; peak = ( peak > count ) ? peak : count ; let s = peak + ( ( ( count - 1 ) * count ) >> 1 ) ; return s ; } let a = [ 5 , 5 , 4 , 3 , 2 , 1 ] ; let n = a . length ; document . write ( " " + minChocolates ( a , n ) ) ;
Remove array elements to reduce frequency of each array element to at most K | Function to remove array elements such that frequency of each distinct array element is at most K ; Base Case ; Stores index of array element by removing the array element such that the frequency of array elements is at most K ; Traverse the array ; If j < k or arr [ i ] > arr [ j - k ] ; Update arr [ j ] ; Remove array elements ; Function to print the array ; Traverse the array ; Utility Function to remove array elements such that frequency of each distinct array element is at most K ; Print updated array ; Driver code
function RemoveElemArr ( arr , n , k ) { if ( n == 0 n == 1 ) return arr ; var j = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( j < k arr [ i ] > arr [ j - k ] ) { arr [ j ++ ] = arr [ i ] ; } } while ( arr . length > j ) { arr . pop ( ) ; } return arr ; } function printArray ( arr ) { for ( var i = 0 ; i < arr . length ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } function UtilRemov ( arr , n , k ) { arr = RemoveElemArr ( arr , n , k ) ; printArray ( arr ) ; } var arr = [ 1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 ] ; var k = 2 ; var n = arr . length ; UtilRemov ( arr , n , k ) ;
Count pairs of equal array elements remaining after every removal | Function to count pairs of equal elements by removing arr [ i ] from the array ; Stores total count of pairs of equal elements ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Traverse the map ; Stores key of an element ; Traverse the array ; Stores count of pairs of equal element by removing arr [ i ] ; Given Array
function pairs_after_removing ( arr , N ) { var cntPairs = 0 ; var mp = new Map ( ) ; for ( var 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 ) ; } } mp . forEach ( ( value , key ) => { var i = key ; cntPairs += mp . get ( i ) * ( mp . get ( i ) - 1 ) / 2 ; } ) ; for ( var i = 0 ; i < N ; i ++ ) { var pairs_after_arr_i_removed = cntPairs + 1 - mp . get ( arr [ i ] ) ; document . write ( pairs_after_arr_i_removed + ' ' ) ; } return ; } var arr = [ 2 , 3 , 4 , 3 , 2 ] ; var N = arr . length ; pairs_after_removing ( arr , N ) ;
Modify N by adding its smallest positive divisor exactly K times | Function to find the smallest divisor of N greater than 1 ; If i is a divisor of N ; If N is a prime number ; Function to find the value of N by performing the operations K times ; If N is an even number ; Update N ; If N is an odd number ; Update N ; Driver Code
function smallestDivisorGr1 ( N ) { for ( var i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { return i ; } } return N ; } function findValOfNWithOperat ( N , K ) { if ( N % 2 == 0 ) { N += K * 2 ; } else { N += smallestDivisorGr1 ( N ) + ( K - 1 ) * 2 ; } return N ; } var N = 6 , K = 4 ; document . write ( findValOfNWithOperat ( N , K ) ) ;
Partition array into minimum number of equal length subsets consisting of a single distinct value | Function to find the minimum count of subsets by partitioning the array with given conditions ; Store frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores GCD of frequency of each distinct element of the array ; Update freqGCD ; Recursive function to return gcd of a and b ; Driver Code
function CntOfSubsetsByPartitioning ( arr , N ) { var freq = { } ; for ( var i = 0 ; i < N ; i ++ ) { if ( freq . hasOwnProperty ( arr [ i ] ) ) { freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; } else { freq [ arr [ i ] ] = 1 ; } } var freqGCD = 0 ; for ( const [ key , value ] of Object . entries ( freq ) ) { freqGCD = __gcd ( freqGCD , value ) ; } return parseInt ( N / freqGCD ) ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var arr = [ 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 ] ; var N = arr . length ; document . write ( CntOfSubsetsByPartitioning ( arr , N ) ) ;
Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix | Initialize rows and columns ; Store all 8 directions ; Function to check if a cell ( i , j ) is valid or not ; Function to find sum of adjacent cells for cell ( i , j ) ; Initialize sum ; Visit all 8 directions ; Check if cell is valid ; Return sum ; Function to print sum of adjacent elements ; Stores the resultant matrix ; Iterate each elements of matrix ; Find adjacent sum ; Given matrix ; Size of matrix ; Function call
let r , c ; let dir = [ [ 1 , 0 ] , [ - 1 , 0 ] , [ 0 , 1 ] , [ 0 , - 1 ] , [ - 1 , - 1 ] , [ - 1 , 1 ] , [ 1 , 1 ] , [ 1 , - 1 ] ] ; function valid ( i , j ) { if ( i >= 0 && j >= 0 && i < r && j < c ) return true ; return false ; } function find ( i , j , v ) { let s = 0 ; for ( let k = 0 ; k < 8 ; k ++ ) { let ni = i + dir [ k ] [ 0 ] , nj = j + dir [ k ] [ 1 ] ; if ( valid ( ni , nj ) ) s += v [ ni ] [ nj ] ; } return s ; } function findsumofneighbors ( M ) { let v = new Array ( r ) ; for ( var i = 0 ; i < v . length ; i ++ ) { v [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < r ; i ++ ) { for ( let j = 0 ; j < c ; j ++ ) { v [ i ] [ j ] = find ( i , j , M ) ; document . write ( v [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let M = [ [ 1 , 4 , 1 ] , [ 2 , 4 , 5 ] , [ 3 , 1 , 2 ] ] ; r = M . length ; c = M [ 0 ] . length ; findsumofneighbors ( M ) ;
Nth term of a recurrence relation generated by two given arrays | Javascript program for the above approach ; Declare T [ ] [ ] as global matrix ; Result matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i ] [ j ] ; Update the final matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i ] [ j ] ; Update the final matrix ; Function to calculate matrix ^ n using binary exponentaion ; Initialize result matrix and unity matrix ; Function to calculate nth term of general recurrence ; Fill T [ ] [ ] with appropriate value ; Function Call to calculate T ^ n ; Calculate nth term as first element of F * ( T ^ n ) ; Print the result ; Given Initial terms ; Given coefficients ; Given K ; Given N ; Function Call
let mod = ( 1e9 + 7 ) ; let T = new Array ( 2000 ) ; let result = new Array ( 2000 ) ; for ( let i = 0 ; i < 2000 ; i ++ ) { T [ i ] = new Array ( 2000 ) ; result [ i ] = new Array ( 2000 ) ; for ( let j = 0 ; j < 2000 ; j ++ ) { T [ i ] [ j ] = 0 ; result [ i ] [ j ] = 0 ; } } function mul_2 ( K ) { let temp = new Array ( K + 1 ) ; for ( let i = 0 ; i <= K ; i ++ ) { temp [ i ] = new Array ( K + 1 ) ; for ( let j = 0 ; j <= K ; j ++ ) { temp [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= K ; i ++ ) { for ( let j = 1 ; j <= K ; j ++ ) { for ( let k = 1 ; k <= K ; k ++ ) { temp [ i ] [ j ] = ( temp [ i ] [ j ] + ( T [ i ] [ k ] * T [ k ] [ j ] ) % mod ) % mod ; } } } for ( let i = 1 ; i <= K ; i ++ ) { for ( let j = 1 ; j <= K ; j ++ ) { T [ i ] [ j ] = temp [ i ] [ j ] ; } } } function mul_1 ( K ) { let temp = new Array ( K + 1 ) ; for ( let i = 0 ; i <= K ; i ++ ) { temp [ i ] = new Array ( K + 1 ) ; for ( let j = 0 ; j <= K ; j ++ ) { temp [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= K ; i ++ ) { for ( let j = 1 ; j <= K ; j ++ ) { for ( let k = 1 ; k <= K ; k ++ ) { temp [ i ] [ j ] = ( temp [ i ] [ j ] + ( result [ i ] [ k ] * T [ k ] [ j ] ) % mod ) % mod ; } } } for ( let i = 1 ; i <= K ; i ++ ) { for ( let j = 1 ; j <= K ; j ++ ) { result [ i ] [ j ] = temp [ i ] [ j ] ; } } } function matrix_pow ( K , n ) { for ( let i = 1 ; i <= K ; i ++ ) { for ( let j = 1 ; j <= K ; j ++ ) { if ( i == j ) result [ i ] [ j ] = 1 ; } } while ( n > 0 ) { if ( n % 2 == 1 ) mul_1 ( K ) ; mul_2 ( K ) ; n = parseInt ( n / 2 , 10 ) ; } } function NthTerm ( F , C , K , n ) { for ( let i = 1 ; i <= K ; i ++ ) T [ i ] [ K ] = C [ K - i ] ; for ( let i = 1 ; i <= K ; i ++ ) T [ i + 1 ] [ i ] = 1 ; matrix_pow ( K , n ) ; let answer = 0 ; for ( let i = 1 ; i <= K ; i ++ ) { answer += F [ i - 1 ] * result [ i ] [ 1 ] ; } document . write ( answer + " " ) ; return 0 ; } let F = [ 1 , 2 , 3 ] ; let C = [ 1 , 1 , 1 ] ; let K = 3 ; let N = 10 ; NthTerm ( F , C , K , N ) ;
Calculate Root Mean Kth power of all array elements | Function to find the Nth root ; Initially guessing random numberbetween 0 and 9 ; Smaller eps for more accuracy ; Initialize difference between the two roots by Integer . MAX_VALUE ; xK denotes current value of x ; Iterate until desired accuracy is reached ; Find the current value from previous value by newton 's method ; Function to calculate the Root Mean kth power of array elements ; Calculate sum of kth power ; Calculate Mean ; Calculate kth Root of mean ; Driver Code ; Function Call
function nthRoot ( A , N ) { var xPre = ( Math . random ( ) * 10 ) % 10 ; var eps = 1e-3 ; var delX = Number . MAX_VALUE ; var xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + A / Math . pow ( xPre , N - 1 ) ) / N ; delX = Math . abs ( xK - xPre ) ; xPre = xK ; } return xK ; } function RMNValue ( arr , n , k ) { var Nth = 0 ; var mean = 0 , root = 0 ; for ( var i = 0 ; i < n ; i ++ ) { Nth += Math . pow ( arr [ i ] , k ) ; } mean = ( Nth / ( n ) ) ; root = nthRoot ( mean , k ) ; return root ; } var arr = [ 10 , 4 , 6 , 8 ] ; var N = arr . length ; var K = 3 ; document . write ( RMNValue ( arr , N , K ) ) ;
Add two numbers represented by Stacks | Function to return the stack that contains the sum of two numbers ; Calculate the sum of the top elements of both the stacks ; Push the sum into the stack ; Store the carry ; Pop the top elements ; If N1 is not empty ; If N2 is not empty ; If carry remains ; Reverse the stack . so that most significant digit is at the bottom of the stack ; Function to display the resultamt stack ; Driver Code
function addStack ( N1 , N2 ) { var res = [ ] ; var sum = 0 , rem = 0 ; while ( N1 . length != 0 && N2 . length != 0 ) { sum = ( rem + N1 [ N1 . length - 1 ] + N2 [ N2 . length - 1 ] ) ; res . push ( sum % 10 ) ; rem = parseInt ( sum / 10 ) ; N1 . pop ( ) ; N2 . pop ( ) ; } while ( N1 . length != 0 ) { sum = ( rem + N1 [ N1 . length - 1 ] ) ; res . push ( sum % 10 ) ; rem = parseInt ( sum / 10 ) ; N1 . pop ( ) ; } while ( N2 . length != 0 ) { sum = ( rem + N2 [ N2 . length - 1 ] ) ; res . push ( sum % 10 ) ; rem = parseInt ( sum / 10 ) ; N2 . pop ( ) ; } while ( rem > 0 ) { res . push ( rem ) ; rem = parseInt ( rem / 10 ) ; } while ( res . length != 0 ) { N1 . push ( res [ res . length - 1 ] ) ; res . pop ( ) ; } res = N1 ; return res ; } function display ( res ) { var N = res . length ; var s = " " ; while ( res . length != 0 ) { s = ( res [ res . length - 1 ] . toString ( ) ) + s ; res . pop ( ) ; } document . write ( s ) ; } var N1 = [ ] ; N1 . push ( 5 ) ; N1 . push ( 8 ) ; N1 . push ( 7 ) ; N1 . push ( 4 ) ; var N2 = [ ] ; N2 . push ( 2 ) ; N2 . push ( 1 ) ; N2 . push ( 3 ) ; var res = addStack ( N1 , N2 ) ; display ( res ) ;