"Count the number of strings in an array whose distinct characters are less than equal to M | Function to count the strings whose distinct characters count is less than M ; Loop to iterate over all the strings of the array ; Distinct characters in the String with the help of set ; Checking if its less than or equal to M ; Driver Code"
How can the above be solved in JS?

function distinct ( S , M , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let set1 = new Set ( ) ; for ( let j = 0 ; j < S [ i ] . length ; j ++ ) { if ( ! set1 . has ( S [ i ] [ j ] ) ) set1 . add ( S [ i ] [ j ] ) ; } let c = set1 . size ; if ( c <= M ) count += 1 ; } document . write ( count ) ; } let S = [ " " , " " , " " ] ; let M = 7 ; let n = S . length ; distinct ( S , M , n ) ;


"Count the number of ways to construct the target string | Javascript Program to Count the number of ways to construct the target String ; base case ; If current subproblem has been solved , use the value ; search through all the indiced at which the current character occurs . For each index greater than prev , take the index and move to the next position , and add to the answer . ; Store and return the solution for this subproblem ; preprocess the Strings by storing for each character of every String , the index of their occurrence we will use a common list for all because of only the index matter in the String from which the character was picked ; we are storing j + 1 because the initial picked index in the recursive step will ne 0. This is just for ease of implementation ; initialise dp table . - 1 represents that the subproblem hasn 't been solved ; Driver Code"
How can the above be solved in JS?

let mod = 1000000007 ; let dp = new Array ( 1000 ) ; for ( let i = 0 ; i < 1000 ; i ++ ) { dp [ i ] = new Array ( 1000 ) ; } function calculate ( pos , prev , s , index ) { if ( pos == s . length ) return 1 ; if ( dp [ pos ] [ prev ] != - 1 ) return dp [ pos ] [ prev ] ; let answer = 5 ; for ( let i = 0 ; i < index . length ; i ++ ) { if ( ( String . fromCharCode ( index [ i ] ) ) . localeCompare ( prev ) > 1 ) { answer = ( answer % mod + calculate ( pos + 1 , index [ i ] , s , index ) % mod ) % mod ; } } dp [ pos ] [ prev ] = answer ; return dp [ pos ] [ prev ] ; } function countWays ( a , s ) { let n = a . length ; let index = [ ] ; for ( let i = 0 ; i < 26 ; i ++ ) index . push ( [ ] ) ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < a [ i ] . length ; j ++ ) { index [ a [ i ] [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] . push ( j + 1 ) ; } } for ( let i = 0 ; i < 1000 ; i ++ ) { for ( let j = 0 ; j < 1000 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return calculate ( 0 , 0 , s , index [ 0 ] ) ; } let A = [ ] ; A . push ( " " ) ; A . push ( " " ) ; A . push ( " " ) ; let S = " " ; document . write ( countWays ( A , S ) ) ;


"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"
How can the above be solved in JS?

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 ) ) ;


"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 ; Store all the divisors of N ; If i is a divisor ; If i is not equal to N / i ; Iterate through all the divisors of N - 1 and count them in answer ; Check if N - 1 is a divisor or not ; Iterate through all divisors and check for N mod d = 1 or ( N - 1 ) mod d = 0 ; Driver code"
How can the above be solved in JS?

function countValues ( N ) { var div = [ ] ; for ( var i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { div . push ( i ) ; if ( N != i * i ) { div . push ( N / i ) ; } } } var answer = 0 ; for ( var i = 1 ; i * i <= N - 1 ; i ++ ) { if ( ( N - 1 ) % i == 0 ) { if ( i * i == N - 1 ) answer ++ ; else answer += 2 ; } } div . forEach ( d => { var K = N ; while ( K % d == 0 ) K /= d ; if ( ( K - 1 ) % d == 0 ) answer ++ ; } ) ; return answer ; } var N = 6 ; document . write ( countValues ( N ) ) ;


"Count the subarray with sum strictly greater than the sum of remaining elements | Function to count the number of sub - arrays with sum strictly greater than the remaining elements of array ; For loop for beginning point of a subarray ; For loop for ending point of the subarray ; Initialise subarray_sum and remaining_sum to 0 ; For loop to calculate the sum of generated subarray ; For loop to calculate the sum remaining array element ; Checking for condition when subarray sum is strictly greater than remaining sum of array element ; Driver code"
How can the above be solved in JS?

function Count_subarray ( arr , n ) { var subarray_sum , remaining_sum , count = 0 ; var i , j , k , l ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i ; j < n ; j ++ ) { subarray_sum = 0 ; remaining_sum = 0 ; for ( k = i ; k <= j ; k ++ ) { subarray_sum += arr [ k ] ; } for ( l = 0 ; l < i ; l ++ ) { remaining_sum += arr [ l ] ; } for ( l = j + 1 ; l < n ; l ++ ) { remaining_sum += arr [ l ] ; } if ( subarray_sum > remaining_sum ) { count += 1 ; } } } return count ; } var arr = [ 10 , 9 , 12 , 6 ] ; var n = arr . length ; document . write ( Count_subarray ( arr , n ) ) ;


"Count the subarray with sum strictly greater than the sum of remaining elements | javascript implementation of the above approach ; Calculating total sum of given array ; For loop for beginning povar of a subarray ; initialise subarray_sum to 0 ; For loop for calculating subarray_sum and remaining_sum ; Calculating subarray_sum and corresponding remaining_sum ; Checking for the condition when subarray sum is strictly greater than the remaining sum of the array element ; Driver code"
How can the above be solved in JS?

function Count_subarray ( arr , n ) { var total_sum = 0 , subarray_sum , remaining_sum , count = 0 ; for ( i = 0 ; i < n ; i ++ ) { total_sum += arr [ i ] ; } for ( i = 0 ; i < n ; i ++ ) { subarray_sum = 0 ; for ( j = i ; j < n ; j ++ ) { subarray_sum += arr [ j ] ; remaining_sum = total_sum - subarray_sum ; if ( subarray_sum > remaining_sum ) { count += 1 ; } } } return count ; } var arr = [ 10 , 9 , 12 , 6 ] ; var n = arr . length ; document . write ( Count_subarray ( arr , n ) ) ;


"Count the triplets such that A [ i ] < B [ j ] < C [ k ] | Function to return the count of elements in arr [ ] which are less than the given key ; Modified binary search ; Function to return the count of elements in arr [ ] which are greater than the given key ; Modified binary search ; Function to return the count of the required triplets ; Sort all three arrays ; Iterate for all the elements of array B ; Count of elements in A [ ] which are less than the chosen element from B [ ] ; Count of elements in C [ ] which are greater than the chosen element from B [ ] ; Update the count ; Driver code"
How can the above be solved in JS?

function countLessThan ( arr , n , key ) { let l = 0 , r = n - 1 ; let index = - 1 ; while ( l <= r ) { let m = Math . floor ( ( l + r ) / 2 ) ; if ( arr [ m ] < key ) { l = m + 1 ; index = m ; } else { r = m - 1 ; } } return ( index + 1 ) ; } function countGreaterThan ( arr , n , key ) { let l = 0 , r = n - 1 ; let index = - 1 ; while ( l <= r ) { let m = Math . floor ( ( l + r ) / 2 ) ; if ( arr [ m ] <= key ) { l = m + 1 ; } else { r = m - 1 ; index = m ; } } if ( index == - 1 ) return 0 ; return ( n - index ) ; } function countTriplets ( n , a , b , c ) { a . sort ( function ( e , f ) { return e - f ; } ) ; b . sort ( function ( e , f ) { return e - f ; } ) ; c . sort ( function ( e , f ) { return e - f ; } ) ; let count = 0 ; for ( let i = 0 ; i < n ; ++ i ) { let current = b [ i ] ; let low = countLessThan ( a , n , current ) ; let high = countGreaterThan ( c , n , current ) ; count += ( low * high ) ; } return count ; } let a = [ 1 , 5 ] ; let b = [ 2 , 4 ] ; let c = [ 3 , 6 ] ; let size = a . length ; document . write ( countTriplets ( size , a , b , c ) ) ;


"Count trailing zero bits using lookup table | Javascript code for counting trailing zeros in binary representation of a number ; Map a bit value mod 37 to its position ; Only difference between ( x and - x ) is the value of signed magnitude ( leftmostbit ) negative numbers signed bit is 1 ; Driver code"
How can the above be solved in JS?


"Count trailing zero bits using lookup table | Simple Javascript code for counting trailing zeros in binary representation of a number ; Driver Code"
How can the above be solved in JS?

function countTrailingZero ( x ) { let count = 0 ; while ( ( x & 1 ) == 0 ) { x = x >> 1 ; count ++ ; } return count ; } document . write ( countTrailingZero ( 11 ) ) ;


"Count triples with Bitwise AND equal to Zero | Function to find the number of triplets whose Bitwise AND is 0. ; Stores the count of triplets having bitwise AND equal to 0 ; Stores frequencies of all possible A [ i ] & A [ j ] ; Traverse the array ; Update frequency of Bitwise AND of all array elements with a ; Traverse the array ; Update frequency of Bitwise AND of all array elements with a ; If bitwise AND of triplet is zero , increment cnt ; Return the number of triplets whose Bitwise AND is 0. ; Input Array ; Function Call"
How can the above be solved in JS?

function countTriplets ( A ) { var cnt = 0 ; var tuples = new Map ( ) ; A . forEach ( a => { A . forEach ( b => { if ( tuples . has ( a & b ) ) tuples . set ( a & b , tuples . get ( a & b ) + 1 ) else tuples . set ( a & b , 1 ) } ) ; } ) ; A . forEach ( a => { tuples . forEach ( ( value , key ) => { if ( ( key & a ) == 0 ) cnt += value ; } ) ; } ) ; return cnt ; } var A = [ 2 , 1 , 3 ] ; document . write ( countTriplets ( A ) ) ;


"Count triplets from a given range having sum of two numbers of a triplet equal to the third number | Function to find the number of triplets from the range [ L , R ] having sum of two numbers from the triplet equal to the third number ; Stores the total number of triplets ; Find the difference of the range ; Case 1 : If triplets can 't  be formed, then return 0 ; Otherwise ; Update the total number of triplets ; Return the count ; Driver Code"
How can the above be solved in JS?

function totalCombination ( L , R ) { let count = 0 ; let K = R - L ; if ( K < L ) return 0 ; let ans = K - L ; count = ( ( ans + 1 ) * ( ans + 2 ) ) / 2 ; return count ; } let L = 2 , R = 6 ; document . write ( totalCombination ( L , R ) ) ;


"Count unimodal and non | JavaScript program for the above approach ; Function to calculate the factorials up to a number ; Calculate the factorial ; Function to find power ( a , b ) ; Iterate until b exists ; If b is divisible by 2 ; Decrease the value of b ; Return the answer ; Function that counts the unimodal and non - unimodal permutations of a given integer N ; Function Call for finding factorials up to N ; Function to count unimodal permutations ; Non - unimodal permutation is N ! - unimodal permutations ; Driver Code Given Number N ; Function Call"
How can the above be solved in JS?

var mod = parseInt ( 1e9 + 7 ) ; var mx = 1000000 ; var fact = new Array ( mx + 1 ) . fill ( 0 ) ; function Calculate_factorial ( ) { fact [ 0 ] = 1 ; for ( var i = 1 ; i <= mx ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; fact [ i ] %= mod ; } } function UniModal_per ( a , b ) { var res = 1 ; while ( b > 0 ) { if ( b % 2 !== 0 ) res = res * a ; res %= mod ; a = a * a ; a %= mod ; b = parseInt ( b / 2 ) ; } return res ; } function countPermutations ( n ) { Calculate_factorial ( ) ; var uni_modal = UniModal_per ( 2 , n - 1 ) ; var nonuni_modal = fact [ n ] - uni_modal ; document . write ( uni_modal + " " + nonuni_modal ) ; return ; } var N = 4 ; countPermutations ( N ) ;


"Count ways of choosing a pair with maximum difference | JavaScript Code to find no . of Ways of choosing a pair with maximum difference ; To find minimum and maximum of the array ; to find the count of minimum and maximum elements ; Count variables ; condition for all elements equal ; Driver Code"
How can the above be solved in JS?

function countPairs ( a , n ) { let mn = Number . MAX_VALUE ; let mx = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { mn = Math . min ( mn , a [ i ] ) ; mx = Math . max ( mx , a [ i ] ) ; } let c1 = 0 ; let c2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] == mn ) c1 ++ ; if ( a [ i ] == mx ) c2 ++ ; } if ( mn == mx ) return n * ( n - 1 ) / 2 ; else return c1 * c2 ; } let a = [ 3 , 2 , 1 , 1 , 3 ] ; let n = a . length ; document . write ( countPairs ( a , n ) ) ;


"Count ways to construct array with even product from given array such that absolute difference of same indexed elements is at most 1 | Function to find count the ways to construct an array , B such that abs ( A [ i ] - B [ i ] ) <= 1 and product of elements of B is even ; Stores count of arrays B such that abs ( A [ i ] - B [ i ] ) <= 1 ; Stores count of arrays B whose product of elements is not even ; Traverse the array ; Update total ; If A [ i ] is an even number ; Update oddArray ; Print var 3 ^ N - 2 ^ X ; Driver Code"
How can the above be solved in JS?

function cntWaysConsArray ( A , N ) { var total = 1 ; var oddArray = 1 ; for ( i = 0 ; i < N ; i ++ ) { total = total * 3 ; if ( A [ i ] % 2 == 0 ) { oddArray *= 2 ; } } document . write ( total - oddArray ) ; } var A = [ 2 , 4 ] ; var N = A . length ; cntWaysConsArray ( A , N ) ;


"Count ways to express a number as sum of consecutive numbers | Utility method to compute number of ways in which N can be represented as sum of consecutive number ; constraint on values of L gives us the time Complexity as O ( N ^ 0.5 ) ; Driver code"
How can the above be solved in JS?

function countConsecutive ( N ) { let count = 0 ; for ( let L = 1 ; L * ( L + 1 ) < 2 * N ; L ++ ) { let a = ( ( 1.0 * N - ( L * ( L + 1 ) ) / 2 ) / ( L + 1 ) ) ; if ( a - parseInt ( a , 10 ) == 0.0 ) count ++ ; } return count ; } let N = 15 ; document . write ( countConsecutive ( N ) + " " ) ; N = 10 ; document . write ( countConsecutive ( N ) ) ;


"Count ways to express a number as sum of exactly two numbers | Function returns the count of ways express a number as sum of two numbers . ; Driver Code"
How can the above be solved in JS?

function CountWays ( n ) { let ans = Math . floor ( ( n - 1 ) / 2 ) ; return ans ; } let N = 8 ; document . write ( CountWays ( N ) ) ;


"Count ways to obtain given sum by repeated throws of a dice | Function to calculate the total number of ways to have sum N ; Base Case ; Return already stored result ; Recur for all 6 states ; Return the result ; Given sum N ; Initialize the dp array ; Function Call"
How can the above be solved in JS?

function findWays ( N , dp ) { if ( N == 0 ) { return 1 ; } if ( dp [ N ] != - 1 ) { return dp [ N ] ; } let cnt = 0 ; for ( let i = 1 ; i <= 6 ; i ++ ) { if ( N - i >= 0 ) { cnt = cnt + findWays ( N - i , dp ) ; } } return dp [ N ] = cnt ; } let N = 4 ; let dp = new Array ( N + 1 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) dp [ i ] = - 1 ; document . write ( findWays ( N , dp ) ) ;


"Count ways to obtain given sum by repeated throws of a dice | Function to calculate the total number of ways to have sum N ; Initialize dp array ; Iterate over all the possible intermediate values to reach N ; Calculate the sum for all 6 faces ; Print the total number of ways ; Given sum N ; Function call"
How can the above be solved in JS?

function findWays ( N ) { let dp = new Array ( N + 1 ) ; dp [ 0 ] = 1 ; for ( let i = 1 ; i <= N ; i ++ ) { dp [ i ] = 0 ; for ( let j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 ) { dp [ i ] = dp [ i ] + dp [ i - j ] ; } } } document . write ( dp [ N ] ) ; } let N = 4 ; findWays ( N ) ;


"Count ways to obtain given sum by repeated throws of a dice | Function to find the number of ways to get the sum N with throw of dice ; Base Case ; Stores the count of total number of ways to get sum N ; Recur for all 6 states ; Return answer ; Driver Code ; Function call"
How can the above be solved in JS?

function findWays ( N ) { if ( N == 0 ) { return 1 ; } var cnt = 0 ; for ( var i = 1 ; i <= 6 ; i ++ ) { if ( N - i >= 0 ) { cnt = cnt + findWays ( N - i ) ; } } return cnt ; } var N = 4 ; document . write ( findWays ( N ) ) ;


"Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step | Function to find the number the number of ways to reach Nth stair ; Array including number of ways that includes 3 ; Array including number of ways that doesn 't includes 3 ; Initially to reach 3 stairs by taking 3 steps can be reached by 1 way ; Loop to find the number the number of ways to reach Nth stair ; Driver Code"
How can the above be solved in JS?

function number_of_ways ( n ) { let includes_3 = new Uint8Array ( n + 1 ) ; let not_includes_3 = new Uint8Array ( n + 1 ) ; includes_3 [ 3 ] = 1 ; not_includes_3 [ 1 ] = 1 ; not_includes_3 [ 2 ] = 2 ; not_includes_3 [ 3 ] = 3 ; for ( let i = 4 ; i <= n ; i ++ ) { includes_3 [ i ] = includes_3 [ i - 1 ] + includes_3 [ i - 2 ] + not_includes_3 [ i - 3 ] ; not_includes_3 [ i ] = not_includes_3 [ i - 1 ] + not_includes_3 [ i - 2 ] ; } return includes_3 [ n ] ; } let n = 7 ; document . write ( number_of_ways ( n ) ) ;


"Count ways to reach a score using 1 and 2 with no consecutive 2 s | A simple recursive implementation for counting ways to reach a score using 1 and 2 with consecutive 2 allowed ; base cases ; For cases n > 2 ; Driver code"
How can the above be solved in JS?

function CountWays ( n , flag ) { if ( n == 0 ) { return 1 ; } if ( n == 1 ) { return 1 ; } if ( n == 2 ) { return 1 + 1 ; } return CountWays ( n - 1 ) + CountWays ( n - 3 ) ; } let n = 5 ; document . write ( CountWays ( n , false ) ) ;


"Count ways to reach a score using 1 and 2 with no consecutive 2 s | Bottom up approach for counting ways to reach a score using 1 and 2 with consecutive 2 allowed ; noOfWays [ i ] will store count for last 3 values before i . ; Loop till " n + 1" to compute value for " n " ; number of ways if first run is 1 ; number of ways if first run is 2 and second run is 1 ; Remember last 3 values ; Driver code"
How can the above be solved in JS?

function CountWays ( n ) { var noOfWays = Array ( 3 ) . fill ( 0 ) ; noOfWays [ 0 ] = 1 ; noOfWays [ 1 ] = 1 ; noOfWays [ 2 ] = 1 + 1 ; for ( var i = 3 ; i < n + 1 ; i ++ ) { noOfWays [ i ] = noOfWays [ 3 - 1 ] + noOfWays [ 3 - 3 ] ; noOfWays [ 0 ] = noOfWays [ 1 ] ; noOfWays [ 1 ] = noOfWays [ 2 ] ; noOfWays [ 2 ] = noOfWays [ i ] ; } return noOfWays [ n ] ; } var n = 5 ; document . write ( CountWays ( n ) ) ;


"Count ways to reach a score using 1 and 2 with no consecutive 2 s | Bottom up approach for counting ways to reach a score using 1 and 2 with consecutive 2 allowed ; noOfWays [ i ] will store count for value i . 3 extra values are to take care of cornser case n = 0 ; Loop till " n + 1" to compute value for " n " ; number of ways if first run is 1 number of ways if first run is 2 and second run is 1 ; Driver code"
How can the above be solved in JS?

function CountWays ( n ) { var noOfWays = Array ( n + 3 ) . fill ( 0 ) ; noOfWays [ 0 ] = 1 ; noOfWays [ 1 ] = 1 ; noOfWays [ 2 ] = 1 + 1 ; for ( var i = 3 ; i < n + 1 ; i ++ ) { noOfWays [ i ] = noOfWays [ i - 1 ] + noOfWays [ i - 3 ] ; } return noOfWays [ n ] ; } var n = 5 ; document . write ( CountWays ( n ) ) ;


"Count ways to represent a number as sum of perfect squares | Store perfect squares less than or equal to N ; Utility function to calculate perfect squares less than or equal to N ; Function to find the number of ways to represent a number as sum of perfect squares ; Handle the base cases ; Include the i - th index element ; Exclude the i - th index element ; Return the result ; Given Input ; Precalculate perfect squares <= N ; Function Call"
How can the above be solved in JS?

var psquare = [ ] function calcPsquare ( N ) { var i ; for ( i = 1 ; i * i <= N ; i ++ ) psquare . push ( i * i ) ; } function countWays ( index , target ) { if ( target == 0 ) return 1 ; if ( index < 0 target < 0 ) return 0 ; var inc = countWays ( index , target - psquare [ index ] ) ; var exc = countWays ( index - 1 , target ) ; return inc + exc ; } var N = 9 ; calcPsquare ( N ) ; document . write ( countWays ( psquare . length - 1 , N ) ) ;


"Count ways to represent an integer as an exponent | Function to calculate GCD of a and b using Euclidean Algorithm ; Iterate until b is non - zero ; Return the GCD ; Function to count the number of ways N can be expressed as x ^ y ; Base Case ; Stores the gcd of powers ; Calculate the degree of 2 in N ; Calculate the degree of prime numbers in N ; Calculate the degree of prime ' i ' in N ; If N is a prime , g becomes 1. ; Stores the number of ways to represent N as x ^ y ; Find the number of Factors of g ; Update the count of ways ; Iterate to find rest of the prime numbers ; Find the power of i ; Update the count of ways ; If g is prime ; Return the total number of ways ; Driver Code"
How can the above be solved in JS?

function gcd ( a , b ) { while ( b > 0 ) { let rem = a % b ; a = b ; b = rem ; } return a ; } function countNumberOfWays ( n ) { if ( n == 1 ) return - 1 ; let g = 0 ; let power = 0 ; while ( n % 2 == 0 ) { power ++ ; n /= 2 ; } g = gcd ( g , power ) ; for ( let i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { power = 0 ; while ( n % i == 0 ) { power ++ ; n /= i ; } g = gcd ( g , power ) ; } if ( n > 2 ) g = gcd ( g , 1 ) ; let ways = 1 ; power = 0 ; while ( g % 2 == 0 ) { g /= 2 ; power ++ ; } ways *= ( power + 1 ) ; for ( let i = 3 ; i <= Math . sqrt ( g ) ; i += 2 ) { power = 0 ; while ( g % i == 0 ) { power ++ ; g /= i ; } ways *= ( power + 1 ) ; } if ( g > 2 ) ways *= 2 ; return ways ; } let N = 64 ; document . write ( countNumberOfWays ( N ) ) ;


"Count ways to select N pairs of candies of distinct colors ( Dynamic Programming + Bitmasking ) | Function to count ways to select N distinct pairs of candies with different colours ; If n pairs are selected ; Stores count of ways to select the i - th pair ; Iterate over the range [ 0 , n ] ; If pair ( i , j ) is not included ; Driver Code"
How can the above be solved in JS?

function numOfWays ( a , n , i , blue ) { if ( i == n ) return 1 ; let count = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( a [ i ] [ j ] == 1 && ! blue . has ( j ) ) { blue . add ( j ) ; count += numOfWays ( a , n , i + 1 , blue ) ; blue . delete ( j ) ; } } return count ; } let n = 3 ; let mat = [ [ 0 , 1 , 1 ] , [ 1 , 0 , 1 ] , [ 1 , 1 , 1 ] ] ; let mpp = new Set ( ) ; document . write ( numOfWays ( mat , n , 0 , mpp ) ) ;


"Count ways to split a Binary String into three substrings having equal count of zeros | Function to calculate the number of ways to split ; Calculating the total number of zeros ; Case1 If total count of zeros is not divisible by 3 ; Case2 if total count of zeros is zero ; Number of zeros in each substring ; Initialising zero to the number of ways for first and second cut ; Initializing the count ; Traversing from the beginning ; Incrementing the count if the element is '0' ; Incrementing the ways for the 1 st cut if count is equal to zeros required in each substring ; Incrementing the ways for the 2 nd cut if count is equal to 2 * ( zeros required in each substring ) ; Total number of ways to split is multiplication of ways for the 1 st and 2 nd cut ; Driver code ; Function call"
How can the above be solved in JS?

function splitstring ( s ) { let n = s . length ; let zeros = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( s [ i ] == ' ' ) zeros ++ ; if ( zeros % 3 != 0 ) return 0 ; if ( zeros == 0 ) return parseInt ( ( ( n - 1 ) * ( n - 2 ) ) / 2 , 10 ) ; let zerosInEachSubstring = parseInt ( zeros / 3 , 10 ) ; let waysOfFirstCut = 0 ; let waysOfSecondCut = 0 ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) count ++ ; if ( count == zerosInEachSubstring ) waysOfFirstCut ++ ; else if ( count == 2 * zerosInEachSubstring ) waysOfSecondCut ++ ; } return waysOfFirstCut * waysOfSecondCut ; } let s = " " ; document . write ( " " + " " + splitstring ( s ) ) ;


"Count ways to split a Binary String into three substrings having equal count of zeros | Function to return ways to split a string into three parts with the equal number of 0 ; Store total count of 0 s ; Count total no . of 0 s character in given string ; If total count of 0 character is not divisible by 3 ; Initialize mp to store frequency of k ; Traverse string to find ways to split string ; Increment count if 0 appears ; Increment result if sum equal to 2 * k and k exists in mp ; Insert sum in mp ; Return result ; Given string ; Function call"
How can the above be solved in JS?

function count ( s ) { var cnt = 0 ; s . split ( ' ' ) . forEach ( c => { cnt += ( c == ' ' ) ? 1 : 0 ; } ) ; if ( cnt % 3 != 0 ) return 0 ; var res = 0 , k = parseInt ( cnt / 3 ) , sum = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < s . length ; i ++ ) { sum += ( s [ i ] == ' ' ) ? 1 : 0 ; if ( sum == 2 * k && mp . has ( k ) && i < s . length - 1 && i > 0 ) { res += mp . get ( k ) ; } if ( mp . has ( sum ) ) mp . set ( sum , mp . get ( sum ) + 1 ) else mp . set ( sum , 1 ) ; } return res ; } var str = " " ; document . write ( count ( str ) ) ;


"Counting sets of 1 s and 0 s in a binary matrix | no of columns ; no of rows ; function to calculate the number of non empty sets of cell ; stores the final answer ; traverses row - wise ; traverses column wise ; at the end subtract n * m as no of single sets have been added twice . ; Driver code"
How can the above be solved in JS?

var m = 3 ; var n = 2 ; function countSets ( a ) { var res = 0 ; for ( i = 0 ; i < n ; i ++ ) { var u = 0 , v = 0 ; for ( j = 0 ; j < m ; j ++ ) { if ( a [ i ] [ j ] == 1 ) u ++ ; else v ++ ; } res += Math . pow ( 2 , u ) - 1 + Math . pow ( 2 , v ) - 1 ; } for ( i = 0 ; i < m ; i ++ ) { var u = 0 , v = 0 ; for ( j = 0 ; j < n ; j ++ ) { if ( a [ j ] [ i ] == 1 ) u ++ ; else v ++ ; } res += Math . pow ( 2 , u ) - 1 + Math . pow ( 2 , v ) - 1 ; } return res - ( n * m ) ; } var a = [ [ 1 , 0 , 1 ] , [ 0 , 1 , 0 ] ] ; document . write ( countSets ( a ) ) ;


"Counts Path in an Array | Javascript implementation of above approach ; find the number of ways to reach the end ; dp to store value ; base case ; Bottom up dp structure ; F [ i ] is dependent of F [ i + 1 ] to F [ i + k ] ; Return value of dp [ 0 ] ; Driver code"
How can the above be solved in JS?

let mod = ( 1e9 + 7 ) ; function ways ( arr , n ) { let dp = new Array ( n + 1 ) ; dp . fill ( 0 ) ; dp [ n - 1 ] = 1 ; for ( let i = n - 2 ; i >= 0 ; i -- ) { dp [ i ] = 0 ; for ( let j = 1 ; ( ( j + i ) < n && j <= arr [ i ] ) ; j ++ ) { dp [ i ] += dp [ i + j ] ; dp [ i ] %= mod ; } } return dp [ 0 ] % mod ; } let arr = [ 5 , 3 , 1 , 4 , 3 ] ; let n = arr . length ; document . write ( ways ( arr , n ) % mod ) ;


"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"
How can the above be solved in JS?

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 ) ) ;


"Counts paths from a point to reach Origin | DP based function to count number of paths ; Fill entries in bottommost row and leftmost columns ; Fill DP in bottom up manner ; Driver Code"
How can the above be solved in JS?

function countPaths ( n , m ) { var dp = Array ( n + 1 ) . fill ( 0 ) . map ( x => Array ( m + 1 ) . fill ( 0 ) ) ; for ( i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( i = 1 ; i <= n ; i ++ ) for ( j = 1 ; j <= m ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] ; return dp [ n ] [ m ] ; } var n = 3 , m = 2 ; document . write ( " " + countPaths ( n , m ) ) ;


"Counts paths from a point to reach Origin | Recursive function to count number of paths ; If we reach bottom or top left , we are have only one way to reach ( 0 , 0 ) ; Else count sum of both ways ; Driver Code"
How can the above be solved in JS?

function countPaths ( n , m ) { if ( n == 0 m == 0 ) return 1 ; return ( countPaths ( n - 1 , m ) + countPaths ( n , m - 1 ) ) ; } let n = 3 , m = 2 ; document . write ( " " + countPaths ( n , m ) ) ;


"Counts paths from a point to reach Origin | utility min ; Function for binomial Coefficient ; Constructing Pascal 's Triangle ; Driver Code"
How can the above be solved in JS?

function min ( a , b ) { return a < b ? a : b ; } function binomialCoeff ( n , k ) { var C = Array ( k + 1 ) . fill ( 0 ) ; C [ 0 ] = 1 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } var n = 3 , m = 2 ; document . write ( " " + binomialCoeff ( n + m , n ) ) ;


"Cutting a Rod | DP | Global Array for the purpose of memoization . ; A recursive program , using , memoization , to implement the rod cutting problem ( Top - Down ) . ; The maximum price will be zero , when either the length of the rod is zero or price is zero . ; If the length of the rod is less than the maximum length , Max_lene will consider it . Now depending upon the profit , either Max_lene we will take it or discard it . ; If the length of the rod is greater than the permitted size , Max_len we will not consider it . ; Max_lene Max_lenill return the maximum value obtained , Max_lenhich is present at the nth roMax_len and Max_lenth column . ; Driver code ; Function Call"
How can the above be solved in JS?

let t = new Array ( 9 ) ; for ( var i = 0 ; i < t . length ; i ++ ) { t [ i ] = new Array ( 2 ) ; } function un_kp ( price , length , Max_len , n ) { if ( n == 0 Max_len == 0 ) { return 0 ; } if ( length [ n - 1 ] <= Max_len ) { t [ n ] [ Max_len ] = Math . max ( price [ n - 1 ] + un_kp ( price , length , Max_len - length [ n - 1 ] , n ) , un_kp ( price , length , Max_len , n - 1 ) ) ; } else { t [ n ] [ Max_len ] = un_kp ( price , length , Max_len , n - 1 ) ; } return t [ n ] [ Max_len ] ; } let price = [ 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ] ; let n = price . length ; let length = Array ( n ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { length [ i ] = i + 1 ; } let Max_len = n ; document . write ( " " + un_kp ( price , length , n , Max_len ) ) ;


"Cutting a Rod | DP | Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Build the table val [ ] in bottom up manner and return the last entry from the table ; Driver Code"
How can the above be solved in JS?

function cutRod ( price , n ) { let val = new Array ( n + 1 ) ; val [ 0 ] = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let max_val = Number . MIN_VALUE ; for ( let j = 0 ; j < i ; j ++ ) max_val = Math . max ( max_val , price [ j ] + val [ i - j - 1 ] ) ; val [ i ] = max_val ; } return val [ n ] ; } let arr = [ 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ] ; let size = arr . length ; document . write ( " " + cutRod ( arr , size ) + " " ) ;


"Cutting a Rod | DP | Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Recursively cut the rod in different pieces and compare different configurations ; Driver Code"
How can the above be solved in JS?

function cutRod ( price , n ) { if ( n <= 0 ) return 0 ; let max_val = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) max_val = Math . max ( max_val , price [ i ] + cutRod ( price , n - i - 1 ) ) ; return max_val ; } let arr = [ 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ] ; let size = arr . length ; document . write ( " " + cutRod ( arr , size ) ) ;


"D Numbers | Function to find the N - th icosikaipentagon number ; Number should be greater than 3 ; Check every k in range 2 to n - 1 ; Condition for D - Number ; Driver Code"
How can the above be solved in JS?

function isDNum ( n ) { if ( n < 4 ) return false ; let numerator = 0 , hcf = 0 ; for ( k = 2 ; k <= n ; k ++ ) { numerator = parseInt ( ( Math . pow ( k , n - 2 ) - k ) ) ; hcf = __gcd ( n , k ) ; } if ( hcf == 1 && ( numerator % n ) != 0 ) return false ; return true ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } let n = 15 ; let a = isDNum ( n ) ; if ( a ) document . write ( " " ) ; else document . write ( " " ) ;


"Decimal to Binary using recursion and without using power operator | Recursive function to convert n to its binary equivalent ; Base case ; Recursive call ; Driver code"
How can the above be solved in JS?

function decimalToBinary ( n ) { if ( n == 0 ) { document . write ( " " ) ; return ; } decimalToBinary ( parseInt ( n / 2 ) ) ; document . write ( n % 2 ) ; } var n = 13 ; decimalToBinary ( n ) ;


"Decrypt a string encrypted by repeating i | Function to return the decrypted string ; Initial jump will be 1 ; Increment jump by 1 with every character ; Driver code"
How can the above be solved in JS?

function decryptString ( str , n ) { let i = 0 , jump = 1 ; let decryptedStr = " " ; while ( i < n ) { decryptedStr += str [ i ] ; i += jump ; jump ++ ; } return decryptedStr ; } let str = " " ; let n = str . length ; document . write ( decryptString ( str , n ) ) ;


"Delete odd and even numbers at alternate step such that sum of remaining elements is minimized | Function to find the minimized sum ; If more odd elements ; Sort the elements ; Left - over elements ; Find the sum of leftover elements ; Return the sum ; If more even elements ; Sort the elements ; Left - over elements ; Find the sum of leftover elements ; Return the sum ; If same elements ; Driver code"
How can the above be solved in JS?

function MinimizeleftOverSum ( a , n ) { var v1 = [ ] , v2 = [ ] ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) v1 . push ( a [ i ] ) ; else v2 . push ( a [ i ] ) ; } if ( v1 . length > v2 . length ) { v1 . sort ( ) ; v2 . sort ( ) ; var x = v1 . length - v2 . length - 1 ; var sum = 0 ; var i = 0 ; while ( i < x ) { sum += v1 [ i ++ ] ; } return sum ; } else if ( v2 . length > v1 . length ) { v1 . sort ( ) ; v2 . sort ( ) ; var x = v2 . length - v1 . length - 1 ; var sum = 0 ; var i = 0 ; while ( i < x ) { sum += v2 [ i ++ ] ; } return sum ; } else return 0 ; } var a = [ 2 , 2 , 2 , 2 ] ; var n = a . length ; document . write ( MinimizeleftOverSum ( a , n ) ) ;


"Diameter of a Binary Indexed Tree with N nodes | Function to find diameter of BIT with N + 1 nodes ; L is size of subtree just before subtree in which N lies ; H is the height of subtree just before subtree in which N lies ; Base Cases ; Size of subtree are power of 2 ; 3 Cases as explained in Approach ; Driver Code"
How can the above be solved in JS?

function diameter ( n ) { var L , H , templen ; L = 1 ; H = 0 ; if ( n == 1 ) { return 1 ; } if ( n == 2 ) { return 2 ; } if ( n == 3 ) { return 3 ; } while ( L * 2 <= n ) { L *= 2 ; H ++ ; } if ( n >= L * 2 - 1 ) return 2 * H + 1 ; else if ( n >= L + ( L / 2 ) - 1 ) return 2 * H ; return 2 * H - 1 ; } var n = 15 ; document . write ( diameter ( n ) ) ;


"Difference between Insertion sort and Selection sort | Function to implement the selection sort ; One by one move boundary of unsorted subarray ; Find the minimum element in unsorted array ; Swap the found minimum element with the first element ; Function to print an array ; Driver Code ; Function Call ; Print the array"
How can the above be solved in JS?

function selectionSort ( arr , n ) { let i , j , min_idx ; for ( i = 0 ; i < n - 1 ; i ++ ) { min_idx = i ; for ( j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] < arr [ min_idx ] ) min_idx = j ; let temp = arr [ min_idx ] ; arr [ min_idx ] = arr [ i ] ; arr [ i ] = temp ; } } function printArray ( arr , size ) { let i ; for ( i = 0 ; i < size ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } let arr = [ 64 , 25 , 12 , 22 , 11 ] ; let n = arr . length ; selectionSort ( arr , n ) ; document . write ( " " ) ; printArray ( arr , n ) ;


"Difference between Insertion sort and Selection sort | Function to sort an array using insertion sort ; Move elements of arr [ 0. . i - 1 ] , that are greater than key to one position ahead of their current position ; Function to print an array of size N ; Print the array ; Driver code ; Function Call"
How can the above be solved in JS?

function insertionSort ( arr , n ) { let i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; } arr [ j + 1 ] = key ; } } function printArray ( arr , n ) { let i ; for ( i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } let arr = [ 12 , 11 , 13 , 5 , 6 ] ; let N = arr . length ; insertionSort ( arr , N ) ; printArray ( arr , N ) ;


"Distance between a point and a Plane in 3 D | Function to find distance ; driver code ; Function call"
How can the above be solved in JS?

function shortest_distance ( x1 , y1 , z1 , a , b , c , d ) { d = Math . abs ( ( a * x1 + b * y1 + c * z1 + d ) ) ; let e = Math . sqrt ( a * a + b * b + c * c ) ; document . write ( " " + ( d / e ) ) ; return ; } let x1 = 4 ; let y1 = - 4 ; let z1 = 3 ; let a = 2 ; let b = - 2 ; let c = 5 ; let d = 8 ; shortest_distance ( x1 , y1 , z1 , a , b , c , d ) ;


"Distance between two parallel Planes in 3 | Function to find distance ; Driver Code"
How can the above be solved in JS?


"Divide array in two maximum equal length arrays of similar and dissimilar elements | Function to find the max - size to which an array can be divided leto 2 equal parts ; Array to find the frequency of each element of array ; Find the index maximum frequency element present in array arr [ ] ; Find total unique elements present in array arr [ ] ; Find the Max - Size to which an array arr [ ] can be splitted ; Find the first array containing same elements ; Find the second array containing unique elements ; initialise n ; array declaration ; size of array"
How can the above be solved in JS?

function Solve ( arr , size , n ) { let v = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < size ; i ++ ) v [ arr [ i ] ] ++ ; let max1 = - 1 , mx = - 1 ; for ( let i = 0 ; i < v . length ; i ++ ) { if ( v [ i ] > mx ) { mx = v [ i ] ; max1 = i ; } } let cnt = 0 ; for ( let i in v ) { if ( i == 0 ) ++ cnt ; } let diff1 = n + 1 - cnt ; let max_size = Math . max ( Math . min ( v [ max1 ] - 1 , diff1 ) , Math . min ( v [ max1 ] , diff1 - 1 ) ) ; document . write ( " " + max_size + " " ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < max_size ; i ++ ) { document . write ( max1 + " " ) ; v [ max1 ] -= 1 ; } document . write ( " " ) ; document . write ( " " + " " ) ; for ( let i = 0 ; i < ( n + 1 ) ; i ++ ) { if ( v [ i ] > 0 ) { document . write ( i + " " ) ; max_size -- ; } if ( max_size < 1 ) break ; } document . write ( " " ) ; } let n = 7 ; let arr = [ 1 , 2 , 1 , 5 , 1 , 6 , 7 , 2 ] ; let size = arr . length ; Solve ( arr , size , n ) ;


"Double Knapsack | Dynamic Programming | Javascript implementation of the above approach ; 3D array to store states of DP ; w1_r represents remaining capacity of 1 st knapsack w2_r represents remaining capacity of 2 nd knapsack i represents index of the array arr we are working on ; Base case ; Variables to store the result of three parts of recurrence relation ; Store the state in the 3D array ; Input array ; Number of elements in the array ; Capacity of knapsacks ; Function to be called"
How can the above be solved in JS?

var maxN = 31 var maxW = 31 var dp = Array ( maxN ) ; for ( var i = 0 ; i < maxN ; i ++ ) { dp [ i ] = Array ( maxW ) ; for ( var j = 0 ; j < maxW ; j ++ ) { dp [ i ] [ j ] = Array ( maxW ) . fill ( - 1 ) ; } } function maxWeight ( arr , n , w1_r , w2_r , i ) { if ( i == n ) return 0 ; if ( dp [ i ] [ w1_r ] [ w2_r ] != - 1 ) return dp [ i ] [ w1_r ] [ w2_r ] ; var fill_w1 = 0 , fill_w2 = 0 , fill_none = 0 ; if ( w1_r >= arr [ i ] ) fill_w1 = arr [ i ] + maxWeight ( arr , n , w1_r - arr [ i ] , w2_r , i + 1 ) ; if ( w2_r >= arr [ i ] ) fill_w2 = arr [ i ] + maxWeight ( arr , n , w1_r , w2_r - arr [ i ] , i + 1 ) ; fill_none = maxWeight ( arr , n , w1_r , w2_r , i + 1 ) ; dp [ i ] [ w1_r ] [ w2_r ] = Math . max ( fill_none , Math . max ( fill_w1 , fill_w2 ) ) ; return dp [ i ] [ w1_r ] [ w2_r ] ; } var arr = [ 8 , 2 , 3 ] ; var n = arr . length ; var w1 = 10 , w2 = 3 ; document . write ( maxWeight ( arr , n , w1 , w2 , 0 ) ) ;


"Double the first element and move zero to end | Function For Swaping Two Element Of An Array ; shift all zero to left side of an array ; Maintain last index with positive value ; If Element is non - zero ; swap current index , with lastSeen non - zero ; next element will be last seen non - zero"
How can the above be solved in JS?

function swap ( A , i , j ) { let temp = A [ i ] ; A [ i ] = A [ j ] ; A [ j ] = temp ; } function shiftAllZeroToLeft ( array , n ) { let lastSeenNonZero = 0 ; for ( let index = 0 ; index < n ; index ++ ) { if ( array [ index ] != 0 ) { swap ( array , array [ index ] , array [ lastSeenNonZero ] ) ; lastSeenNonZero ++ ; } } } }


"Double the first element and move zero to end | function which pushes all zeros to end of an array . ; Count of non - zero elements ; Traverse the array . If element encountered is non - zero , then replace the element at index ' count ' with this element ; here count is incremented ; Now all non - zero elements have been shifted to front and ' count ' is set as index of first 0. Make all elements 0 from count to end . ; function to rearrange the array elements after modification ; if ' arr [ ] ' contains a single element only ; traverse the array ; if true , perform the required modification ; double current index value ; put 0 in the next index ; increment by 1 so as to move two indexes ahead during loop iteration ; push all the zeros at the end of ' arr [ ] ' ; function to print the array elements ; Driver program to test above"
How can the above be solved in JS?

function pushZerosToEnd ( arr , n ) { var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 0 ) arr [ count ++ ] = arr [ i ] ; while ( count < n ) arr [ count ++ ] = 0 ; } function modifyAndRearrangeArr ( arr , n ) { if ( n == 1 ) return ; for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != 0 && arr [ i ] == arr [ i + 1 ] ) { arr [ i ] = 2 * arr [ i ] ; arr [ i + 1 ] = 0 ; i ++ ; } } pushZerosToEnd ( arr , n ) ; } function printArray ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } var arr = [ 0 , 2 , 2 , 2 , 0 , 6 , 6 , 0 , 0 , 8 ] ; var n = arr . length ; document . write ( " " ) ; printArray ( arr , n ) ; modifyAndRearrangeArr ( arr , n ) ; document . write ( " " ) ; document . write ( " " ) ; printArray ( arr , n ) ;


"Encrypt a string by repeating i | Function to return the encrypted String ; Number of times the current character will be repeated ; Repeat the current character in the encrypted String ; Driver code"
How can the above be solved in JS?

function encryptString ( str , n ) { let i = 0 , cnt = 0 ; let encryptedStr = " " ; while ( i < n ) { cnt = i + 1 ; while ( cnt -- > 0 ) encryptedStr += str [ i ] ; i ++ ; } return encryptedStr ; } let str = " " ; let n = str . length ; document . write ( encryptString ( str , n ) ) ;


"Encrypt string with product of number of vowels and consonants in substring of size k | isVowel ( ) is a function that returns true for a vowel and false otherwise . ; function to Encrypt the String ; cv to count vowel cc to count consonants ; Counting prefix count of vowel and prefix count of consonants ; generating the encrypted String . ; Driver Code"
How can the above be solved in JS?

function isVowel ( c ) { return c === " " || c === " " || c === " " || c === " " || c === " " ; } function encryptString ( s , n , k ) { var cv = new Array ( n ) . fill ( 0 ) ; var cc = new Array ( n ) . fill ( 0 ) ; if ( isVowel ( s [ 0 ] ) ) cv [ 0 ] = 1 ; else cc [ 0 ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) { cv [ i ] = cv [ i - 1 ] + ( isVowel ( s [ i ] ) === true ? 1 : 0 ) ; cc [ i ] = cc [ i - 1 ] + ( isVowel ( s [ i ] ) === true ? 0 : 1 ) ; } var ans = " " ; var prod = 0 ; prod = cc [ k - 1 ] * cv [ k - 1 ] ; ans += prod ; for ( var i = k ; i < s . length ; i ++ ) { prod = ( cc [ i ] - cc [ i - k ] ) * ( cv [ i ] - cv [ i - k ] ) ; ans += prod ; } return ans ; } var s = " " ; var n = s . length ; var k = 2 ; document . write ( encryptString ( s . split ( " " ) , n , k ) + " " ) ;


"Encrypt string with product of number of vowels and consonants in substring of size k | isVowel ( ) is a function that returns true for a vowel and false otherwise . ; function to Encrypt the dtring ; for each substring ; substring of size k ; counting number of vowels and consonants ; append product to answer . ; Driven Program"
How can the above be solved in JS?

function isVowel ( c ) { return ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) ; } function encryptString ( s , n , k ) { var countVowels = 0 ; var countConsonants = 0 ; var ans = " " ; for ( var l = 0 ; l <= n - k ; l ++ ) { countVowels = 0 ; countConsonants = 0 ; for ( var r = l ; r <= l + k - 1 ; r ++ ) { if ( isVowel ( s [ r ] ) == true ) countVowels ++ ; else countConsonants ++ ; } ans += ( countVowels * countConsonants ) . toString ( ) ; } return ans ; } var s = " " ; var n = s . length ; var k = 2 ; document . write ( encryptString ( s , n , k ) ) ;


"Encrypt the given string with the following operations | Javascript implementation of the above approach : ; Function to return the encrypted string ; Reduce x because rotation of length 26 is unnecessary ; Calculate the frequency of characters ; If the frequency of current character is even then increment it by x ; Else decrement it by x ; Return the count ; Driver code"
How can the above be solved in JS?

var MAX = 26 ; function encryptStr ( str , n , x ) { x = x % MAX ; var freq = Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } for ( var i = 0 ; i < n ; i ++ ) { if ( freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] % 2 == 0 ) { var pos = ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + x ) % MAX ; str [ i ] = String . fromCharCode ( pos + ' ' . charCodeAt ( 0 ) ) ; } else { var pos = ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) - x ) ; if ( pos < 0 ) { pos += MAX ; } str [ i ] = String . fromCharCode ( pos + ' ' . charCodeAt ( 0 ) ) ; } } return str . join ( ' ' ) ; } var s = " " ; var n = s . length ; var x = 3 ; document . write ( encryptStr ( s . split ( ' ' ) , n , x ) ) ;


"Encrypt the string | Function to convert Decimal to Hex ; Function to encrypt the string ; Iterate the characters of the string ; Iterate until S [ i ] is equal to ch ; Update count and i ; Decrement i by 1 ; Convert count to hexadecimal representation ; Append the character ; Append the characters frequency in hexadecimal representation ; Reverse the obtained answer ; Return required answer ; Given Input ; Function Call"
How can the above be solved in JS?

function convertToHex ( num ) { let temp = " " ; while ( num != 0 ) { let rem = num % 16 ; let c = 0 ; if ( rem < 10 ) { c = rem + 48 ; } else { c = rem + 87 ; } temp += String . fromCharCode ( c ) ; num = Math . floor ( num / 16 ) ; } return temp ; } function encryptString ( S , N ) { let ans = " " ; for ( let i = 0 ; i < N ; i ++ ) { let ch = S [ i ] ; let count = 0 ; let hex ; while ( i < N && S [ i ] == ch ) { count ++ ; i ++ ; } i -- ; hex = convertToHex ( count ) ; ans += ch ; ans += hex ; } ans = ans . split ( ' ' ) . reverse ( ) . join ( " " ) ; return ans ; } let S = " " ; let N = S . length ; document . write ( encryptString ( S , N ) ) ;


"Enneacontahexagon numbers | Function to find the nth Enneacontahexagon Number ; Driver code"
How can the above be solved in JS?

function EnneacontahexagonNum ( n ) { return ( 94 * n * n - 92 * n ) / 2 ; } let n = 3 ; document . write ( EnneacontahexagonNum ( n ) ) ;


"Euler zigzag numbers ( Alternating Permutation ) | Function to print first n zigzag numbers ; To store factorial and n 'th zig zag number ; Initialize factorial upto n ; Set first two zig zag numbers ; Print first two zig zag number ; Print the rest zig zag numbers ; Binomial ( n , k ) * a ( k ) * a ( n - k ) ; Store the value ; Print the number ; Driver code ; Function call"
How can the above be solved in JS?

function ZigZag ( n ) { var fact = Array ( n + 1 ) . fill ( 0 ) ; var zig = Array ( n + 1 ) . fill ( 0 ) ; fact [ 0 ] = 1 ; for ( var i = 1 ; i <= n ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; zig [ 0 ] = 1 ; zig [ 1 ] = 1 ; document . write ( " " ) ; document . write ( zig [ 0 ] + " " + zig [ 1 ] + " " ) ; for ( var i = 2 ; i < n ; i ++ ) { var sum = 0 ; for ( var k = 0 ; k <= i - 1 ; k ++ ) { sum += parseInt ( fact [ i - 1 ] / ( fact [ i - 1 - k ] * fact [ k ] ) ) * zig [ k ] * zig [ i - 1 - k ] ; } zig [ i ] = parseInt ( sum / 2 ) ; document . write ( parseInt ( sum / 2 ) + " " ) ; } } var n = 10 ; ZigZag ( n ) ;


"Expected Number of Trials to get N Consecutive Heads | Driver Code ; Formula for number of trails for N consecutive heads"
How can the above be solved in JS?

let N = 3 ; document . write ( Math . pow ( 2 , N + 1 ) - 2 ) ;


"Exponential factorial of N | Function to find exponential factorial of a given number ; Stores the exponetial factor of N ; Iterare over the range [ 2 , N ] ; Update res ; Return res ; Input ; Function call"
How can the above be solved in JS?

function ExpoFactorial ( N ) { let res = 1 ; let mod = 1000000007 ; for ( let i = 2 ; i < N + 1 ; i ++ ) res = Math . pow ( i , res ) % mod ; return res ; } let N = 4 ; document . write ( ( ExpoFactorial ( N ) ) ) ;


"Farthest distance of a Node from each Node of a Tree | JavaScript program to implement the above approach ; Adjacency List to store the graph ; Stores the height of each node ; Stores the maximum distance of a node from its ancestors ; Function to add edge between two vertices ; Insert edge from u to v ; Insert edge from v to u ; Function to calculate height of each Node ; Iterate in the adjacency list of the current node ; Dfs for child node ; Calculate height of nodes ; Increase height ; Function to calculate the maximum distance of a node from its ancestor ; Iterate in the adjacency list of the current node ; Find two children with maximum heights ; Calculate the maximum distance with ancestor for every node ; Calculating for children ; Driver Code ; Calculate height of nodes of the tree ; Calculate the maximum distance with ancestors ; Print the maximum of the two distances from each node"
How can the above be solved in JS?

let maxN = 100001 ; let adj = new Array ( maxN ) ; adj . fill ( 0 ) ; let height = new Array ( maxN ) ; height . fill ( 0 ) ; let dist = new Array ( maxN ) ; dist . fill ( 0 ) ; function addEdge ( u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } function dfs1 ( cur , par ) { for ( let u = 0 ; u < adj [ cur ] . length ; u ++ ) { if ( adj [ cur ] [ u ] != par ) { dfs1 ( adj [ cur ] [ u ] , cur ) ; height [ cur ] = Math . max ( height [ cur ] , height [ adj [ cur ] [ u ] ] ) ; } } height [ cur ] += 1 ; } function dfs2 ( cur , par ) { let max1 = 0 ; let max2 = 0 ; for ( let u = 0 ; u < adj [ cur ] . length ; u ++ ) { if ( adj [ cur ] [ u ] != par ) { if ( height [ adj [ cur ] [ u ] ] >= max1 ) { max2 = max1 ; max1 = height [ adj [ cur ] [ u ] ] ; } else if ( height [ adj [ cur ] [ u ] ] > max2 ) { max2 = height [ adj [ cur ] [ u ] ] ; } } } let sum = 0 ; for ( let u = 0 ; u < adj [ cur ] . length ; u ++ ) { if ( adj [ cur ] [ u ] != par ) { sum = ( ( max1 == height [ adj [ cur ] [ u ] ] ) ? max2 : max1 ) ; if ( max1 == height [ adj [ cur ] [ u ] ] ) dist [ adj [ cur ] [ u ] ] = 1 + Math . max ( 1 + max2 , dist [ cur ] ) ; else dist [ adj [ cur ] [ u ] ] = 1 + Math . max ( 1 + max1 , dist [ cur ] ) ; dfs2 ( adj [ cur ] [ u ] , cur ) ; } } } let n = 6 ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; addEdge ( 1 , 2 ) ; addEdge ( 2 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 5 , 6 ) ; dfs1 ( 1 , 0 ) ; dfs2 ( 1 , 0 ) ; for ( let i = 1 ; i <= n ; i ++ ) document . write ( ( Math . max ( dist [ i ] , height [ i ] ) - 1 ) + " " ) ;


"Fast Exponention using Bit Manipulation | Function to return a ^ n ; Stores final answer ; Check if current LSB is set ; Right shift ; Driver Code"
How can the above be solved in JS?

function powerOptimised ( a , n ) { let ans = 1 ; while ( n > 0 ) { let last_bit = ( n & 1 ) ; if ( last_bit > 0 ) { ans = ans * a ; } a = a * a ; n = n >> 1 ; } return ans ; } let a = 3 , n = 5 ; document . write ( powerOptimised ( a , n ) ) ;


"Fibonacci Cube Graph | function to find fibonacci number ; function for finding number of vertices in fibonacci cube graph ; return fibonacci number for f ( n + 2 ) ; driver program"
How can the above be solved in JS?

function fib ( n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } function findVertices ( n ) { return fib ( n + 2 ) ; } var n = 3 ; document . write ( findVertices ( n ) ) ;


"Find ( a ^ b ) % m where ' a ' is very large | Utility function to calculate a % m ; ( s [ i ] - '0' ) gives the digit value and form the number ; Returns find ( a ^ b ) % m ; Find a % m ; Now multiply ans by b - 1 times and take mod with m ; Driver Code"
How can the above be solved in JS?

function aModM ( s , mod ) { let number = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { number = ( number * 10 ) ; let x = ( s [ i ] - ' ' ) ; number = number + x ; number %= mod ; } return number ; } function ApowBmodM ( a , b , m ) { let ans = aModM ( a , m ) ; let mul = ans ; for ( let i = 1 ; i < b ; i ++ ) ans = ( ans * mul ) % m ; return ans ; } let a = " " ; let b = 3 , m = 11 ; document . write ( ApowBmodM ( a , b , m ) ) ;


"Find Bit whose minimum sequence flips makes all bits same | Function to check which bit is to be flipped ; variable to store first and last character of string ; Check if first and last characters are equal , if yes , then return the character which is not at last ; else return last ; Driver Code"
How can the above be solved in JS?

function bitToBeFlipped ( s ) { let last = s [ s . length - 1 ] ; let first = s [ 0 ] ; if ( last == first ) { if ( last == ' ' ) { return ' ' ; } else { return ' ' ; } } else if ( last != first ) { return last ; } } let s = " " ; document . write ( bitToBeFlipped ( s ) , ' ' ) ;


"Find Cube Pairs | Set 2 ( A n ^ ( 1 / 3 ) Solution ) | Function to find pairs that can represent the given number as sum of two cubes ; find cube root of n ; create a array of size of size ' cubeRoot ' ; for index i , cube [ i ] will contain i ^ 3 ; Find all pairs in above sorted array cube whose sum is equal to n ; Driver Code"
How can the above be solved in JS?

function findPairs ( n ) { var cubeRoot = parseInt ( Math . pow ( n , 1.0 / 3.0 ) ) ; var cube = Array . from ( { length : cubeRoot + 1 } , ( _ , i ) => 0 ) ; for ( i = 1 ; i <= cubeRoot ; i ++ ) cube [ i ] = i * i * i ; var l = 1 ; var r = cubeRoot ; while ( l < r ) { if ( cube [ l ] + cube [ r ] < n ) l ++ ; else if ( cube [ l ] + cube [ r ] > n ) r -- ; else { document . write ( " " + l + " " + r + " " ) ; l ++ ; r -- ; } } } var n = 20683 ; findPairs ( n ) ;


"Find K consecutive integers such that their sum is N | Function to check if a number can be expressed as the sum of k consecutive ; Finding the first term of AP ; Checking if first term is an integer ; Loop to prvar the K consecutive integers ; Driver Code"
How can the above be solved in JS?

function checksum ( n , k ) { var first_term = ( ( ( 2 * n ) / k + ( 1 - k ) ) / 2.0 ) ; if ( first_term - parseInt ( ( first_term ) ) == 0 ) { for ( i = parseInt ( first_term ) ; i <= first_term + k - 1 ; i ++ ) { document . write ( i + " " ) ; } } else document . write ( " " ) ; } var n = 33 , k = 6 ; checksum ( n , k ) ;


"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 ) ;"
How can the above be solved in JS?

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 ) ;


"Find K such that changing all elements of the Array greater than K to K will make array sum N | Function to return K such that changing all elements greater than K to K will make array sum N otherwise return - 1 ; Sorting the array in increasing order ; Loop through all the elements of the array ; Checking if sum of array equals N ; Driver code"
How can the above be solved in JS?

function findK ( arr , size , N ) { arr . sort ( function ( a , b ) { return a - b } ) ; let temp_sum = 0 ; for ( let i = 0 ; i < size ; i ++ ) { temp_sum += arr [ i ] ; if ( N - temp_sum == arr [ i ] * ( size - i - 1 ) ) { return arr [ i ] ; } } return - 1 ; } let arr = [ 3 , 1 , 10 , 4 , 8 ] ; let size = arr . length ; let N = 16 ; document . write ( findK ( arr , size , 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"
How can the above be solved in JS?

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 ) ;


"Find M such that GCD of M and given number N is maximum | Function to find the integer M such that gcd ( N , M ) is maximum ; Initialize a variable ; Find all the divisors of N and return the maximum divisor ; Check if i is divisible by N ; Update max_gcd ; Return the maximum value ; Given Number ; Function Call"
How can the above be solved in JS?

function findMaximumGcd ( n ) { let max_gcd = 1 ; for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i > max_gcd ) max_gcd = i ; if ( ( n / i != i ) && ( n / i != n ) && ( ( n / i ) > max_gcd ) ) max_gcd = n / i ; } } return max_gcd ; } let N = 10 ; document . write ( findMaximumGcd ( N ) ) ;


"Find Prime number just less than and just greater each element of given Array | Utility function to check for primality of a number X by checking whether X haACCs any factors other than 1 and itself . ; if ( X % i == 0 ) Factor found ; Function to print primes just less than and just greater than of each element in an array ; Traverse the array ; Traverse for finding prime just less than A [ i ] ; Prime just less than A [ i ] found ; Traverse for finding prime just greater than A [ i ] ; Prime just greater than A [ i ] found ; Input ; Function call"
How can the above be solved in JS?

function isPrime ( X ) { for ( let i = 2 ; i * i <= X ; i ++ ) return false ; return true ; } function printPrimes ( A , N ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = A [ i ] - 1 ; ; j -- ) { if ( isPrime ( j ) ) { document . write ( j + " " ) ; break ; } } for ( let j = A [ i ] + 1 ; ; j ++ ) { if ( isPrime ( j ) ) { document . write ( j + " " ) ; break ; } } document . write ( " " ) ; } } let A = [ 17 , 28 ] ; let N = A . length ; printPrimes ( A , N ) ;


"Find Range Value of the Expression | Function to return the value of the given expression ; Value of the first term ; Value of the last term ; Driver code ; Get the result"
How can the above be solved in JS?

function get ( L , R ) { let x = 1.0 / L ; let y = 1.0 / ( R + 1.0 ) ; return ( x - y ) ; } let L = 6 , R = 12 ; let ans = get ( L , R ) ; document . write ( Math . round ( ans * 100 ) / 100 ) ;


"Find Square Root under Modulo p | Set 1 ( When p is in form of 4 * i + 3 ) | Utility function to do modular exponentiation . It returns ( x ^ y ) % p . ; let res = 1 ; Initialize result 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 true if square root of n under modulo p exists Assumption : p is of the form 3 * i + 4 where i >= 1 ; Try " + ( n ^ ( ( p ▁ + ▁ 1 ) / 4 ) ) " ; Try " - ( n ▁ ^ ▁ ( ( p ▁ + ▁ 1 ) / 4 ) ) " ; If none of the above two work , then square root doesn 't exist ; Driver program to test"
How can the above be solved in JS?

function power ( x , y , p ) { while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } function squareRoot ( n , p ) { if ( p % 4 != 3 ) { document . write ( " " ) ; return ; } n = n % p ; let x = power ( n , Math . floor ( ( p + 1 ) / 4 ) , p ) ; if ( ( x * x ) % p == n ) { document . write ( " " + x ) ; return ; } x = p - x ; if ( ( x * x ) % p == n ) { document . write ( " " + x ) ; return ; } document . write ( " " ) ; } let p = 7 ; let n = 2 ; squareRoot ( n , p ) ;


"Find a number K such that exactly K array elements are greater than or equal to K | Function to find K for which there are exactly K array elements greater than or equal to K ; Finding the largest array element ; Possible values of K ; Traverse the array ; If current array element is greater than or equal to i ; If i array elements are greater than or equal to i ; Otherwise ; To find maximum Element ; Driver Code"
How can the above be solved in JS?

function zvalue ( nums ) { var m = max_element ( nums ) ; var cnt = 0 ; for ( i = 0 ; i <= m ; i ++ ) { cnt = 0 ; for ( j = 0 ; j < nums . length ; j ++ ) { if ( nums [ j ] >= i ) cnt ++ ; } if ( cnt == i ) return i ; } return - 1 ; } function max_element ( nums ) { var max = nums [ 0 ] ; for ( i = 1 ; i < nums . length ; i ++ ) max = Math . max ( max , nums [ i ] ) ; return max ; } nums = [ 7 , 8 , 9 , 0 , 0 , 1 ] ; document . write ( zvalue ( nums ) ) ;


"Find a number such that maximum in array is minimum possible after XOR | Recursive function that find the minimum value after exclusive - OR ; Condition if ref size is zero or bit is negative then return 0 ; Condition if current bit is off then push current value in curr_off vector ; Condition if current bit is on then push current value in curr_on vector ; Condition if curr_off is empty then call recursive function on curr_on vector ; Condition if curr_on is empty then call recursive function on curr_off vector ; Return the minimum of curr_off and curr_on and add power of 2 of current bit ; Function that print the minimum value after exclusive - OR ; Pushing values in vector ; Printing answer ; Driver Code"
How can the above be solved in JS?

function RecursiveFunction ( ref , bit ) { if ( ref . length == 0 bit < 0 ) return 0 ; let curr_on = [ ] , curr_off = [ ] ; for ( let i = 0 ; i < ref . length ; i ++ ) { if ( ( ( ref [ i ] >> bit ) & 1 ) == 0 ) curr_off . push ( ref [ i ] ) ; else curr_on . push ( ref [ i ] ) ; } if ( curr_off . length == 0 ) return RecursiveFunction ( curr_on , bit - 1 ) ; if ( curr_on . length == 0 ) return RecursiveFunction ( curr_off , bit - 1 ) ; return Math . min ( RecursiveFunction ( curr_off , bit - 1 ) , RecursiveFunction ( curr_on , bit - 1 ) ) + ( 1 << bit ) ; } function PrintMinimum ( a , n ) { let v = [ ] ; for ( let i = 0 ; i < n ; i ++ ) v . push ( a [ i ] ) ; document . write ( RecursiveFunction ( v , 30 ) + " " ) ; } let arr = [ 3 , 2 , 1 ] ; let size = arr . length ; PrintMinimum ( arr , size ) ;


"Find a pair ( a , b ) such that Aa + Bb = N | Function to calculate the minimum power of A and B greater than N ; Stores the power of A which is greater than N ; Increment count by 1 ; Divide N by A ; Function to find a pair ( a , b ) such that A ^ a + B ^ b = N ; Calculate the minimum power of A greater than N ; Calculate the minimum power of B greater than N ; Make copy of A and B ; Traverse for every pair ( i , j ) ; Check if B ^ j + A ^ i = N To overcome the overflow problem use B = N - A rather than B + A = N ; Increment power B by 1 ; Increment power A by 1 ; Finally print - 1 if no pair is found ; Given A , B and N ; Function Call"
How can the above be solved in JS?

function power ( A , N ) { let count = 0 ; if ( A == 1 ) return 0 ; while ( N > 0 ) { count ++ ; N /= A ; } return count ; } function Pairs ( N , A , B ) { let powerA , powerB ; powerA = power ( A , N ) ; powerB = power ( B , N ) ; let letialB = B , letialA = A ; A = 1 ; for ( let i = 0 ; i <= powerA ; i ++ ) { B = 1 ; for ( let j = 0 ; j <= powerB ; j ++ ) { if ( B == N - A ) { document . write ( i + " " + j ) ; return ; } B *= letialB ; } A *= letialA ; } document . write ( " " ) ; return ; } let N = 106 , A = 3 , B = 5 ; Pairs ( N , A , B ) ;


"Find a pair of numbers with set bit count as at most that of N and whose Bitwise XOR is N | Function to find the pair ( X , Y ) such that X xor Y = N and the count of set bits in X and Y is less than count of set bit in N ; Stores MSB ( Most Significant Bit ) ; Stores the value of X ; Stores the value of Y ; Traversing over all bits of N ; If ith bit of N is 0 ; Set ith bit of X to 1 ; Set ith bit of Y to 1 ; Print Answer ; Driver Code"
How can the above be solved in JS?

function maximizeProduct ( N ) { let MSB = Math . log2 ( N ) ; let X = 1 << MSB ; let Y = N - ( 1 << MSB ) ; for ( let i = 0 ; i < MSB ; i ++ ) { if ( ! ( N & ( 1 << i ) ) ) { X += 1 << i ; Y += 1 << i ; } } document . write ( X + " " + Y ) ; } let N = 45 ; maximizeProduct ( N ) ;


"Find a pair with the given difference | The function assumes that the array is sorted ; Initialize positions of two elements ; Search for a pair ; Driver program to test above function"
How can the above be solved in JS?

function findPair ( arr , size , n ) { let i = 0 ; let j = 1 ; while ( i < size && j < size ) { if ( i != j && arr [ j ] - arr [ i ] == n ) { document . write ( " " + arr [ i ] + " " + arr [ j ] + " " ) ; return true ; } else if ( arr [ j ] - arr [ i ] < n ) j ++ ; else i ++ ; } document . write ( " " ) ; return false ; } let arr = [ 1 , 8 , 30 , 40 , 100 ] ; let size = arr . length ; let n = 60 ; findPair ( arr , size , n ) ;


"Find a specific pair in Matrix | The function returns maximum value A ( c , d ) - A ( a , b ) over all choices of indexes such that both c > a and d > b . ; stores maximum value ; maxArr [ i ] [ j ] stores max of elements in matrix from ( i , j ) to ( N - 1 , N - 1 ) ; last element of maxArr will be same 's as of  the input matrix ; preprocess last row Initialize max ; preprocess last column Initialize max ; preprocess rest of the matrix from bottom ; Update maxValue ; set maxArr ( i , j ) ; Driver code"
How can the above be solved in JS?

function findMaxValue ( N , mat ) { let maxValue = Number . MIN_VALUE ; let maxArr = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { maxArr [ i ] = new Array ( N ) ; } maxArr [ N - 1 ] [ N - 1 ] = mat [ N - 1 ] [ N - 1 ] ; let maxv = mat [ N - 1 ] [ N - 1 ] ; for ( let j = N - 2 ; j >= 0 ; j -- ) { if ( mat [ N - 1 ] [ j ] > maxv ) maxv = mat [ N - 1 ] [ j ] ; maxArr [ N - 1 ] [ j ] = maxv ; } maxv = mat [ N - 1 ] [ N - 1 ] ; for ( let i = N - 2 ; i >= 0 ; i -- ) { if ( mat [ i ] [ N - 1 ] > maxv ) maxv = mat [ i ] [ N - 1 ] ; maxArr [ i ] [ N - 1 ] = maxv ; } for ( let i = N - 2 ; i >= 0 ; i -- ) { for ( let j = N - 2 ; j >= 0 ; j -- ) { if ( maxArr [ i + 1 ] [ j + 1 ] - mat [ i ] [ j ] > maxValue ) maxValue = maxArr [ i + 1 ] [ j + 1 ] - mat [ i ] [ j ] ; maxArr [ i ] [ j ] = Math . max ( mat [ i ] [ j ] , Math . max ( maxArr [ i ] [ j + 1 ] , maxArr [ i + 1 ] [ j ] ) ) ; } } return maxValue ; } let N = 5 ; let mat = [ [ 1 , 2 , - 1 , - 4 , - 20 ] , [ - 8 , - 3 , 4 , 2 , 1 ] , [ 3 , 8 , 6 , 1 , 3 ] , [ - 4 , - 1 , 1 , 7 , - 6 ] , [ 0 , - 4 , 10 , - 5 , 1 ] ] ; document . write ( " " + findMaxValue ( N , mat ) ) ;


"Find a specific pair in Matrix | The function returns maximum value A ( d , e ) - A ( a , b ) over all choices of indexes such that both d > a and e > b . ; stores maximum value ; Consider all possible pairs mat [ a ] [ b ] and mat1 [ d ] [ e ] ; Driver code"
How can the above be solved in JS?

function findMaxValue ( N , mat ) { let maxValue = Number . MIN_VALUE ; for ( let a = 0 ; a < N - 1 ; a ++ ) for ( let b = 0 ; b < N - 1 ; b ++ ) for ( let d = a + 1 ; d < N ; d ++ ) for ( let e = b + 1 ; e < N ; e ++ ) if ( maxValue < ( mat [ d ] [ e ] - mat [ a ] [ b ] ) ) maxValue = mat [ d ] [ e ] - mat [ a ] [ b ] ; return maxValue ; } let N = 5 ; let mat = [ [ 1 , 2 , - 1 , - 4 , - 20 ] , [ - 8 , - 3 , 4 , 2 , 1 ] , [ 3 , 8 , 6 , 1 , 3 ] , [ - 4 , - 1 , 1 , 7 , - 6 ] , [ 0 , - 4 , 10 , - 5 , 1 ] ] ; document . write ( " " + findMaxValue ( N , mat ) ) ;


"Find all Pairs possible from the given Array | Function to print all possible pairs from the array ; Nested loop for all possible pairs ; Driver code"
How can the above be solved in JS?

function printPairs ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < n ; j ++ ) { document . write ( " " + arr [ i ] + " " + arr [ j ] + " " + " " ) ; } } } var arr = [ 1 , 2 ] ; var n = arr . length ; printPairs ( arr , n ) ;


"Find all divisors of a natural number | Set 2 | function to print the divisors ; Driver program to test above function"
How can the above be solved in JS?

function printDivisors ( n ) { for ( var i = 1 ; i * i < n ; i ++ ) { if ( n % i == 0 ) document . write ( i + " " ) ; } for ( var i = Math . sqrt ( n ) ; i >= 1 ; i -- ) { if ( n % i == 0 ) document . write ( " " + n / i ) ; } } document . write ( " " ) ; printDivisors ( 100 ) ;


"Find all divisors of a natural number | Set 2 | function to print the divisors ; Vector to store half of the divisors ; check if divisors are equal ; push the second divisor in the vector ; The vector will be printed in reverse ; Driver code"
How can the above be solved in JS?

function printDivisors ( n ) { let v = [ ] ; let t = 0 ; for ( let i = 1 ; i <= parseInt ( Math . sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { if ( parseInt ( n / i ) == i ) document . write ( i + " " ) ; else { document . write ( i + " " ) ; v [ t ++ ] = parseInt ( n / i ) ; } } } for ( let i = v . length - 1 ; i >= 0 ; i -- ) { document . write ( v [ i ] + " " ) ; } } document . write ( " " ) ; printDivisors ( 100 ) ;


"Find all elements in array which have at | Javascript program to find all elements in array which have atleast two greater elements itself . ; If current element is smaller than first then update both first and second ; If arr [ i ] is in between first and second then update second ; Driver code"
How can the above be solved in JS?

function findElements ( arr , n ) { let first = Number . MIN_VALUE ; let second = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] < second ) document . write ( arr [ i ] + " " ) ; } let arr = [ 2 , - 6 , 3 , 5 , 1 ] ; let n = arr . length ; findElements ( arr , n ) ;


"Find all elements in array which have at | Simple Javascript program to find all elements in array which have at - least two greater elements itself . ; Pick elements one by one and count greater elements . If count is more than 2 , print that element . ; Driver code"
How can the above be solved in JS?

function findElements ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { let count = 0 ; for ( let j = 0 ; j < n ; j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ; if ( count >= 2 ) document . write ( arr [ i ] + " " ) ; } } let arr = [ 2 , - 6 , 3 , 5 , 1 ] ; let n = arr . length ; findElements ( arr , n ) ;


"Find all elements in array which have at | Sorting based Javascript program to find all elements in array which have atleast two greater elements itself . ; Driver code"
How can the above be solved in JS?

function findElements ( arr , n ) { arr . sort ( ) ; for ( let i = 0 ; i < n - 2 ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 2 , - 6 , 3 , 5 , 1 ] ; let n = arr . length ; findElements ( arr , n ) ;


"Find all factors of a natural number | Set 1 | Function to print the divisors ; Note that this loop runs till square root ; If divisors are equal , print only one ; Otherwise print both ; Driver code"
How can the above be solved in JS?

function printDivisors ( n ) { for ( let i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( parseInt ( n / i , 10 ) == i ) document . write ( i ) ; else document . write ( i + " " + parseInt ( n / i , 10 ) + " " ) ; } } } document . write ( " " ) ; printDivisors ( 100 ) ;


"Find all factors of a natural number | Set 1 | function to print the divisors ; Driver program to test above function"
How can the above be solved in JS?

function printDivisors ( n ) { for ( i = 1 ; i <= n ; i ++ ) if ( n % i == 0 ) document . write ( i + " " ) ; } document . write ( " " + " " ) ; printDivisors ( 100 ) ;


"Find all possible outcomes of a given expression | Utility function to evaluate a simple expression with one operator only . ; This function evaluates all possible values and returns a list of evaluated values . ; To store result ( all possible evaluations of given expression ' expr ' ) ; If there is only one character , it must be a digit ( or operand ) , return it . ; If there are only three characters , middle one must be operator and corner ones must be operand ; every i refers to an operator ; l refers to all the possible values in the left of operator ' expr [ i ] ' ; r refers to all the possible values in the right of operator ' expr [ i ] ' ; Take above evaluated all possible values in left side of ' i ' ; Take above evaluated all possible values in right side of ' i ' ; Calculate value for every pair and add the value to result . ; Driver program"
How can the above be solved in JS?

function eval ( a , op , b ) { if ( op == ' ' ) { return a + b ; } if ( op == ' ' ) { return a - b ; } if ( op == ' ' ) { return a * b ; } return Number . MAX_VALUE ; } function evaluateAll ( expr , low , high ) { let res = [ ] ; if ( low == high ) { res . push ( expr [ low ] - ' ' ) ; return res ; } if ( low == ( high - 2 ) ) { let num = eval ( expr [ low ] - ' ' , expr [ low + 1 ] , expr [ low + 2 ] - ' ' ) ; res . push ( num ) ; return res ; } for ( let i = low + 1 ; i <= high ; i += 2 ) { let l = evaluateAll ( expr , low , i - 1 ) ; let r = evaluateAll ( expr , i + 1 , high ) ; for ( let s1 = 0 ; s1 < l . length ; s1 ++ ) { for ( let s2 = 0 ; s2 < r . length ; s2 ++ ) { let val = eval ( l [ s1 ] , expr [ i ] , r [ s2 ] ) ; res . push ( val ) ; } } } return res ; } let expr = " " ; let len = expr . length ; let ans = evaluateAll ( expr , 0 , len - 1 ) ; for ( let i = 0 ; i < ans . length ; i ++ ) { document . write ( ans [ i ] + " " ) ; }


"Find and Count total factors of co | Function to return the count of numbers which are divisible by both A and B in the range [ 1 , N ] in constant time ; Compute the count of numbers divisible by A in the range [ 1 , N ] ; Compute the count of numbers divisible by B in the range [ 1 , N ] ; Adding the counts which are divisible by A and B ; The above value might contain repeated values which are divisible by both A and B . Therefore , the count of numbers which are divisible by both A and B are found ; The count computed above is subtracted to compute the final count ; Function to return the sum of numbers which are divisible by both A and B in the range [ 1 , N ] ; Set to store the numbers so that the numbers are not repeated ; For loop to find the numbers which are divisible by A and insert them into the set ; For loop to find the numbers which are divisible by A and insert them into the set ; For loop to iterate through the set and find the sum ; Driver code"
How can the above be solved in JS?

function countOfNum ( n , a , b ) { let cnt_of_a , cnt_of_b , cnt_of_ab , sum ; cnt_of_a = Math . floor ( n / a ) ; cnt_of_b = Math . floor ( n / b ) ; sum = cnt_of_b + cnt_of_a ; cnt_of_ab = Math . floor ( n / ( a * b ) ) ; sum = sum - cnt_of_ab ; return sum ; } function sumOfNum ( n , a , b ) { let i ; let sum = 0 ; let ans = new Set ( ) ; for ( i = a ; i <= n ; i = i + a ) { ans . add ( i ) ; } for ( i = b ; i <= n ; i = i + b ) { ans . add ( i ) ; } for ( let it of ans . values ( ) ) { sum = sum + it ; } return sum ; } let N = 88 ; let A = 11 ; let B = 8 ; let count = countOfNum ( N , A , B ) ; let sumofnum = sumOfNum ( N , A , B ) ; document . write ( sumofnum % count ) ;


"Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts | javascript program to find Bitwise OR of two equal halves of an array after performing K right circular shifts ; Array for storing the segment tree ; Function to build the segment tree ; Function to return the OR of elements in the range [ l , r ] ; Check for out of bound condition ; Find middle of the range ; Recurse for all the elements in array ; Function to find the OR sum ; Function to build the segment Tree ; Loop to handle q queries ; Effective number of right circular shifts ; OR of second half of the array [ n / 2 - i , n - 1 - i ] ; OR of first half of the array [ n - i , n - 1 ] OR [ 0 , n / 2 - 1 - i ] ; Prvar final answer to the query ; Driver Code"
How can the above be solved in JS?

const MAX = 100005 ; var seg = Array ( 4 * MAX ) . fill ( 0 ) ; function build ( node , l , r , a ) { if ( l == r ) seg [ node ] = a [ l ] ; else { var mid = parseInt ( ( l + r ) / 2 ) ; build ( 2 * node , l , mid , a ) ; build ( 2 * node + 1 , mid + 1 , r , a ) ; seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) ; } } function query ( node , l , r , start , end , a ) { if ( l > end r < start ) return 0 ; if ( start <= l && r <= end ) return seg [ node ] ; var mid = parseInt ( ( l + r ) / 2 ) ; return ( ( query ( 2 * node , l , mid , start , end , a ) ) | ( query ( 2 * node + 1 , mid + 1 , r , start , end , a ) ) ) ; } function orsum ( a , n , q , k ) { build ( 1 , 0 , n - 1 , a ) ; for ( j = 0 ; j < q ; j ++ ) { var i = k [ j ] % ( n / 2 ) ; var sec = query ( 1 , 0 , n - 1 , n / 2 - i , n - i - 1 , a ) ; var first = ( query ( 1 , 0 , n - 1 , 0 , n / 2 - 1 - i , a ) | query ( 1 , 0 , n - 1 , n - i , n - 1 , a ) ) ; var temp = sec + first ; document . write ( temp + " " ) ; } } var a = [ 7 , 44 , 19 , 86 , 65 , 39 , 75 , 101 ] ; var n = a . length ; var q = 2 ; var k = [ 4 , 2 ] ; orsum ( a , n , q , k ) ;


"Find average of two numbers using bit operation | Function to return the average of x and y using bit operations ; Calculate the average Floor value of ( x + y ) / 2 ; Driver code"
How can the above be solved in JS?

function getAverage ( x , y ) { var avg = ( x & y ) + ( ( x ^ y ) >> 1 ) ; return avg ; } var x = 10 , y = 9 ; document . write ( getAverage ( x , y ) ) ;


"Find extra element in the second array | Function to return the extra element in B [ ] ; To store the result ; Find the XOR of all the element of array A [ ] and array B [ ] ; Driver code"
How can the above be solved in JS?

function extraElement ( A , B , n ) { let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) ans ^= A [ i ] ; for ( let i = 0 ; i < n + 1 ; i ++ ) ans ^= B [ i ] ; return ans ; } let A = [ 10 , 15 , 5 ] ; let B = [ 10 , 100 , 15 , 5 ] ; let n = A . length ; document . write ( extraElement ( A , B , n ) ) ;


"Find four missing numbers in an array containing elements from 1 to N | Finds missing 4 numbers in O ( N ) time and O ( 1 ) auxiliary space . ; To keep track of 4 possible numbers greater than length of input array In Java , helper is automatically initialized as 0. ; Traverse the input array and mark visited elements either by marking them as negative in arr [ ] or in helper [ ] . ; If element is smaller than or equal to length , mark its presence in arr [ ] ; Mark presence in helper [ ] ; Print all those elements whose presence is not marked . ; Driver code"
How can the above be solved in JS?

function missing4 ( arr ) { let helper = [ ] ; for ( let i = 0 ; i < 4 ; i ++ ) { helper [ i ] = 0 ; } for ( let i = 0 ; i < arr . length ; i ++ ) { let temp = Math . abs ( arr [ i ] ) ; if ( temp <= arr . length ) arr [ temp - 1 ] = Math . floor ( arr [ temp - 1 ] * ( - 1 ) ) ; else if ( temp > arr . length ) { if ( temp % arr . length != 0 ) helper [ temp % arr . length - 1 ] = - 1 ; else helper [ ( temp % arr . length ) + arr . length - 1 ] = - 1 ; } } for ( let i = 0 ; i < arr . length ; i ++ ) if ( arr [ i ] > 0 ) document . write ( i + 1 + " " ) ; for ( let i = 0 ; i < helper . length ; i ++ ) if ( helper [ i ] >= 0 ) document . write ( arr . length + i + 1 + " " ) ; return ; } let arr = [ 1 , 7 , 3 , 12 , 5 , 10 , 8 , 4 , 9 ] ; missing4 ( arr ) ;


"Find if a point lies inside a Circle | Javascript program to check if a point lies inside a circle or not ; Compare radius of circle with distance of its center from given point ; Driver code"
How can the above be solved in JS?

function isInside ( circle_x , circle_y , rad , x , y ) { if ( ( x - circle_x ) * ( x - circle_x ) + ( y - circle_y ) * ( y - circle_y ) <= rad * rad ) return true ; else return false ; } var x = 1 ; var y = 1 ; var circle_x = 0 ; var circle_y = 1 ; var rad = 2 ; if ( isInside ( circle_x , circle_y , rad , x , y ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Find k closest elements to a given value | Function to find the cross over point ( the point before which elements aresmaller than or equal to x and afterwhich greater than x ) ; Base cases x is greater than all ; x is smaller than all ; Find the middle point ; If x is same as middle element , then return mid ; If x is greater than arr [ mid ] , then either arr [ mid + 1 ] is ceiling of x or ceiling lies in arr [ mid + 1. . . high ] ; This function prints k closest elements to x in arr [ ] . n is the number of elements in arr [ ] ; Find the crossover point ; Right index to search ; To keep track of count of elements already printed ; If x is present in arr [ ] , then reduce left index . Assumption : all elements in arr [ ] are distinct ; Compare elements on left and right of crossover point to find the k closest elements ; If there are no more elements on right side , then print left elements ; If there are no more elements on left side , then print right elements ; Driver Code"
How can the above be solved in JS?

function findCrossOver ( arr , low , high , x ) { if ( arr [ high ] <= x ) return high if ( arr [ low ] > x ) return low var mid = ( low + high ) / 2 if ( arr [ mid ] <= x && arr [ mid + 1 ] > x ) return mid if ( arr [ mid ] < x ) return findCrossOver ( arr , mid + 1 , high , x ) return findCrossOver ( arr , low , mid - 1 , x ) } function printKclosest ( arr , x , k , n ) { var l = findCrossOver ( arr , 0 , n - 1 , x ) var r = l + 1 var count = 0 if ( arr [ l ] == x ) l -= 1 while ( l >= 0 && r < n && count < k ) { if ( x - arr [ l ] < arr [ r ] - x ) { document . write ( arr [ l ] + " " ) l -= 1 } else { document . write ( arr [ r ] + " " ) r += 1 } count += 1 } while ( count < k && l >= 0 ) { print ( arr [ l ] ) l -= 1 count += 1 } while ( count < k && r < n ) { print ( arr [ r ] ) r += 1 count += 1 } } var arr = [ 12 , 16 , 22 , 30 , 35 , 39 , 42 , 45 , 48 , 50 , 53 , 55 , 56 ] var n = arr . length var x = 35 var k = 4 printKclosest ( arr , x , 4 , n )


"Find k maximum elements of array in original order | Function to print m Maximum elements ; vector to store the copy of the original array ; Sorting the vector in descending order . Please refer below link for details www . geeksforgeeks . org / sort - c - stl / https : ; Traversing through original array and printing all those elements that are in first k of sorted vector . goo . gl / 44 Rwgt Please refer https : for details of binary_search ( ) ; Driver code"
How can the above be solved in JS?

function printMax ( arr , k , n ) { var brr = arr . slice ( ) ; brr . sort ( ( a , b ) => b - a ) ; for ( var i = 0 ; i < n ; ++ i ) if ( brr . indexOf ( arr [ i ] ) < k ) document . write ( arr [ i ] + " " ) ; } var arr = [ 50 , 8 , 45 , 12 , 25 , 40 , 84 ] ; var n = arr . length ; var k = 3 ; printMax ( arr , k , n ) ;


"Find largest d in array such that a + b + c = d | To store and retrieve indices pair i & j ; The function finds four elements with given sum X ; Store sums ( a + b ) of all pairs ( a , b ) in a hash table ; Traverse through all pairs and find ( d - c ) is present in hash table ; If d - c is present in hash table , ; Making sure that all elements are distinct array elements and an element is not considered more than once . ; Driver code"
How can the above be solved in JS?

class Indexes { constructor ( i , j ) { this . i = i ; this . j = j ; } getI ( ) { return this . i ; } getJ ( ) { return this . j ; } } function findFourElements ( arr , n ) { let map = new Map ( ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { map . set ( arr [ i ] + arr [ j ] , new Indexes ( i , j ) ) ; } } let d = Number . MIN_VALUE ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { let abs_diff = Math . abs ( arr [ i ] - arr [ j ] ) ; if ( map . has ( abs_diff ) ) { let indexes = map . get ( abs_diff ) ; if ( indexes . getI ( ) != i && indexes . getI ( ) != j && indexes . getJ ( ) != i && indexes . getJ ( ) != j ) { d = Math . max ( d , Math . max ( arr [ i ] , arr [ j ] ) ) ; } } } } return d ; } let arr = [ 2 , 3 , 5 , 7 , 12 ] ; let n = arr . length ; let res = findFourElements ( arr , n ) ; if ( res == Number . MIN_VALUE ) document . write ( " " ) ; else document . write ( res ) ;


"Find largest d in array such that a + b + c = d | function to find largest d ; sort the array in ascending order ; iterating from backwards to find the required largest d ; since all four a , b , c , d should be distinct ; if the current combination of j , k , l in the set is equal to S [ i ] return this value as this would be the largest dsince we are iterating in descending order ; Driver Code"
How can the above be solved in JS?

function findLargestd ( S , n ) { let found = false ; S . sort ( ) ; for ( let i = n - 1 ; i >= 0 ; i -- ) { for ( let j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; for ( let k = j + 1 ; k < n ; k ++ ) { if ( i == k ) continue ; for ( let l = k + 1 ; l < n ; l ++ ) { if ( i == l ) continue ; if ( S [ i ] == S [ j ] + S [ k ] + S [ l ] ) { found = true ; return S [ i ] ; } } } } } if ( found == false ) return Number . MAX_VALUE ; return - 1 ; } let S = [ 2 , 3 , 5 , 7 , 12 ] ; let n = S . length ; let ans = findLargestd ( S , n ) ; if ( ans == Number . MAX_VALUE ) document . write ( " " ) ; else document . write ( " " + " " + ans ) ;


"Find length of longest Fibonacci like subsequence | Function to return the max Length of Fibonacci subsequence ; Store all array elements in a hash table ; check until next fib element is found ; next element of fib subseq ; Driver program"
How can the above be solved in JS?

function LongestFibSubseq ( A , n ) { var S = new Set ( A ) ; var maxLen = 0 , x , y ; for ( var i = 0 ; i < n ; ++ i ) { for ( var j = i + 1 ; j < n ; ++ j ) { x = A [ j ] ; y = A [ i ] + A [ j ] ; var length = 2 ; while ( S . has ( y ) ) { var z = x + y ; x = y ; y = z ; maxLen = Math . max ( maxLen , ++ length ) ; } } } return maxLen >= 3 ? maxLen : 0 ; } var A = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ; var n = A . length ; document . write ( LongestFibSubseq ( A , n ) ) ;


"Find max of two Rational numbers | JAVASCRIPT program to find max between two Rational numbers ; Get lcm of two number 's ; Get max rational number ; Find the LCM of first . denominator and sec . denominator ; Declare nume1 and nume2 for get the value of first numerator and second numerator ; Driver Code"
How can the above be solved in JS?

class Rational { constructor ( nume , deno ) { this . nume = nume ; this . deno = deno ; } } function lcm ( a , b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } function maxRational ( first , sec ) { let k = lcm ( first . deno , sec . deno ) ; let nume1 = first . nume ; let nume2 = sec . nume ; nume1 *= k / ( first . deno ) ; nume2 *= k / ( sec . deno ) ; return ( nume2 < nume1 ) ? first : sec ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } let first = new Rational ( 3 , 2 ) ; let sec = new Rational ( 3 , 4 ) ; let res = maxRational ( first , sec ) ; document . write ( res . nume + " " + res . deno ) ;


"Find maximum average subarray of k length | Returns beginning index of maximum average subarray of length ' k ' ; Check if ' k ' is valid ; Compute sum of first ' k ' elements ; Compute sum of remaining subarrays ; Return starting index ; Driver program"
How can the above be solved in JS?

function findMaxAverage ( arr , n , k ) { if ( k > n ) return - 1 ; let sum = arr [ 0 ] ; for ( let i = 1 ; i < k ; i ++ ) sum += arr [ i ] ; let max_sum = sum ; let max_end = k - 1 ; for ( let i = k ; i < n ; i ++ ) { sum = sum + arr [ i ] - arr [ i - k ] ; if ( sum > max_sum ) { max_sum = sum ; max_end = i ; } } return max_end - k + 1 ; } let arr = [ 1 , 12 , - 5 , - 6 , 50 , 3 ] ; let k = 4 ; let n = arr . length ; document . write ( " " + " " + k + " " + findMaxAverage ( arr , n , k ) ) ;


"Find maximum average subarray of k length | Returns beginning index of maximum average subarray of length ' k ' ; Check if ' k ' is valid ; Create and fill array to store cumulative sum . csum [ i ] stores sum of arr [ 0 ] to arr [ i ] ; Initialize max_sm as sum of first subarray ; Find sum of other subarrays and update max_sum if required . ; Return starting index ; Driver code"
How can the above be solved in JS?

function findMaxAverage ( arr , n , k ) { if ( k > n ) return - 1 ; let csum = new Array ( n ) ; csum [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) csum [ i ] = csum [ i - 1 ] + arr [ i ] ; let max_sum = csum [ k - 1 ] , max_end = k - 1 ; for ( let i = k ; i < n ; i ++ ) { let curr_sum = csum [ i ] - csum [ i - k ] ; if ( curr_sum > max_sum ) { max_sum = curr_sum ; max_end = i ; } } return max_end - k + 1 ; } let arr = [ 1 , 12 , - 5 , - 6 , 50 , 3 ] ; let k = 4 ; let n = arr . length ; document . write ( " " + " " + k + " " + findMaxAverage ( arr , n , k ) ) ;


"Find maximum matching in a given Binary Tree | Javascript Program to implement the above approach ; Adjacency list to store edges ; Add an edge between U and V in tree ; Edge from u to v ; Edge from V to U ; Function that finds the maximum matching of the DFS ; Go further as we are not allowed to go towards its parent ; If U and its parent P is not taken then we must take & mark them as taken ; Increment size of edge set ; Function to find the maximum matching in a graph ; Taking 1 as a root of the tree ; Print maximum Matching ; Driver Code ; Joining edge between two nodes in tree ; Function call"
How can the above be solved in JS?

let N = 10000 ; let adj = new Array ( N ) ; let used = new Array ( N ) ; used . fill ( 0 ) ; let max_matching = 0 ; function AddEdge ( u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } function Matching_dfs ( u , p ) { for ( let i = 0 ; i < adj [ u ] . length ; i ++ ) { if ( adj [ u ] [ i ] != p ) { Matching_dfs ( adj [ u ] [ i ] , u ) ; } } if ( used [ u ] == 0 && used [ p ] == 0 && p != 0 ) { max_matching ++ ; used [ u ] = used [ p ] = 1 ; } } function maxMatching ( ) { Matching_dfs ( 1 , 0 ) ; document . write ( max_matching + " " ) ; } for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; AddEdge ( 1 , 2 ) ; AddEdge ( 1 , 3 ) ; AddEdge ( 3 , 4 ) ; AddEdge ( 3 , 5 ) ; maxMatching ( ) ;


"Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum | Function to find the maximum subset sum ; Ignore the current element ; Including element in partition 1 ; Including element in partition 2 ; Size of the array"
How can the above be solved in JS?

function maxSum ( p0 , p1 , a , pos , n ) { if ( pos == n ) { if ( p0 == p1 ) return p0 ; else return 0 ; } var ans = maxSum ( p0 , p1 , a , pos + 1 , n ) ; ans = Math . max ( ans , maxSum ( p0 + a [ pos ] , p1 , a , pos + 1 , n ) ) ; ans = Math . max ( ans , maxSum ( p0 , p1 + a [ pos ] , a , pos + 1 , n ) ) ; return ans ; } var n = 4 ; var a = [ 1 , 2 , 3 , 6 ] ; document . write ( maxSum ( 0 , 0 , a , 0 , n ) ) ;


"Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum | Function to find the maximum subset sum ; sum of all elements ; bottom up lookup table ; ; initialising dp table with - 1000000000 where , - 1000000000 means no solution ; Case when diff is 0 ; Putting ith element in g0 ; Putting ith element in g1 ; Ignoring ith element ; Driver code"
How can the above be solved in JS?


"Find minimum adjustment cost of an array | Javascript program to find minimum adjustment cost of an array ; Function to find minimum adjustment cost of an array ; dp [ i ] [ j ] stores minimal adjustment cost on changing A [ i ] to j ; handle first element of array separately ; do for rest elements of the array ; replace A [ i ] to j and calculate minimal adjustment cost dp [ i ] [ j ] ; initialize minimal adjustment cost to INT_MAX ; consider all k such that k >= max ( j - target , 0 ) and k <= min ( M , j + target ) and take minimum ; return minimum value from last row of dp table ; Driver Code"
How can the above be solved in JS?

let M = 100 ; function minAdjustmentCost ( A , n , target ) { let dp = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { dp [ i ] = new Array ( n ) ; for ( let j = 0 ; j <= M ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let j = 0 ; j <= M ; j ++ ) dp [ 0 ] [ j ] = Math . abs ( j - A [ 0 ] ) ; for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 0 ; j <= M ; j ++ ) { dp [ i ] [ j ] = Number . MAX_VALUE ; let k = Math . max ( j - target , 0 ) ; for ( ; k <= Math . min ( M , j + target ) ; k ++ ) dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ i - 1 ] [ k ] + Math . abs ( A [ i ] - j ) ) ; } } let res = Number . MAX_VALUE ; for ( let j = 0 ; j <= M ; j ++ ) res = Math . min ( res , dp [ n - 1 ] [ j ] ) ; return res ; } let arr = [ 55 , 77 , 52 , 61 , 39 , 6 , 25 , 60 , 49 , 47 ] ; let n = arr . length ; let target = 10 ; document . write ( " " + minAdjustmentCost ( arr , n , target ) ) ;


"Find minimum changes required in an array for it to contain k distinct elements | Javascript program to minimum changes required in an array for k distinct elements . ; Function to minimum changes required in an array for k distinct elements . ; Store the frequency of each element ; Store the frequency of elements ; Sort frequencies in descending order ; To store the required answer ; Return the required answer ; Driver code"
How can the above be solved in JS?

var MAX = 100005 ; function Min_Replace ( arr , n , k ) { arr . sort ( ( a , b ) => a - b ) var freq = Array ( MAX ) . fill ( 0 ) ; var p = 0 ; freq [ p ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) ++ freq [ p ] ; else ++ freq [ ++ p ] ; } freq . sort ( ( a , b ) => b - a ) ; var ans = 0 ; for ( var i = k ; i <= p ; i ++ ) ans += freq [ i ] ; return ans ; } var arr = [ 1 , 2 , 7 , 8 , 2 , 3 , 2 , 3 ] ; var n = arr . length ; var k = 2 ; document . write ( Min_Replace ( arr , n , k ) ) ;


"Find minimum possible values of A , B and C when two of the ( A + B ) , ( A + C ) and ( B + C ) are given | Function to find A , B and C ; Keep minimum number in x ; Find the numbers ; Driver code ; Function call"
How can the above be solved in JS?

function MinimumValue ( x , y ) { if ( x > y ) { var temp = x ; x = y ; y = temp ; } var a = 1 ; var b = x - 1 ; var c = y - b ; document . write ( a + " " + b + " " + c ) ; } var x = 123 , y = 13 ; MinimumValue ( x , y ) ;


"Find most significant set bit of a number | Javascript program to find MSB number for given n . ; Driver code"
How can the above be solved in JS?

function setBitNumber ( n ) { if ( n == 0 ) return 0 ; let msb = 0 ; n = n / 2 ; while ( n != 0 ) { n = $n / 2 ; msb ++ ; } return ( 1 << msb ) ; } let n = 0 ; document . write ( setBitNumber ( n ) ) ;


"Find most significant set bit of a number | Javascript program to find MSB number for given n . ; Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001 ; This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111 ; Increment n by 1 so that there is only one set bit which is just before original MSB . n now becomes 1000000000 ; Return original MSB after shifting . n now becomes 100000000 ; Driver code"
How can the above be solved in JS?

function setBitNumber ( n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n = n + 1 ; return ( n >> 1 ) ; } let n = 273 ; document . write ( setBitNumber ( n ) ) ;


"Find most significant set bit of a number | Javascript program to find MSB number for given n . ; To find the position of the most significant set bit ; To return the the value of the number with set bit at k - th position ; Driver code"
How can the above be solved in JS?

function setBitNumber ( n ) { let k = parseInt ( Math . log ( n ) / Math . log ( 2 ) , 10 ) ; return 1 << k ; } let n = 273 ; document . write ( setBitNumber ( n ) ) ;