"Add elements in start to sort the array | Variation of Stalin Sort | Function to sort the array ; Iterator < Integer > index = arr . iterator ( ) ; ; Driver Code ; Function call"
How can the above be solved in JS?

function variationStalinsort ( arr ) { let j = 0 ; while ( true ) { let moved = 0 ; for ( let i = 0 ; i < ( arr . length - 1 - j ) ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { let index ; let temp ; index = arr [ i ] ; temp = arr [ i + 1 ] ; arr . splice ( i , 1 ) ; arr . splice ( i , 0 , temp ) ; arr [ i ] = temp ; arr . splice ( i + 1 , 1 ) ; arr . splice ( i + 1 , 0 , index ) arr [ i + 1 ] = index ; moved ++ ; } } j ++ ; if ( moved == 0 ) { break ; } } document . write ( " " + arr + " " ) ; } let arr = [ 2 , 1 , 4 , 3 , 6 , 5 , 8 , 7 , 10 , 9 ] ; let arr1 = [ ] ; for ( let i = 0 ; i < arr . length ; i ++ ) arr1 . push ( arr [ i ] ) ; variationStalinsort ( arr1 ) ;


"All about Bit Manipulation | Function to clear the ith bit of the given number N ; Create the mask for the ith bit unset ; Return the update value"
How can the above be solved in JS?

function clearBit ( num , i ) { let mask = ~ ( 1 << i ) ; return num & mask ; }


"All about Bit Manipulation | Function to get the bit at the ith position ; Return true if the bit is set . Otherwise return false"
How can the above be solved in JS?

function getBit ( num , i ) { return ( ( num & ( 1 << i ) ) != 0 ) ; }


"Alternate Odd and Even Nodes in a Singly Linked List | Structure node ; A utility function to print linked list ; Function to create newNode in a linkedlist ; Function to insert at beginning ; Function to rearrange the odd and even nodes ; Step 1 : Segregate even and odd nodes Step 2 : Split odd and even lists Step 3 : Merge even list into odd list ; Step 1 : Segregate Odd and Even Nodes ; Backup next pointer of temp ; If temp is odd move the node to beginning of list ; Advance Temp Pointer ; Step 2 Split the List into Odd and even ; End the odd List ( Make last node null ) ; Step 3 : Merge Even List into odd ; While both lists are not exhausted Backup next pointers of i and j ; ptr points to the latest node added ; Advance i and j pointers ; Odd list exhausts before even , append remainder of even list to odd . ; The case where even list exhausts before odd list is automatically handled since we merge the even list into the odd list ; Driver Code"
How can the above be solved in JS?

class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function printList ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . next ; } document . write ( " " ) ; } function newNode ( key ) { var temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } function insertBeg ( head , val ) { var temp = newNode ( val ) ; temp . next = head ; head = temp ; return head ; } function rearrange ( head ) { var even ; var temp , prev_temp ; var i , j , k , l , ptr = null ; temp = ( head ) . next ; prev_temp = head ; while ( temp != null ) { var x = temp . next ; if ( temp . data % 2 != 0 ) { prev_temp . next = x ; temp . next = ( head ) ; ( head ) = temp ; } else { prev_temp = temp ; } temp = x ; } temp = ( head ) . next ; prev_temp = ( head ) ; while ( temp != null && temp . data % 2 != 0 ) { prev_temp = temp ; temp = temp . next ; } even = temp ; prev_temp . next = null ; i = head ; j = even ; while ( j != null && i != null ) { k = i . next ; l = j . next ; i . next = j ; j . next = k ; ptr = j ; i = k ; j = l ; } if ( i == null ) { ptr . next = j ; } return head ; } var head = newNode ( 8 ) ; head = insertBeg ( head , 7 ) ; head = insertBeg ( head , 6 ) ; head = insertBeg ( head , 3 ) ; head = insertBeg ( head , 5 ) ; head = insertBeg ( head , 1 ) ; head = insertBeg ( head , 2 ) ; head = insertBeg ( head , 10 ) ; document . write ( " " ) ; printList ( head ) ; document . write ( " " ) ; head = rearrange ( head ) ; printList ( head ) ;


"Alternate Odd and Even Nodes in a Singly Linked List | class node ; A utility function to print linked list ; Function to create newNode in a linkedlist ; Function to insert at beginning ; Function to rearrange the odd and even nodes ; Odd Value in Even Position Add pointer to current node in odd stack ; Even Value in Odd Position Add pointer to current node in even stack ; Swap Data at the top of two stacks ; Driver code"
How can the above be solved in JS?

class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function printList ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . next ; } document . write ( ) ; } function newNode ( key ) { var temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } function insertBeg ( head , val ) { var temp = newNode ( val ) ; temp . next = head ; head = temp ; return head ; } function rearrangeOddEven ( head ) { var odd = [ ] ; var even = [ ] ; var i = 1 ; while ( head != null ) { if ( head . data % 2 != 0 && i % 2 == 0 ) { odd . push ( head ) ; } else if ( head . data % 2 == 0 && i % 2 != 0 ) { even . push ( head ) ; } head = head . next ; i ++ ; } while ( odd . length > 0 && even . length > 0 ) { var k = odd [ odd . length - 1 ] . data ; odd [ odd . length - 1 ] . data = even [ even . length - 1 ] . data ; even [ even . length - 1 ] . data = k ; odd . pop ( ) ; even . pop ( ) ; } } var head = newNode ( 8 ) ; head = insertBeg ( head , 7 ) ; head = insertBeg ( head , 6 ) ; head = insertBeg ( head , 5 ) ; head = insertBeg ( head , 3 ) ; head = insertBeg ( head , 2 ) ; head = insertBeg ( head , 1 ) ; document . write ( " " ) ; printList ( head ) ; rearrangeOddEven ( head ) ; document . write ( " " + " " ) ; printList ( head ) ;


"An efficient way to check whether n | A simple Javascript program to check if n - th Fibonacci number is multiple of 10. ; Returns true if n - th Fibonacci number is multiple of 10. ; Driver code"
How can the above be solved in JS?

function fibonacci ( n ) { let a = 0 ; let b = 1 ; let c ; if ( n <= 1 ) return n ; for ( let i = 2 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return c ; } function isMultipleOf10 ( n ) { let f = fibonacci ( 30 ) ; return ( f % 10 == 0 ) ; } let n = 30 ; if ( isMultipleOf10 ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;


"An efficient way to check whether n | Returns true if n - th Fibonacci number is multiple of 10. ; Driver Code"
How can the above be solved in JS?

function isMultipleOf10 ( n ) { return ( n % 15 == 0 ) ; } let n = 30 ; if ( isMultipleOf10 ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Area of a Circumscribed Circle of a Square | Utiity function ; Driver code"
How can the above be solved in JS?

function areacircumscribed ( a ) { return ( a * a * ( 3.1415 / 2 ) ) ; } let a = 6 ; document . write ( " " , areacircumscribed ( a ) ) ;


"Area of a circle inscribed in a rectangle which is inscribed in a semicircle | Function to find the area of the circle ; radius cannot be negative ; area of the circle ; Driver code"
How can the above be solved in JS?

function area ( r ) { if ( r < 0 ) return - 1 ; var area = ( 3.14 * Math . pow ( r / ( 2 * Math . sqrt ( 2 ) ) , 2 ) ) ; return area ; } var a = 5 ; document . write ( area ( a ) . toFixed ( 6 ) ) ;


"Area of triangle formed by the axes of co | Function to find area ; Driver code"
How can the above be solved in JS?

function area ( a , b , c ) { var d = Math . abs ( ( c * c ) / ( 2 * a * b ) ) ; return d ; } var a = - 2 , b = 4 , c = 3 ; document . write ( area ( a , b , c ) ) ;


"Array Range Queries to find the Maximum Armstrong number with updates | A utility function to get the middle index of given range . ; Function that return true if num is armstrong else return false ; A recursive function to get the sum of values in the given range of the array . The following are parameters for this function . st . Pointer to segment tree node . Index of current node in the segment tree . ss & se . Starting and ending indexes of the segment represented by current node , i . e . , st [ node ] l & r . Starting and ending indexes of range query ; If segment of this node is completely part of given range , then return the max of segment . ; If segment of this node does not belong to given range ; If segment of this node is partially the part of given range ; A recursive function to update the nodes which have the given the index in their range . The following are parameters st , ss and se are same as defined above index . index of the element to be updated . ; update value in array and in segment tree ; Return max of elements in range from index l ( query start ) to r ( query end ) . ; Check for erroneous input values ; A recursive function that constructs Segment Tree for array [ ss . . se ] . si is index of current node in segment tree st ; If there is one element in array , store it in current node of segment tree and return ; If there are more than one elements , then recur for left and right subtrees and store the max of values in this node ; Function to cona segment tree from given array . This function allocates memory for segment tree . ; Height of segment tree ; Maximum size of segment tree ; Allocate memory ; Fill the allocated memory st ; Return the constructed segment tree ; Driver code ; Build segment tree from given array ; Print max of values in array from index 1 to 3 ; Update : set arr [ 1 ] = 153 and update corresponding segment tree nodes . ; Find max after the value is updated"
How can the above be solved in JS?

function getMid ( s , e ) { return s + Math . floor ( ( e - s ) / 2 ) ; } function isArmstrong ( x ) { let n = ( x ) . toString ( ) . length ; let sum1 = 0 ; let temp = x ; while ( temp > 0 ) { let digit = temp % 10 ; sum1 += Math . pow ( digit , n ) ; temp = Math . floor ( temp / 10 ) ; } if ( sum1 == x ) return true ; return false ; } function MaxUtil ( st , ss , se , l , r , node ) { if ( l <= ss && r >= se ) return st [ node ] ; if ( se < l ss > r ) return - 1 ; let mid = getMid ( ss , se ) ; return Math . max ( MaxUtil ( st , ss , mid , l , r , 2 * node ) , MaxUtil ( st , mid + 1 , se , l , r , 2 * node + 1 ) ) ; } function updateValue ( arr , st , ss , se , index , value , node ) { if ( index < ss index > se ) { document . write ( " " + " " ) ; return ; } if ( ss == se ) { arr [ index ] = value ; if ( isArmstrong ( value ) ) st [ node ] = value ; else st [ node ] = - 1 ; } else { let mid = getMid ( ss , se ) ; if ( index >= ss && index <= mid ) updateValue ( arr , st , ss , mid , index , value , 2 * node ) ; else updateValue ( arr , st , mid + 1 , se , index , value , 2 * node + 1 ) ; st [ node ] = Math . max ( st [ 2 * node + 1 ] , st [ 2 * node + 2 ] ) ; } return ; } function getMax ( st , n , l , r ) { if ( l < 0 r > n - 1 l > r ) { document . write ( " " ) ; return - 1 ; } return MaxUtil ( st , 0 , n - 1 , l , r , 0 ) ; } function constructSTUtil ( arr , ss , se , st , si ) { if ( ss == se ) { if ( isArmstrong ( arr [ ss ] ) ) st [ si ] = arr [ ss ] ; else st [ si ] = - 1 ; return st [ si ] ; } let mid = getMid ( ss , se ) ; st [ si ] = Math . max ( constructSTUtil ( arr , ss , mid , st , si * 2 ) , constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 1 ) ) ; return st [ si ] ; } function constructST ( arr , n ) { let x = ( Math . ceil ( Math . log ( n ) ) ) ; let max_size = 2 * Math . pow ( 2 , x ) - 1 ; let st = new Array ( max_size ) ; constructSTUtil ( arr , 0 , n - 1 , st , 0 ) ; return st ; } let arr = [ 192 , 113 , 535 , 7 , 19 , 111 ] ; let n = arr . length ; let st = constructST ( arr , n )...


"Average of a stream of numbers | Returns the new average after including x ; Prints average of a stream of numbers ; Driver program to test above functions"
How can the above be solved in JS?

function getAvg ( prev_avg , x , n ) { return ( prev_avg * n + x ) / ( n + 1 ) ; } function streamAvg ( arr , n ) { let avg = 0 ; for ( let i = 0 ; i < n ; i ++ ) { avg = getAvg ( avg , arr [ i ] , i ) ; document . write ( " " + ( i + 1 ) + " " + avg . toFixed ( 6 ) + " " ) ; } return ; } let arr = [ 10 , 20 , 30 , 40 , 50 , 60 ] ; let n = arr . length ; streamAvg ( arr , n ) ;


"Average of a stream of numbers | javascript program to return Average of a stream of numbers ; Returns the new average after including x ; Prints average of a stream of numbers ; Driver Code"
How can the above be solved in JS?

var sum = 0 , n = 0 ; function getAvg ( x ) { sum += x ; n ++ ; return ( sum / n ) ; } function streamAvg ( arr , m ) { var avg = 0 ; for ( i = 0 ; i < m ; i ++ ) { avg = getAvg ( parseInt ( arr [ i ] ) ) ; document . write ( " " + ( i + 1 ) + " " + avg . toFixed ( 1 ) + " " ) ; } return ; } var arr = [ 10 , 20 , 30 , 40 , 50 , 60 ] ; var m = arr . length ; streamAvg ( arr , m ) ;


"Babylonian method for square root | Returns the square root of n . Note that the function ; We are using n itself as initial approximation This can definitely be improved ; e decides the accuracy level ; Driver program to test above function"
How can the above be solved in JS?

function squareRoot ( n ) { let x = n ; let y = 1 ; let e = 0.000001 ; while ( x - y > e ) { x = ( x + y ) / 2 ; y = n / x ; } return x ; } let n = 50 ; document . write ( " " + n + " " + squareRoot ( n ) . toFixed ( 6 ) ) ;


"Bessel 's Interpolation | Calculating u mentioned in the formula ; Calculating factorial of given number n ; Number of values given ; y is used for difference table with y [ 0 ] used for input ; Calculating the central difference table ; Displaying the central difference table ; Value to interpolate at ; Initializing u and sum ; k is origin thats is f ( 0 ) ; Origin for odd ; Origin for even ; Solving using bessel 's formula"
How can the above be solved in JS?


"Biggest integer which has maximum digit sum in range from 1 to n | Function to calculate the sum of digits of a number . ; Returns the maximum number with maximum sum of digits . ; Initializing b as 1 and initial max sum to be of n ; Iterates from right to left in a digit ; While iterating this is the number from from right to left ; Calls the function to check if sum of cur is more then of ans ; Reduces the number to one unit less ; Driver Code"
How can the above be solved in JS?

function sumOfDigits ( a ) { var sum = 0 ; while ( a != 0 ) { sum += a % 10 ; a = parseInt ( a / 10 ) ; } return sum ; } function findMax ( x ) { var b = 1 , ans = x ; while ( x != 0 ) { var cur = ( x - 1 ) * b + ( b - 1 ) ; if ( sumOfDigits ( cur ) > sumOfDigits ( ans ) || ( sumOfDigits ( cur ) == sumOfDigits ( ans ) && cur > ans ) ) ans = cur ; x = parseInt ( x / 10 ) ; b *= 10 ; } return ans ; } var n = 521 ; document . write ( findMax ( n ) ) ;


"Binomial Coefficient | DP | Function to find binomial coefficient ; Getting the modular inversion for all the numbers from 2 to r with respect to m here m = 1000000007 ; for 1 / ( r ! ) part ; for ( n ) * ( n - 1 ) * ( n - 2 ) * ... * ( n - r + 1 ) part ; Driver code"
How can the above be solved in JS?

function binomialCoeff ( n , r ) { if ( r > n ) return 0 ; let m = 1000000007 ; let inv = new Array ( r + 1 ) . fill ( 0 ) ; inv [ 0 ] = 1 ; if ( r + 1 >= 2 ) inv [ 1 ] = 1 ; for ( let i = 2 ; i <= r ; i ++ ) { inv [ i ] = m - Math . floor ( m / i ) * inv [ m % i ] % m ; } let ans = 1 ; for ( let i = 2 ; i <= r ; i ++ ) { ans = ( ( ans % m ) * ( inv [ i ] % m ) ) % m ; } for ( let i = n ; i >= ( n - r + 1 ) ; i -- ) { ans = ( ( ans % m ) * ( i % m ) ) % m ; } return ans ; } let n = 5 , r = 2 ; document . write ( " " + n + " " + r + " " + binomialCoeff ( n , r ) + " " ) ;


"Binomial Coefficient | DP | Function to find gcd of two numbers in O ( log ( min ( a , b ) ) ) ; Base case ; C ( n , r ) = C ( n , n - r ) better time complexity for lesser r value ; Array of elements from n - r + 1 to n ; For numbers from 1 to r find arr [ j ] such that gcd ( i , arr [ j ] ) > 1 ; If gcd > 1 , divide both by gcd ; If i becomes 1 , no need to search arr ; Single pass to multiply the numerator ; Driver code"
How can the above be solved in JS?

function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function nCr ( n , r ) { if ( r > n ) return 0 ; if ( r > n - r ) r = n - r ; mod = 1000000007 ; var arr = new Array ( r ) ; for ( var i = n - r + 1 ; i <= n ; i ++ ) { arr [ i + r - n - 1 ] = i ; } var ans = 1 ; for ( var k = 1 ; k < r + 1 ; k ++ ) { var j = 0 , i = k ; while ( j < arr . length ) { var x = gcd ( i , arr [ j ] ) ; if ( x > 1 ) { arr [ j ] /= x ; i /= x ; } if ( i == 1 ) break ; j += 1 ; } } arr . forEach ( function ( i ) { ans = ( ans * i ) % mod ; } ) ; return ans ; } var n = 5 , r = 2 ; document . write ( " " + n + " " + r + " " + nCr ( n , r ) + " " ) ;


"Binomial Coefficient | DP | Javascript program for space optimized Dynamic Programming Solution of Binomial Coefficient ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Driver code"
How can the above be solved in JS?

function binomialCoeff ( n , k ) { let C = new Array ( k + 1 ) ; C . fill ( 0 ) ; C [ 0 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) { for ( let j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } let n = 5 , k = 2 ; document . write ( " " + n + " " + k + " " + binomialCoeff ( n , k ) ) ;


"Binomial Coefficient | DP | Returns value of Binomial Coefficient C ( n , k ) ; Base Cases ; Recur ; Driver program to test above function"
How can the above be solved in JS?

function binomialCoeff ( n , k ) { if ( k > n ) return 0 ; if ( k == 0 k == n ) return 1 ; return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ; } var n = 5 , k = 2 ; document . write ( " " + n + " " + k + " " + binomialCoeff ( n , k ) ) ;


"Bitwise AND of all the odd numbers from 1 to N | Function to return the bitwise AND of all the odd integers from the range [ 1 , n ] ; Driver code"
How can the above be solved in JS?

function bitwiseAndOdd ( n ) { return 1 ; } var n = 10 ; document . write ( bitwiseAndOdd ( n ) ) ;


"Bitwise AND of all the odd numbers from 1 to N | Function to return the bitwise AND of all the odd integers from the range [ 1 , n ] ; Initialize result to 1 ; Starting from 3 , bitwise AND all the odd integers less than or equal to n ; Driver code"
How can the above be solved in JS?

function bitwiseAndOdd ( n ) { var result = 1 ; for ( var i = 3 ; i <= n ; i = i + 2 ) { result = ( result & i ) ; } return result ; } var n = 10 ; document . write ( bitwiseAndOdd ( n ) ) ;


"Calculate depth of a full Binary tree from Preorder | function to return max of left subtree height or right subtree height ; calc height of left subtree ( In preorder left subtree is processed before right ) ; calc height of right subtree ; Wrapper over findDepthRec ( ) ; Driver program"
How can the above be solved in JS?

function findDepthRec ( tree , n , index ) { if ( index >= n tree [ index ] == ' ' ) return 0 ; index ++ ; let left = findDepthRec ( tree , n , index ) ; index ++ ; let right = findDepthRec ( tree , n , index ) ; return Math . max ( left , right ) + 1 ; } function findDepth ( tree , n ) { let index = 0 ; return ( findDepthRec ( tree , n , index ) ) ; } let tree = " " . split ( ' ' ) ; let n = tree . length ; document . write ( findDepth ( tree , n ) ) ;


"Calculate square of a number without using * , / and pow ( ) | Simple solution to calculate square without using * and pow ( ) ; handle negative input ; Initialize result ; Add n to res n - 1 times ; Driver code"
How can the above be solved in JS?

function square ( n ) { if ( n < 0 ) n = - n ; let res = n ; for ( let i = 1 ; i < n ; i ++ ) res += n ; return res ; } for ( let n = 1 ; n <= 5 ; n ++ ) document . write ( " " + n + " " + square ( n ) + " " ) ;


"Case | Function to return the sorted string ; Vectors to store the lowercase and uppercase characters ; Sort both the vectors ; If current character is lowercase then pick the lowercase character from the sorted list ; Else pick the uppercase character ; Return the sorted string ; Driver code"
How can the above be solved in JS?

function getSortedString ( s , n ) { var v1 = [ ] ; var v2 = [ ] ; var i = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] . charCodeAt ( 0 ) > " " . charCodeAt ( 0 ) && s [ i ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) v1 . push ( s [ i ] ) ; if ( s [ i ] . charCodeAt ( 0 ) > " " . charCodeAt ( 0 ) && s [ i ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) v2 . push ( s [ i ] ) ; } console . log ( v1 ) ; v1 . sort ( ) ; v2 . sort ( ) ; var j = 0 ; i = 0 ; for ( var k = 0 ; k < n ; k ++ ) { if ( s [ k ] . charCodeAt ( 0 ) > " " . charCodeAt ( 0 ) && s [ k ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) { s [ k ] = v1 [ i ] ; ++ i ; } else if ( s [ k ] . charCodeAt ( 0 ) > " " . charCodeAt ( 0 ) && s [ k ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) { s [ k ] = v2 [ j ] ; ++ j ; } } return s . join ( " " ) ; } var s = " " ; var n = s . length ; document . write ( getSortedString ( s . split ( " " ) , n ) ) ;


"Case | Javascript implementation of the approach ; Function to return the sorted string ; To store the frequencies of the lowercase and the uppercase characters in the given string ; If current character is lowercase then increment its frequency in the lower [ ] array ; Else increment in the upper [ ] array ; Pointers that point to the smallest lowercase and the smallest uppercase characters respectively in the given string ; For every character in the given string ; If the current character is lowercase then replace it with the smallest lowercase character available ; Decrement the frequency of the used character ; Else replace it with the smallest uppercase character available ; Decrement the frequency of the used character ; Return the sorted string ; Driver code"
How can the above be solved in JS?

var MAX = 26 ; function getSortedString ( s , n ) { var lower = Array ( MAX ) . fill ( 0 ) ; var upper = Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( ( s [ i ] ) == s [ i ] . toLowerCase ( ) ) lower [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; else if ( s [ i ] = s [ i ] . toUpperCase ( ) ) upper [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } var i = 0 , j = 0 ; while ( i < MAX && lower [ i ] == 0 ) i ++ ; while ( j < MAX && upper [ j ] == 0 ) j ++ ; for ( var k = 0 ; k < n ; k ++ ) { if ( s [ k ] == s [ k ] . toLowerCase ( ) ) { while ( lower [ i ] == 0 ) i ++ ; s [ k ] = String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; lower [ i ] -- ; } else if ( s [ k ] == s [ k ] . toUpperCase ( ) ) { while ( upper [ j ] == 0 ) j ++ ; s [ k ] = String . fromCharCode ( j + ' ' . charCodeAt ( 0 ) ) ; upper [ j ] -- ; } } return s . join ( ' ' ) ; } var s = " " ; var n = s . length ; document . write ( getSortedString ( s . split ( ' ' ) , n ) ) ;


"Change in Median of given array after deleting given elements | Function to find the median change after removing elements from arr2 [ ] ; To store the median ; If N is odd ; If N is even ; Find the current element in arr1 ; Erase the element ; Decrement N ; If N is odd ; If N is even ; Print the corresponding difference of median ; Given arrays ; Function Call"
How can the above be solved in JS?

function medianChange ( arr1 , arr2 ) { let N = arr1 . length ; let median = [ ] ; if ( ( N & 1 ) ) median . push ( ( arr1 [ Math . floor ( N / 2 ) ] * 1 ) ) ; else median . push ( Math . floor ( ( arr1 [ Math . floor ( N / 2 ) ] + arr1 [ Math . floor ( ( N - 1 ) / 2 ) ] ) / 2 ) ) ; for ( let x = 0 ; x < arr2 . length ; x ++ ) { let it = arr1 . indexOf ( arr2 [ x ] ) ; arr1 . splice ( it , 1 ) ; N -- ; if ( ( N & 1 ) ) { median . push ( arr1 [ Math . floor ( N / 2 ) ] * 1 ) ; } else { median . push ( Math . floor ( ( arr1 [ Math . floor ( N / 2 ) ] + arr1 [ Math . floor ( ( N - 1 ) / 2 ) ] ) / 2 ) ) ; } } for ( let i = 0 ; i < median . length - 1 ; i ++ ) { document . write ( ( median [ i + 1 ] - median [ i ] ) + " " ) ; } } let arr1 = [ 2 , 4 , 6 , 8 , 10 ] ; let arr2 = [ 4 , 6 ] ; medianChange ( arr1 , arr2 )


"Check horizontal and vertical symmetry in binary matrix | Javascript program to find if a matrix is symmetric . ; Initializing as both horizontal and vertical symmetric . ; Checking for Horizontal Symmetry . We compare first row with last row , second row with second last row and so on . ; Checking each cell of a column . ; check if every cell is identical ; Checking for Vertical Symmetry . We compare first column with last column , second xolumn with second last column and so on . ; Checking each cell of a row . ; check if every cell is identical ; Driver Code"
How can the above be solved in JS?

function checkHV ( arr , N , M ) { let horizontal = true ; let vertical = true ; for ( let i = 0 , k = N - 1 ; i < parseInt ( N / 2 , 10 ) ; i ++ , k -- ) { for ( let j = 0 ; j < M ; j ++ ) { if ( arr [ i ] [ j ] != arr [ k ] [ j ] ) { horizontal = false ; break ; } } } for ( let i = 0 , k = M - 1 ; i < parseInt ( M / 2 , 10 ) ; i ++ , k -- ) { for ( let j = 0 ; j < N ; j ++ ) { if ( arr [ i ] [ j ] != arr [ k ] [ j ] ) { horizontal = false ; break ; } } } if ( ! horizontal && ! vertical ) document . write ( " " ) ; else if ( horizontal && ! vertical ) document . write ( " " ) ; else if ( vertical && ! horizontal ) document . write ( " " ) ; else document . write ( " " ) ; } let mat = [ [ 1 , 0 , 1 ] , [ 0 , 0 , 0 ] , [ 1 , 0 , 1 ] ] ; checkHV ( mat , 3 , 3 ) ;


"Check if Decimal representation of given Binary String is divisible by K or not | Function to check the binary number is divisible by K ; Array poweroftwo will store pow ( 2 , i ) % k ; Initializing the first element in Array ; Storing every pow ( 2 , i ) % k value in the array ; To store the remaining ; Iterating till N ; If current bit is 1 ; Updating rem ; If completely divisible ; If not Completely divisible ; Given Input ; length of string s ; Function Call"
How can the above be solved in JS?

function divisibleByk ( s , n , k ) { let poweroftwo = new Array ( n ) ; poweroftwo [ 0 ] = 1 % k ; for ( let i = 1 ; i < n ; i ++ ) { poweroftwo [ i ] = ( poweroftwo [ i - 1 ] * ( 2 % k ) ) % k ; } let rem = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ n - i - 1 ] == ' ' ) { rem += ( poweroftwo [ i ] ) ; rem %= k ; } } if ( rem == 0 ) { return " " ; } else return " " ; } let s = " " ; let k = 9 ; let n = s . length ; document . write ( divisibleByk ( s , n , k ) ) ;


"Check if N can be divided into K consecutive elements with a sum equal to N | Function to find the K consecutive elements with a sum equal to N ; Iterate over [ 2 , INF ] ; Store the sum ; If the sum exceeds N then break the loop ; Common difference should be divisible by number of terms ; Print value of i & return ; Print " - 1" if not possible to break N ; Given N ; Function call"
How can the above be solved in JS?

function canBreakN ( n ) { for ( let i = 2 ; ; i ++ ) { let m = parseInt ( i * ( i + 1 ) / 2 , 10 ) ; if ( m > n ) break ; let k = n - m ; if ( k % i != 0 ) continue ; document . write ( i ) ; return ; } document . write ( " " ) ; } let N = 12 ; canBreakN ( N ) ;


"Check if N rectangles of equal area can be formed from ( 4 * N ) integers | Function to check whether we can make n rectangles of equal area ; Sort the array ; Find the area of any one rectangle ; Check whether we have two equal sides for each rectangle and that area of each rectangle formed is the same ; Update the answer to false if any condition fails ; If possible ; Driver code"
How can the above be solved in JS?

function checkRectangles ( arr , n ) { let ans = true ; arr . sort ( ) ; var area = arr [ 0 ] * arr [ 4 * n - 1 ] ; for ( let i = 0 ; i < 2 * n ; i = i + 2 ) { if ( arr [ i ] != arr [ i + 1 ] arr [ 4 * n - i - 1 ] != arr [ 4 * n - i - 2 ] arr [ i ] * arr [ 4 * n - i - 1 ] != area ) { ans = false ; break ; } } if ( ans ) return true ; return false ; } var arr = [ 1 , 8 , 2 , 1 , 2 , 4 , 4 , 8 ] ; var n = 2 ; if ( checkRectangles ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if a Binary Tree is an Even | Tree node ; Function to return new tree node ; Function to check if the tree is even - odd tree ; Stores nodes of each level ; Store the current level of the binary tree ; Traverse until the queue is empty ; Stores the number of nodes present in the current level ; Check if the level is even or odd ; Add the nodes of the next level into the queue ; Increment the level count ; Construct a Binary Tree ; Check if the binary tree is even - odd tree or not"
How can the above be solved in JS?

class Node { constructor ( data ) { this . left = null ; this . right = null ; this . val = data ; } } function newNode ( data ) { let temp = new Node ( data ) ; return temp ; } function isEvenOddBinaryTree ( root ) { if ( root == null ) return true ; let q = [ ] ; q . push ( root ) ; let level = 0 ; while ( q . length > 0 ) { let size = q . length ; for ( let i = 0 ; i < size ; i ++ ) { let node = q [ 0 ] ; q . shift ( ) ; if ( level % 2 == 0 ) { if ( node . val % 2 == 1 ) return false ; } else if ( level % 2 == 1 ) { if ( node . val % 2 == 0 ) return false ; } if ( node . left != null ) { q . push ( node . left ) ; } if ( node . right != null ) { q . push ( node . right ) ; } } level ++ ; } return true ; } let root = null ; root = newNode ( 2 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 9 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 10 ) ; root . right . right = newNode ( 6 ) ; if ( isEvenOddBinaryTree ( root ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if a given array is sorted in Spiral manner or not | Function to check if the array is spirally sorted or not ; Stores start index of the array ; Stores end index of an array ; If arr [ start ] greater than arr [ end ] ; Update start ; If arr [ end ] greater than arr [ start ] ; Update end ; Driver code ; Function Call"
How can the above be solved in JS?

function isSpiralSorted ( arr , n ) { let start = 0 ; let end = n - 1 ; while ( start < end ) { if ( arr [ start ] > arr [ end ] ) { return false ; } start ++ ; if ( arr [ end ] > arr [ start ] ) { return false ; } end -- ; } return true ; } let arr = [ 1 , 10 , 14 , 20 , 18 , 12 , 5 ] ; let N = arr . length ; if ( isSpiralSorted ( arr , N ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if a string can be made equal to another string by swapping or replacement of characters | Function to find if given Strings are same or not ; Base Condition ; Stores frequency of characters of the String str1 and str2 ; Traverse Strings str1 & str2 and store frequencies in a [ ] and b [ ] ; Check if both Strings have same characters or not ; If a character is present in one String and is not in another String , return false ; Sort the array a [ ] and b [ ] ; Check arrays a and b contain the same frequency or not ; If the frequencies are not the same after sorting ; At this point , str1 can be converted to str2 ; Driver Code"
How can the above be solved in JS?

function sameStrings ( str1 , str2 ) { var N = str1 . length ; var M = str2 . length ; if ( N !== M ) { return false ; } var a = new Array ( 256 ) . fill ( 0 ) ; var b = new Array ( 256 ) . fill ( 0 ) ; for ( var j = 0 ; j < N ; j ++ ) { a [ str1 [ j ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; b [ str2 [ j ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } var i = 0 ; while ( i < 256 ) { if ( ( a [ i ] === 0 && b [ i ] === 0 ) || ( a [ i ] !== 0 && b [ i ] !== 0 ) ) { i ++ ; } else { return false ; } } a . sort ( ( x , y ) => x - y ) ; b . sort ( ( x , y ) => x - y ) ; for ( var j = 0 ; j < 256 ; j ++ ) { if ( a [ j ] !== b [ j ] ) return false ; } return true ; } var S1 = " " , S2 = " " ; if ( sameStrings ( S1 , S2 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;


"Check if a string has all characters with same frequency with one variation allowed | Assuming only lower case characters ; To check a string S can be converted to a valid string by removing less than or equal to one character . ; freq [ ] : stores the frequency of each character of a string ; Find first character with non - zero frequency ; Find a character with frequency different from freq1 . ; If we find a third non - zero frequency or count of both frequencies become more than 1 , then return false ; } else If we find a third non - zero freq ; If counts of both frequencies is more than 1 ; Return true if we reach here ; Driver code"
How can the above be solved in JS?

let CHARS = 26 ; function isValidString ( str ) { let freq = new Array ( CHARS ) ; for ( let i = 0 ; i < CHARS ; i ++ ) { freq [ i ] = 0 ; } for ( let i = 0 ; i < str . length ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let i , freq1 = 0 , count_freq1 = 0 ; for ( i = 0 ; i < CHARS ; i ++ ) { if ( freq [ i ] != 0 ) { freq1 = freq [ i ] ; count_freq1 = 1 ; break ; } } let j , freq2 = 0 , count_freq2 = 0 ; for ( j = i + 1 ; j < CHARS ; j ++ ) { if ( freq [ j ] != 0 ) { if ( freq [ j ] == freq1 ) { count_freq1 ++ ; } else { count_freq2 = 1 ; freq2 = freq [ j ] ; break ; } } } for ( let k = j + 1 ; k < CHARS ; k ++ ) { if ( freq [ k ] != 0 ) { if ( freq [ k ] == freq1 ) { count_freq1 ++ ; } if ( freq [ k ] == freq2 ) { count_freq2 ++ ; { return false ; } } if ( count_freq1 > 1 && count_freq2 > 1 ) { return false ; } } return true ; } let str = " " ; if ( isValidString ( str ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if a string has all characters with same frequency with one variation allowed | To check a string S can be converted to a variation string ; Run loop form 0 to length of string ; declaration of variables ; if first is true than countOfVal1 increase ; if second is true than countOfVal2 increase ; Driver code"
How can the above be solved in JS?

function checkForVariation ( str ) { if ( str == null str . length == 0 ) { return true ; } let map = new Map ( ) ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( ! map . has ( str [ i ] ) ) map . set ( str [ i ] , 0 ) ; map . set ( str [ i ] , map . get ( str [ i ] ) + 1 ) ; } let first = true , second = true ; let val1 = 0 , val2 = 0 ; let countOfVal1 = 0 , countOfVal2 = 0 ; for ( let [ key , value ] of map . entries ( ) ) { let i = value ; if ( first ) { val1 = i ; first = false ; countOfVal1 ++ ; continue ; } if ( i == val1 ) { countOfVal1 ++ ; continue ; } if ( second ) { val2 = i ; countOfVal2 ++ ; second = false ; continue ; } if ( i == val2 ) { countOfVal2 ++ ; continue ; } return false ; } if ( countOfVal1 > 1 && countOfVal2 > 1 ) { return false ; } else { return true ; } } document . write ( checkForVariation ( " " ) ) ;


"Check if an array of pairs can be sorted by swapping pairs with different first elements | Function to check if an array is sorted or not ; Traverse the array arr [ ] ; Return true ; Function to check if it is possible to sort the array w . r . t . first element ; Stores the ID of the first element ; Traverse the array arr [ ] ; If arr [ i ] . second is not equal to that of the group ; If array is sorted ; Driver Code"
How can the above be solved in JS?

function isSorted ( arr , N ) { for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] [ 0 ] > arr [ i - 1 ] [ 0 ] ) { return false ; } } return true ; } function isPossibleToSort ( arr , N ) { let group = arr [ 0 ] [ 1 ] ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] [ 1 ] != group ) { return " " ; } } if ( isSorted ( arr , N ) ) { return " " ; } else { return " " ; } } let arr = [ [ 340000 , 2 ] , [ 15000 , 2 ] , [ 34000 , 2 ] , [ 10000 , 2 ] ] ; let N = arr . length ; document . write ( isPossibleToSort ( arr , N ) ) ;


"Check if any permutation of string is a K times repeated string | Function to check that permutation of the given string is a K times repeating String ; if length of string is not divisible by K ; Frequency Array ; Initially frequency of each character is 0 ; Computing the frequency of each character in the string ; Loop to check that frequency of every character of the string is divisible by K ; Driver Code"
How can the above be solved in JS?

function repeatingString ( s , n , k ) { if ( n % k != 0 ) { return false ; } var frequency = new Array ( 123 ) ; for ( let i = 0 ; i < 123 ; i ++ ) { frequency [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { frequency [ s [ i ] ] ++ ; } var repeat = n / k ; for ( let i = 0 ; i < 123 ; i ++ ) { if ( frequency [ i ] % repeat != 0 ) { return false ; } } return true ; } var s = " " ; var n = s . length ; var k = 3 ; if ( repeatingString ( s , n , k ) ) { console . log ( " " ) ; } else { console . log ( " " ) ; }


"Check if any subarray can be made palindromic by replacing less than half of its elements | A Utility Function to check if a subarray can be palindromic by replacing less than half of the elements present in it ; Stores frequency of array elements ; Traverse the array ; Update frequency of each array element ; Iterator over the Map ; If frequency of any element exceeds 1 ; If no repetition is found ; Function to check and print if any subarray can be made palindromic by replacing less than half of its elements ; Given array arr [ ] ; Size of array ; Function Call"
How can the above be solved in JS?

function isConsistingSubarrayUtil ( arr , n ) { var mp = new Map ( ) ; for ( var i = 0 ; i < n ; ++ i ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } var ans = false ; mp . forEach ( ( value , key ) => { if ( value > 1 ) { ans = true ; } } ) ; if ( ans ) return true ; return false ; } function isConsistingSubarray ( arr , N ) { if ( isConsistingSubarrayUtil ( arr , N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var arr = [ 1 , 2 , 3 , 4 , 5 , 1 ] ; var N = arr . length ; isConsistingSubarray ( arr , N ) ;


"Check if decimal representation of Binary String is divisible by 9 or not | Function to convert the binary string into octal representation ; Stores binary representation of the decimal value [ 0 - 7 ] ; Stores the decimal values of binary Strings [ 0 - 7 ] ; Stores length of S ; Update S ; Update S ; Update N ; Stores octal representation of the binary String ; Traverse the binary String ; Stores 3 consecutive characters of the binary String ; Append octal representation of temp ; Function to check if binary String is divisible by 9 or not ; Stores octal representation of S ; Stores sum of elements present at odd positions of oct ; Stores sum of elements present at odd positions of oct ; Stores length of oct ; Traverse the String oct ; Update oddSum ; Traverse the String oct ; Update evenSum ; Stores octal representation of 9 ; If absolute value of ( oddSum - evenSum ) is divisible by Oct_9 ; Driver Code"
How can the above be solved in JS?

function ConvertequivalentBase8 ( S ) { let mp = new Map ( ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; let N = S . length ; if ( N % 3 == 2 ) { S = " " + S ; } else if ( N % 3 == 1 ) { S = " " + S ; } N = S . length ; let oct = " " ; for ( let i = 0 ; i < N ; i += 3 ) { let temp = S . substring ( i , i + 3 ) ; oct += mp . get ( temp ) ; } return oct ; } function binString_div_9 ( S , N ) { let oct = " " ; oct = ConvertequivalentBase8 ( S ) ; let oddSum = 0 ; let evenSum = 0 ; let M = oct . length ; for ( let i = 0 ; i < M ; i += 2 ) oddSum += ( oct [ i ] - ' ' ) ; for ( let i = 1 ; i < M ; i += 2 ) { evenSum += ( oct [ i ] - ' ' ) ; } let Oct_9 = 11 ; if ( Math . abs ( oddSum - evenSum ) % Oct_9 == 0 ) { return " " ; } return " " ; } let S = " " ; let N = S . length ; document . write ( binString_div_9 ( S , N ) ) ;


"Check if given string is a substring of string formed by repeated concatenation of z to a | Function checks if a given string is valid or not and prints the output ; Boolean flag variable to mark if given string is valid ; Traverse the given string ; If adjacent character differ by 1 ; If character ' a ' is followed by 4 ; Else flip the flag and break from the loop ; Output according to flag variable ; Given string ; Function Call"
How can the above be solved in JS?

function checkInfinite ( s ) { var flag = 1 ; var N = s . length ; for ( var i = 0 ; i < N - 1 ; i ++ ) { if ( s [ i ] == String . fromCharCode ( ( s [ i + 1 ] . charCodeAt ( 0 ) ) + 1 ) ) { continue ; } else if ( s [ i ] == ' ' && s [ i + 1 ] == ' ' ) { continue ; } else { flag = 0 ; break ; } } if ( flag == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var s = " " ; checkInfinite ( s ) ;


"Check if it is possible to reach ( X , Y ) from ( 1 , 0 ) by given steps | Function to find the GCD of two numbers a and b ; Base Case ; Recursively find the GCD ; Function to check if ( x , y ) can be reached from ( 1 , 0 ) from given moves ; If GCD is 1 , then print " Yes " ; Given X and Y ; Function call"
How can the above be solved in JS?

function GCD ( a , b ) { if ( b == 0 ) return a ; else return GCD ( b , a % b ) ; } function check ( x , y ) { if ( GCD ( x , y ) == 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let X = 2 , Y = 7 ; check ( X , Y ) ;


"Check if it is possible to reach ( X , Y ) from ( 1 , 1 ) by given steps | Function to find the gcd of two numbers ; Base case ; Recurse ; Function to print the answer ; GCD of X and Y ; If GCD is power of 2 ; Given X and Y ; Function call"
How can the above be solved in JS?

function gcd ( a , b ) { if ( a < b ) { let t = a ; a = b ; b = t ; } if ( a % b == 0 ) return b ; return gcd ( b , a % b ) ; } function printAnswer ( x , y ) { let val = gcd ( x , y ) ; if ( ( val & ( val - 1 ) ) == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } let x = 4 ; let y = 7 ; printAnswer ( x , y ) ;


"Check if linked list is sorted ( Iterative and Recursive ) | Linked list node ; function to Check Linked List is sorted in descending order or not ; Base cases ; Check first two nodes and recursively check remaining . ; Driver code"
How can the above be solved in JS?

class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function isSortedDesc ( head ) { if ( head == null head . next == null ) return true ; return head . data > head . next . data && isSortedDesc ( head . next ) ; } function newNode ( data ) { var temp = new Node ( ) ; temp . next = null ; temp . data = data ; return temp ; } var head = newNode ( 7 ) ; head . next = newNode ( 5 ) ; head . next . next = newNode ( 4 ) ; head . next . next . next = newNode ( 3 ) ; if ( isSortedDesc ( head ) == true ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if lowercase and uppercase characters are in same order | Function to check if both the case follow the same order ; Traverse the string ; Store both lowercase and uppercase in two different strings ; transform lowerStr1 to upper ; Driver code"
How can the above be solved in JS?

function isCheck ( str ) { var len = str . length ; var lowerStr = " " , upperStr = " " ; for ( var i = 0 ; i < len ; i ++ ) { if ( str [ i ] >= ' ' && str [ i ] < ' ' ) upperStr = upperStr + str [ i ] ; else lowerStr = lowerStr + str [ i ] ; } lowerStr = lowerStr . toUpperCase ( ) ; console . log ( lowerStr ) ; return lowerStr === upperStr ; } var str = " " ; isCheck ( str ) ? document . write ( " " ) : document . write ( " " ) ;


"Check if one string can be converted to another | JavaScript implementation of the above approach . ; Function for find from Disjoint set algorithm ; Function for the union from Disjoint set algorithm ; Function to check if one String can be converted to another . ; All the characters are checked whether it 's either not replaced or replaced  by a similar character using a map. ; To check if there are cycles . If yes , then they are not convertible . Else , they are convertible . ; Function to initialize parent array for union and find algorithm . ; Driver code"
How can the above be solved in JS?

var parent = new Array ( 26 ) . fill ( 0 ) ; function find ( x ) { if ( x !== parent [ x ] ) return ( parent [ x ] = find ( parent [ x ] ) ) ; return x ; } function join ( x , y ) { var px = find ( x ) ; var pz = find ( y ) ; if ( px !== pz ) { parent [ pz ] = px ; } } function convertible ( s1 , s2 ) { var mp = { } ; for ( var i = 0 ; i < s1 . length ; i ++ ) { if ( ! mp . hasOwnProperty ( s1 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) { mp [ s1 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = s2 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ; } else { if ( mp [ s1 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] !== s2 [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) return false ; } } for ( const [ key , value ] of Object . entries ( mp ) ) { if ( key === value ) continue ; else { if ( find ( key ) == find ( value ) ) return false ; else join ( key , value ) ; } } return true ; } function initialize ( ) { for ( var i = 0 ; i < 26 ; i ++ ) { parent [ i ] = i ; } } var s1 , s2 ; s1 = " " ; s2 = " " ; initialize ( ) ; if ( convertible ( s1 , s2 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;


"Check if row | Function to check if row - major order traversal of the matrix is is palindrome ; Loop to check if the matrix is matrix is palindrome or not ; Driver Code"
How can the above be solved in JS?

function isPal ( a , n , m ) { for ( let i = 0 ; i < n / 2 ; i ++ ) { for ( let j = 0 ; j < m - 1 ; j ++ ) { if ( a [ i ] [ j ] != a [ n - 1 - i ] [ m - 1 - j ] ) return false ; } } return true ; } let n = 3 , m = 3 ; let a = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 4 ] , [ 3 , 2 , 1 ] ] ; if ( isPal ( a , n , m ) ) { document . write ( " " + " " ) ; } else { document . write ( " " + " " ) ; }


"Check if string is right to left diagonal or not | Function to check if the given string is right to left diagonal or not ; Iterate over string ; If character is not same as the first character then return false ; Given String str ; Function call"
How can the above be solved in JS?

function is_rtol ( s ) { let tmp = ( Math . sqrt ( s . length ) ) - 1 ; let first = s [ tmp ] ; for ( let pos = tmp ; pos < s . length - 1 ; pos += tmp ) { if ( s [ pos ] != first ) { return false ; } } return true ; } let str = " " ; if ( is_rtol ( str ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }


"Check if the string contains consecutive letters and each letter occurs exactly once | Function to check if the condition holds ; Get the length of the string ; sort the given string ; Iterate for every index and check for the condition ; If are not consecutive ; 1 st example ; 2 nd example"
How can the above be solved in JS?

function check ( s ) { let l = s . length ; s . sort ( ) ; for ( let i = 1 ; i < l ; i ++ ) { if ( ( s [ i ] . charCodeAt ( ) - s [ i - 1 ] . charCodeAt ( ) ) != 1 ) return false ; } return true ; } let str = " " ; if ( check ( str . split ( ' ' ) ) == true ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; let str1 = " " ; if ( check ( str1 . split ( ' ' ) ) == true ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if the string contains consecutive letters and each letter occurs exactly once | javascript program to implement the above approach ; for all the characters of the string ; find the ascii value of the character ; check if if its a valid character , if not then return false ; calculate sum of all the characters ascii values ; find minimum ascii value from the string ; find maximum ascii value from the string ; To get the previous element of the minimum ASCII value ; take the expected sum from the above equation ; check if the expected sum is equals to the calculated sum or not ; 1 st example ; 2 nd example"
How can the above be solved in JS?

function check ( str ) { var min = Number . MAX_VALUE ; var max = Number . MIN_VALUE ; var sum = 0 ; for ( i = 0 ; i < str . length ; i ++ ) { var ascii = parseInt ( str . charCodeAt ( i ) ) ; if ( ascii < 96 ascii > 122 ) return false ; sum += ascii ; if ( min > ascii ) min = ascii ; if ( max < ascii ) max = ascii ; } min -= 1 ; var eSum = parseInt ( ( max * ( max + 1 ) ) / 2 ) - ( ( min * ( min + 1 ) ) / 2 ) ; return sum == eSum ; } var str = " " ; if ( check ( str ) ) document . write ( " " ) ; else document . write ( " " ) ; var str1 = " " ; if ( check ( str1 ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check if the sum of digits of a number N divides it | Function to check if sum of digits of a number divides it ; Calculate sum of all of digits of N ; Driver Code"
How can the above be solved in JS?

function isSumDivides ( N ) { var temp = N ; var sum = 0 ; while ( temp > 0 ) { sum += temp % 10 ; temp = parseInt ( temp / 10 ) ; } if ( N % sum == 0 ) return 1 ; else return 0 ; } var N = 12 ; if ( isSumDivides ( N ) == 1 ) document . write ( " " ) ; else document . write ( " " ) ;


"Check whether given string can be generated after concatenating given strings | Function that return true if pre is a prefix of str ; While there are characters to match ; If characters differ at any position ; str starts with pre ; Function that return true if suff is a suffix of str ; While there are characters to match ; If characters differ at any position ; str ends with suff ; Function that returns true if str = a + b or str = b + a ; str cannot be generated by concatenating a and b ; If str starts with a i . e . a is a prefix of str ; Check if the rest of the characters are equal to b i . e . b is a suffix of str ; If str starts with b i . e . b is a prefix of str ; Check if the rest of the characters are equal to a i . e . a is a suffix of str ; Driver code"
How can the above be solved in JS?

function startsWith ( str , pre ) { let strLen = str . length ; let preLen = pre . length ; let i = 0 , j = 0 ; while ( i < strLen && j < preLen ) { if ( str [ i ] != pre [ j ] ) return false ; i ++ ; j ++ ; } return true ; } function endsWith ( str , suff ) { let i = str . length - 1 ; let j = suff . length - 1 ; while ( i >= 0 && j >= 0 ) { if ( str [ i ] != suff [ j ] ) return false ; i -- ; j -- ; } return true ; } function checkString ( str , a , b ) { if ( str . length != a . length + b . length ) return false ; if ( startsWith ( str , a ) ) { if ( endsWith ( str , b ) ) return true ; } if ( startsWith ( str , b ) ) { if ( endsWith ( str , a ) ) return true ; } return false ; } let str = " " ; let a = " " ; let b = " " ; if ( checkString ( str , a , b ) ) document . write ( " " ) ; else document . write ( " " ) ;


"Check whether jigsaw puzzle solveable or not | Function to check if the jigsaw Puzzle is solveable or not ; Base Case ; By placing the blank tabs as a chain ; Driver code"
How can the above be solved in JS?

function checkSolveable ( n , m ) { if ( n == 1 m == 1 ) document . write ( " " ) ; else if ( m == 2 && n == 2 ) document . write ( " " ) ; else document . write ( " " ) ; } let n = 1 , m = 3 ; checkSolveable ( n , m ) ;


"Compare two strings considering only alphanumeric characters | Function to check alphanumeric equality of both strings ; variable declaration ; Length of first string ; Length of second string ; To check each and every characters of both string ; If the current character of the first string is not an alphanumeric character , increase the pointer i ; If the current character of the second string is not an alphanumeric character , increase the pointer j ; if all alphanumeric characters of both strings are same then return true ; if any alphanumeric characters of both strings are not same then return false ; If current character matched , increase both pointers to check the next character ; If not same , then return false ; Function to print Equal or Unequal if strings are same or not ; check alphanumeric equality of both strings ; if both are alphanumeric equal , print Equal ; otherwise print Unequal ; Driver code"
How can the above be solved in JS?

function CompareAlphanumeric ( str1 , str2 ) { let i , j ; i = 0 ; j = 0 ; let len1 = str1 . length ; let len2 = str2 . length ; while ( i <= len1 && j <= len2 ) { while ( i < len1 && ( ! ( ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) ) ) ) { i ++ ; } while ( j < len2 && ( ! ( ( str2 [ j ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str2 [ j ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str2 [ j ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str2 [ j ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str2 [ j ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str2 [ j ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) ) ) ) { j ++ ; } if ( i == len1 && j == len2 ) { return true ; } else if ( str1 [ i ] != str2 [ j ] ) { return false ; } else { i ++ ; j ++ ; } } return false ; } function CompareAlphanumericUtil ( str1 , str2 ) { let res ; res = CompareAlphanumeric ( str1 . split ( ' ' ) , str2 . split ( ' ' ) ) ; if ( res == true ) { document . write ( " " + " " ) ; } else { document . write ( " " ) ; } } let str1 , str2 ; str1 = " " ; str2 = " " ; CompareAlphanumericUtil ( str1 , str2 ) ; str1 = " " ; str2 = " " ; CompareAlphanumericUtil ( str1 , str2 ) ;


"Compositorial of a number | Javascript program to find compositorial of composite numbers ; Function to check if a number is composite . ; Corner cases ; This is checked so that we can skip the middle five numbers in the below loop ; This function stores all composite numbers less than N ; Function to calculate the compositorial of n ; Multiply first n composite number ; Driver code ; Vector to store all the composite less than N"
How can the above be solved in JS?

let compo = [ ] ; function isComposite ( n ) { if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; let i = 5 ; while ( i * i <= n ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; i = i + 6 ; } return false ; } function Compositorial_list ( n ) { let l = 0 ; for ( let i = 4 ; i < 1000000 ; i ++ ) { if ( l < n ) { if ( isComposite ( i ) ) { compo . push ( i ) ; l += 1 ; } } } } function calculateCompositorial ( n ) { let result = 1 ; for ( let i = 0 ; i < n ; i ++ ) result = result * compo [ i ] ; return result ; } let n = 5 ; Compositorial_list ( n ) ; document . write ( calculateCompositorial ( n ) ) ;


"Compute maximum of the function efficiently over all sub | JavaScript implementation of the above approach ; Function to return maximum sum of a sub - array ; Function to return maximum value of function F ; Compute arrays B [ ] and C [ ] ; Find maximum sum sub - array of both of the arrays and take maximum among them ; Driver code"
How can the above be solved in JS?

const MAX = 100005 ; function kadaneAlgorithm ( ar , n ) { let sum = 0 , maxSum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { sum += ar [ i ] ; if ( sum < 0 ) sum = 0 ; maxSum = Math . max ( maxSum , sum ) ; } return maxSum ; } function maxFunction ( arr , n ) { let b = new Array ( MAX ) , c = new Array ( MAX ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( i & 1 ) { b [ i ] = Math . abs ( arr [ i + 1 ] - arr [ i ] ) ; c [ i ] = - b [ i ] ; } else { c [ i ] = Math . abs ( arr [ i + 1 ] - arr [ i ] ) ; b [ i ] = - c [ i ] ; } } let ans = kadaneAlgorithm ( b , n - 1 ) ; ans = Math . max ( ans , kadaneAlgorithm ( c , n - 1 ) ) ; return ans ; } let arr = [ 1 , 5 , 4 , 7 ] ; let n = arr . length ; document . write ( maxFunction ( arr , n ) ) ;


"Construct a frequency array of digits of the values obtained from x ^ 1 , x ^ 2 , ... ... . . , x ^ n | Function that traverses digits in a number and modifies frequency count array ; Array to keep count of digits ; Traversing through x ^ 1 to x ^ n ; For power function , both its parameters are to be in double ; calling countDigits function on x ^ i ; Printing count of digits 0 - 9 ; Driver code"
How can the above be solved in JS?

function countDigits ( val , arr ) { while ( val > 0 ) { let digit = val % 10 ; arr [ digit ] ++ ; val = Math . floor ( val / 10 ) ; } return ; } function countFrequency ( x , n ) { let freq_count = new Array ( 10 ) ; for ( let i = 0 ; i < 10 ; i ++ ) { freq_count [ i ] = 0 ; } for ( let i = 1 ; i <= n ; i ++ ) { let val = Math . pow ( x , i ) ; countDigits ( val , freq_count ) ; } for ( let i = 0 ; i <= 9 ; i ++ ) { document . write ( freq_count [ i ] + " " ) ; } } let x = 15 , n = 3 ; countFrequency ( x , n ) ;


"Convert a String to an Integer using Recursion | Recursive function to convert string to integer ; If the number represented as a string contains only a single digit then returns its value ; Recursive call for the sub - string starting at the second character ; First digit of the number ; First digit multiplied by the appropriate power of 10 and then add the recursive result For example , xy = ( ( x * 10 ) + y ) ; Driver code"
How can the above be solved in JS?

function stringToInt ( str ) { if ( str . length == 1 ) return ( str [ 0 ] - ' ' ) ; var y = stringToInt ( str . substring ( 1 ) ) ; var x = str [ 0 ] - ' ' ; x = x * Math . pow ( 10 , str . Length - 1 ) + y ; return ( x ) ; } var str = " " . split ( ) document . write ( stringToInt ( str ) ) ;


"Convert to number with digits as 3 and 8 only | function for minimum operation ; remainder and operations count ; count digits not equal to 3 or 8 ; driver function"
How can the above be solved in JS?

function minOp ( num ) { var rem ; var count = 0 ; while ( num ) { rem = num % 10 ; if ( ! ( rem == 3 rem == 8 ) ) count ++ ; num = parseInt ( num / 10 ) ; } return count ; } var num = 234198 ; document . write ( " " + minOp ( num ) ) ;


"Coprime divisors of a number | Function which finds the required pair of divisors of N ; We iterate upto sqrt ( N ) as we can find all the divisors of N in this time ; If x is a divisor of N keep dividing as long as possible ; We have found a required pair ; No such pair of divisors of N was found , hence print - 1 ; Sample example 1 ; Sample example 2"
How can the above be solved in JS?

function findCoprimePair ( N ) { for ( let x = 2 ; x <= Math . sqrt ( N ) ; x ++ ) { if ( N % x == 0 ) { while ( N % x == 0 ) { N = Math . floor ( N / x ) ; } if ( N > 1 ) { document . write ( x + " " + N + " " ) ; return ; } } } document . write ( - 1 + " " ) ; } let N = 45 ; findCoprimePair ( N ) ; N = 25 ; findCoprimePair ( N ) ;


"Count all possible groups of size 2 or 3 that have sum as multiple of 3 | Returns count of all possible groups that can be formed from elements of a [ ] . ; Create an array C [ 3 ] to store counts of elements with remainder 0 , 1 and 2. c [ i ] would store count of elements with remainder i ; To store the result ; Count elements with remainder 0 , 1 and 2 ; Case 3. a : Count groups of size 2 from 0 remainder elements ; Case 3. b : Count groups of size 2 with one element with 1 remainder and other with 2 remainder ; Case 4. a : Count groups of size 3 with all 0 remainder elements ; Case 4. b : Count groups of size 3 with all 1 remainder elements ; Case 4. c : Count groups of size 3 with all 2 remainder elements ; Case 4. c : Count groups of size 3 with different remainders ; Return total count stored in res ; Driver Code"
How can the above be solved in JS?


"Count composite fibonacci numbers from given array | Function to find all Fibonacci numbers up to Max ; Store all Fibonacci numbers upto Max ; Stores previous element of Fibonacci sequence ; Stores previous element of Fibonacci sequence ; Insert prev into hashmap ; Insert all the Fibonacci numbers up to Max ; Insert curr into hashmap ; Stores curr into temp ; Update curr ; Update prev ; Function to find all Composite numbers up to Max ; isPrime [ i ] : Stores if i is a prime number or not ; Calculate all prime numbers up to Max using Sieve of Eratosthenes ; If P is a prime number ; Set all multiple of P as non - prime ; Update isPrime ; Function to find the numbers which is both a composite and Fibonacci number ; Stores the largest element of the array ; Traverse the array arr [ ] ; Update Max ; isPrim [ i ] check i is a prime number or not ; Stores all the Fibonacci numbers ; Traverse the array arr [ ] ; current element is not a composite number ; If current element is a Fibonacci and composite number ; Print current element ; Driver Code"
How can the above be solved in JS?

function createhashmap ( Max ) { var hashmap = new Set ( ) ; var curr = 1 ; var prev = 0 ; hashmap . add ( prev ) ; while ( curr <= Max ) { hashmap . add ( curr ) ; var temp = curr ; curr = curr + prev ; prev = temp ; } return hashmap ; } function SieveOfEratosthenes ( Max ) { var isPrime = Array ( Max + 1 ) . fill ( true ) ; isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( var p = 2 ; p * p <= Max ; p ++ ) { if ( isPrime [ p ] ) { for ( var i = p * p ; i <= Max ; i += p ) { isPrime [ i ] = false ; } } } return isPrime ; } function cntFibonacciPrime ( arr , N ) { var Max = arr [ 0 ] ; for ( var i = 1 ; i < N ; i ++ ) { Max = Math . max ( Max , arr [ i ] ) ; } var isPrime = SieveOfEratosthenes ( Max ) ; var hashmap = createhashmap ( Max ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) continue ; if ( hashmap . has ( arr [ i ] ) && ! isPrime [ arr [ i ] ] ) { document . write ( arr [ i ] + " " ) ; } } } var arr = [ 13 , 55 , 7 , 3 , 5 , 21 , 233 , 144 , 89 ] ; var N = arr . length ; cntFibonacciPrime ( arr , N ) ;


"Count maximum possible pairs from an array having sum K | Function to count the maximum number of pairs from given array with sum K ; Sort array in increasing order ; Stores the final result ; Initialize the left and right pointers ; Traverse array until start < end ; Decrement right by 1 ; Increment left by 1 ; Increment result and left poleter by 1 and decrement right pointer by 1 ; Print the result ; Driver Code ; Function Call"
How can the above be solved in JS?

function maxPairs ( nums , k ) { nums . sort ( ) ; let result = 0 ; let start = 0 , end = nums . length - 1 ; while ( start < end ) { if ( nums [ start ] + nums [ end ] > k ) end -- ; else if ( nums [ start ] + nums [ end ] < k ) start ++ ; else { start ++ ; end -- ; result ++ ; } } document . write ( result ) ; } let arr = [ 1 , 2 , 3 , 4 ] ; let K = 5 ; maxPairs ( arr , K ) ;


"Count maximum possible pairs from an array having sum K | Function to find the maximum number of pairs with a sum K such that same element can 't be used twice ; Initialize a hashm ; Store the final result ; Iterate over the array nums [ ] ; Decrement its frequency in m and increment the result by 1 ; Increment its frequency by 1 if it is already present in m . Otherwise , set its frequency to 1 ; Print the result ; Driver Code ; Function Call"
How can the above be solved in JS?

function maxPairs ( nums , k ) { var m = new Map ( ) ; var result = 0 ; nums . forEach ( i => { if ( m . has ( i ) && m . get ( i ) > 0 ) { m . set ( i , m . get ( i ) - 1 ) ; result ++ ; } else { if ( m . has ( k - i ) ) m . set ( k - i , m . get ( k - i ) + 1 ) else m . set ( k - i , 1 ) } } ) ; document . write ( result ) ; } var arr = [ 1 , 2 , 3 , 4 ] ; var K = 5 ; maxPairs ( arr , K ) ;


"Count number of indices such that s [ i ] = s [ i + 1 ] : Range queries | Function to create prefix array ; Function to return the result of the query ; Driver Code ; Query 1 ; Query 2"
How can the above be solved in JS?

function preCompute ( n , s , pref ) { pref [ 0 ] = 0 ; for ( let i = 1 ; i < n ; i ++ ) { pref [ i ] = pref [ i - 1 ] ; if ( s [ i - 1 ] == s [ i ] ) pref [ i ] ++ ; } } function query ( pref , l , r ) { return pref [ r ] - pref [ l ] ; } let s = " " ; let n = s . length ; let pref = new Array ( n ) ; preCompute ( n , s , pref ) ; let l = 1 ; let r = 2 ; document . write ( query ( pref , l , r ) + " " ) ; l = 1 ; r = 5 ; document . write ( query ( pref , l , r ) + " " ) ;


"Count number of strings ( made of R , G and B ) using given combination | Function to calculate number of strings ; Store factorial of numbers up to n for further computation ; Find the remaining values to be added ; Make all possible combinations of R , B and G for the remaining value ; Compute permutation of each combination one by one and add them . ; Return total no . of strings / permutation ; Drivers code"
How can the above be solved in JS?

function possibleStrings ( n , r , b , g ) { let fact = new Array ( n + 1 ) ; fact [ 0 ] = 1 ; for ( let i = 1 ; i <= n ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; let left = n - ( r + g + b ) ; let sum = 0 ; for ( let i = 0 ; i <= left ; i ++ ) { for ( let j = 0 ; j <= left - i ; j ++ ) { let k = left - ( i + j ) ; sum = sum + fact [ n ] / ( fact [ i + r ] * fact [ j + b ] * fact [ k + g ] ) ; } } return sum ; } let n = 4 , r = 2 ; let b = 0 , g = 1 ; document . write ( possibleStrings ( n , r , b , g ) ) ;


"Count number of substrings of a string consisting of same characters | Function to return the number of substrings of same characters ; Size of the string ; Initialize count to 1 ; Initialize left to 0 and right to 1 to traverse the string ; Checking if consecutive characters are same and increment the count ; When we encounter a different characters ; Increment the result ; To repeat the whole process set left equals right and count variable to 1 ; Store the final value of result ; Driver code"
How can the above be solved in JS?

function findNumbers ( s ) { var n = s . length ; var count = 1 ; var result = 0 ; var left = 0 ; var right = 1 ; while ( right < n ) { if ( s [ left ] == s [ right ] ) { count ++ ; } else { result += parseInt ( count * ( count + 1 ) / 2 ) ; left = right ; count = 1 ; } right ++ ; } result += parseInt ( count * ( count + 1 ) / 2 ) ; document . write ( result ) ; } var s = " " ; findNumbers ( s ) ;


"Count number of ways to cover a distance | Set 2 | Function to return the count of the total number of ways to cover the distance with 1 , 2 and 3 steps ; Base conditions ; To store the last three stages ; Find the numbers of steps required to reach the distance i ; Return the required answer ; Driver code"
How can the above be solved in JS?

function countWays ( n ) { if ( n == 0 ) return 1 ; if ( n <= 2 ) return n ; let f0 = 1 , f1 = 1 , f2 = 2 ; let ans = 0 ; for ( let i = 3 ; i <= n ; i ++ ) { ans = f0 + f1 + f2 ; f0 = f1 ; f1 = f2 ; f2 = ans ; } return ans ; } let n = 4 ; document . write ( countWays ( n ) ) ;


"Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2 | Function which will calculate the elements in the given range ; Store the current power of 2 ; Store the current power of 3 ; power23 [ ] will store pairwise product of elements of power2 and power3 that are <= r ; Insert in power23 ] [ ] only if mul <= r ; Store the required answer ; Print the result ; Driver code"
How can the above be solved in JS?

function calc_ans ( l , r ) { var power2 = [ ] , power3 = [ ] ; var mul2 = 1 ; while ( mul2 <= r ) { power2 . push ( mul2 ) ; mul2 *= 2 ; } var mul3 = 1 ; while ( mul3 <= r ) { power3 . push ( mul3 ) ; mul3 *= 3 ; } var power23 = [ ] ; for ( var x = 0 ; x < power2 . length ; x ++ ) { for ( var y = 0 ; y < power3 . length ; y ++ ) { var mul = power2 [ x ] * power3 [ y ] ; if ( mul == 1 ) continue ; if ( mul <= r ) power23 . push ( mul ) ; } } var ans = 0 ; power23 . forEach ( x => { if ( x >= l && x <= r ) ans ++ ; } ) ; document . write ( ans ) ; } var l = 1 , r = 10 ; calc_ans ( l , r ) ;


"Count numbers having 0 as a digit | Returns 1 if x has 0 , else 0 ; Traverse through all digits of x to check if it has 0. ; If current digit is 0 , return true ; Returns count of numbers from 1 to n with 0 as digit ; Initialize count of numbers having 0 as digit ; Traverse through all numbers and for every number check if it has 0. ; Driver program"
How can the above be solved in JS?

function has0 ( x ) { while ( x ) { if ( x % 10 == 0 ) return 1 ; x = Math . floor ( x / 10 ) ; } return 0 ; } function getCount ( n ) { let count = 0 ; for ( let i = 1 ; i <= n ; i ++ ) count += has0 ( i ) ; return count ; } let n = 107 ; document . write ( " " + " " + n + " " + getCount ( n ) ) ;


"Count of K length subsequence whose product is even | Function to calculate nCr ; Returns factorial of n ; Function for finding number of K length subsequences whose product is even number ; counting odd numbers in the array ; Driver code"
How can the above be solved in JS?

function nCr ( n , r ) { if ( r > n ) return 0 ; return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } function fact ( n ) { var res = 1 ; for ( var i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } function countSubsequences ( arr , n , k ) { var countOdd = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) countOdd ++ ; } var ans = nCr ( n , k ) - nCr ( countOdd , k ) ; return ans ; } var arr = [ 2 , 4 ] ; var K = 1 ; var N = arr . length ; document . write ( countSubsequences ( arr , N , K ) ) ;


"Count of Octal numbers upto N digits | Function to return the count of natural octal numbers upto N digits ; Loop to iterate from 1 to N and calculating number of octal numbers for every ' i ' th digit . ; Driver code"
How can the above be solved in JS?

function count ( N ) { var sum = 0 ; for ( var i = 1 ; i <= N ; i ++ ) { sum += 7 * Math . pow ( 8 , i - 1 ) ; } return sum ; } var N = 4 ; document . write ( count ( N ) ) ;


"Count of cyclic permutations having XOR with other binary string as 0 | Implementation of Z - algorithm for linear time pattern searching ; Function to get the count of the cyclic permutations of b that given 0 when XORed with a ; concatenate b with b ; new b now contains all the cyclic permutations of old b as it 's sub-strings ; concatenate pattern with text ; Fill z array used in Z algorithm ; pattern occurs at index i since z value of i equals pattern length ; Driver Code"
How can the above be solved in JS?

function compute_z ( s , z ) { var l = 0 , r = 0 ; var n = s . length ; for ( var i = 1 ; i <= n - 1 ; i ++ ) { if ( i > r ) { l = i ; r = i ; while ( r < n && s [ r - l ] === s [ r ] ) { r ++ ; } z [ i ] = r - l ; r -- ; } else { var k = i - l ; if ( z [ k ] < r - i + 1 ) { z [ i ] = z [ k ] ; } else { l = i ; while ( r < n && s [ r - l ] === s [ r ] ) { r ++ ; } z [ i ] = r - l ; r -- ; } } } } function countPermutation ( a , b ) { b = b + b ; b = b . substring ( 0 , b . length - 1 ) ; var ans = 0 ; var s = a + " " + b ; var n = s . length ; var z = new Array ( n ) . fill ( 0 ) ; compute_z ( s , z ) ; for ( var i = 1 ; i <= n - 1 ; i ++ ) { if ( z [ i ] === a . length ) { ans ++ ; } } return ans ; } var a = " " ; var b = " " ; document . write ( countPermutation ( a , b ) ) ;


"Count of distinct characters in a substring by given range for Q queries | Javascript Program for Naive Approach ; counter to count distinct char ; Initializing frequency array to count characters as the appear in substring S [ L : R ] ; Iterating over S [ L ] to S [ R ] ; incrementing the count of s [ i ] character in frequency array ; if frequency of any character is > 0 then increment the counter ; Driver code"
How can the above be solved in JS?

function findCount ( s , L , R ) { var distinct = 0 ; var frequency = Array ( 26 ) . fill ( 0 ) ; for ( var i = L ; i <= R ; i ++ ) { frequency [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } for ( var i = 0 ; i < 26 ; i ++ ) { if ( frequency [ i ] > 0 ) distinct ++ ; } document . write ( distinct + " " ) ; } var s = " " ; var queries = 3 ; var Q = [ [ 0 , 10 ] , [ 15 , 18 ] , [ 12 , 20 ] ] ; for ( var i = 0 ; i < queries ; i ++ ) findCount ( s , Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) ;


"Count of elements which are not at the correct position | Function to return the count of elements which are not in the correct position when sorted ; To store a copy of the original array ; Copy the elements of the given array to the new array ; To store the required count ; Sort the original array ; If current element was not at the right position ; Driver code"
How can the above be solved in JS?

function cntElements ( arr , n ) { let copy_arr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) copy_arr [ i ] = arr [ i ] ; let count = 0 ; arr . sort ( ( a , b ) => a - b ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != copy_arr [ i ] ) { count ++ ; } } return count ; } let arr = [ 1 , 2 , 6 , 2 , 4 , 5 ] ; let n = arr . length ; document . write ( cntElements ( arr , n ) ) ;


"Count of elements which are power of 2 in a given range subarray for Q queries | Javascript implementation to find elements that are a power of two ; prefix [ i ] is going to store the number of elements which are a power of two till i ( including i ) . ; Function to find the maximum range whose sum is divisible by M . ; Calculate the prefix sum ; Function to return the number of elements which are a power of two in a subarray ; Driver Code"
How can the above be solved in JS?

let MAX = 10000 ; let prefix = Array . from ( { length : MAX + 1 } , ( _ , i ) => 0 ) ; function isPowerOfTwo ( x ) { if ( x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ) return true ; return false ; } function computePrefix ( n , a ) { if ( isPowerOfTwo ( a [ 0 ] ) ) prefix [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] ; if ( isPowerOfTwo ( a [ i ] ) ) prefix [ i ] ++ ; } } function query ( L , R ) { if ( L == 0 ) return prefix [ R ] ; return prefix [ R ] - prefix [ L - 1 ] ; } let A = [ 3 , 8 , 5 , 2 , 5 , 10 ] ; let N = A . length ; computePrefix ( N , A ) ; document . write ( query ( 0 , 4 ) + " " ) ; document . write ( query ( 3 , 5 ) ) ;


"Count of indices in an array that satisfy the given condition | Function to return the count of indices that satisfy the given condition ; To store the result ; To store the current maximum Initialized to 0 since there are only positive elements in the array ; i is a valid index ; Update the maximum so far ; Increment the counter ; Driver code"
How can the above be solved in JS?

function countIndices ( arr , n ) { var cnt = 0 ; var max = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( max < arr [ i ] ) { max = arr [ i ] ; cnt ++ ; } } return cnt ; } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; document . write ( countIndices ( arr , n ) ) ;


"Count of integral coordinates that lies inside a Square | Function to calculate the integral points inside a square ; Driver Code"
How can the above be solved in JS?

function countIntgralPoints ( x1 , y1 , x2 , y2 ) { document . write ( ( y2 - y1 - 1 ) * ( x2 - x1 - 1 ) ) ; } var x1 = 1 , y1 = 1 ; var x2 = 4 , y2 = 4 ; countIntgralPoints ( x1 , y1 , x2 , y2 ) ;


"Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices | function to find total number of ways ; No of odd indices in n - digit number ; No of even indices in n - digit number ; No of ways of arranging prime number digits in odd indices ; No of ways of arranging odd number digits in even indices ; returning the total number of ways ; drive code"
How can the above be solved in JS?

function find_Numb_ways ( n ) { var odd_indices = n / 2 ; var even_indices = ( n / 2 ) + ( n % 2 ) ; var arr_odd = Math . pow ( 4 , odd_indices ) ; var arr_even = Math . pow ( 5 , even_indices ) ; return arr_odd * arr_even ; } var n = 4 ; document . write ( find_Numb_ways ( n ) ) ;


"Count of strings that does not contain Arc intersection | Function to check if there is arc intersection or not ; Traverse the string S ; Insert all the elements in the stack one by one ; Extract the top element ; Pop out the top element ; Check if the top element is same as the popped element ; Otherwise ; If the stack is empty ; Function to check if there is arc intersection or not for the given array of strings ; Stores count of string not having arc intersection ; Iterate through array ; Length of every string ; Function Call ; Print the desired count ; Driver Code ; Function Call"
How can the above be solved in JS?

function arcIntersection ( S , len ) { var stk = [ ] ; for ( var i = 0 ; i < len ; i ++ ) { stk . push ( S [ i ] ) ; if ( stk . length >= 2 ) { var temp = stk [ stk . length - 1 ] ; stk . pop ( ) ; if ( stk [ stk . length - 1 ] == temp ) { stk . pop ( ) ; } else { stk . push ( temp ) ; } } } if ( stk . length == 0 ) return 1 ; return 0 ; } function countString ( arr , N ) { var count = 0 ; for ( var i = 0 ; i < N ; i ++ ) { var len = arr [ i ] . length ; count += arcIntersection ( arr [ i ] , len ) ; } document . write ( count + " " ) ; } var arr = [ " " , " " , " " ] ; var N = arr . length ; countString ( arr , N ) ;


"Count of substrings formed using a given set of characters only | Function to find the number of substrings that can be formed using given characters ; Boolean array for storing the available characters ; Mark indices of all available characters as 1 ; Initialize lastPos as - 1 ; Initialize ans with the total no of possible substrings ; Traverse the string from left to right ; If the current character is not present in B ; Subtract the total possible substrings ; Update the value of lastpos to current index ; Print the final answer ; Given String ; Given character array ; Function Call"
How can the above be solved in JS?

function numberofsubstrings ( str , k , charArray ) { var N = str . length ; var available = [ 26 ] ; for ( var i = 0 ; i < k ; i ++ ) { available [ charArray [ i ] - ' ' ] = 1 ; } var lastPos = - 1 ; var ans = ( N * ( N + 1 ) ) / 2 ; for ( var i = 0 ; i < N ; i ++ ) { if ( available [ str . charAt ( i ) - ' ' ] == 0 ) { ans -= ( ( i - lastPos ) * ( N - i ) ) ; lastPos = i ; } } document . write ( ans ) ; } var str = " " ; var k = 2 ; var charArray = [ ' ' , ' ' ] ; numberofsubstrings ( str , k , charArray ) ;


"Count of triplets in an array that satisfy the given conditions | Javascript implementation of the approach ; All possible solutions of the equation 1 / a + 1 / b + 1 / c = 1 ; Function to find the triplets ; Storing indices of the elements ; Check if y can act as the middle element of triplet with the given solution of 1 / a + 1 / b + 1 / c = 1 ; Binary search to find the number of possible values of the first element ; Binary search to find the number of possible values of the third element ; Contribution to the answer would be the multiplication of the possible values for the first and the third element ; Driver code"
How can the above be solved in JS?

var MAX = 100001 var ROW = 10 var COl = 3 var indices = Array . from ( Array ( MAX ) , ( ) => new Array ( ) ) ; var test = [ [ 2 , 3 , 6 ] , [ 2 , 4 , 4 ] , [ 2 , 6 , 3 ] , [ 3 , 2 , 6 ] , [ 3 , 3 , 3 ] , [ 3 , 6 , 2 ] , [ 4 , 2 , 4 ] , [ 4 , 4 , 2 ] , [ 6 , 2 , 3 ] , [ 6 , 3 , 2 ] ] ; function find_triplet ( array , n ) { var answer = 0 ; for ( var i = 0 ; i < n ; i ++ ) { indices [ array [ i ] ] . push ( i ) ; } for ( var i = 0 ; i < n ; i ++ ) { var y = array [ i ] ; for ( var j = 0 ; j < ROW ; j ++ ) { var s = test [ j ] [ 1 ] * y ; if ( s % test [ j ] [ 0 ] != 0 ) continue ; if ( s % test [ j ] [ 2 ] != 0 ) continue ; var x = s / test [ j ] [ 0 ] ; var z = s / test [ j ] [ 2 ] ; if ( x > MAX z > MAX ) continue ; var l = 0 ; var r = indices [ x ] . length - 1 ; var first = - 1 ; while ( l <= r ) { var m = ( l + r ) / 2 ; if ( indices [ x ] [ m ] < i ) { first = m ; l = m + 1 ; } else { r = m - 1 ; } } l = 0 ; r = indices [ z ] . length - 1 ; var third = - 1 ; while ( l <= r ) { var m = ( l + r ) / 2 ; if ( indices [ z ] [ m ] > i ) { third = m ; r = m - 1 ; } else { l = m + 1 ; } } if ( first != - 1 && third != - 1 ) { answer += ( first + 1 ) * ( indices [ z ] . length - third ) ; } } } return answer ; } var array = [ 2 , 4 , 5 , 6 , 7 ] ; var n = array . length ; document . write ( find_triplet ( array , n ) ) ;


"Count pairs ( i , j ) from arrays arr [ ] & brr [ ] such that arr [ i ] | Function to count the pairs such that given condition is satisfied ; Stores the sum of element at each corresponding index ; Find the sum of each index of both array ; Stores frequency of each element present in sumArr ; Initialize number of pairs ; Add possible vaid pairs ; Return Number of Pairs ; Given array arr [ ] and brr [ ] ; Size of given array ; Function calling"
How can the above be solved in JS?

function CountPairs ( a , b , n ) { var C = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { C [ i ] = a [ i ] + b [ i ] ; } var freqCount = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( freqCount . has ( C [ i ] ) ) freqCount . set ( C [ i ] , freqCount . get ( C [ i ] ) + 1 ) else freqCount . set ( C [ i ] , 1 ) } var NoOfPairs = 0 ; freqCount . forEach ( ( value , key ) => { var y = value ; NoOfPairs = NoOfPairs + y * ( y - 1 ) / 2 ; } ) ; document . write ( NoOfPairs ) ; } var arr = [ 1 , 4 , 20 , 3 , 10 , 5 ] ; var brr = [ 9 , 6 , 1 , 7 , 11 , 6 ] ; var N = arr . length ; CountPairs ( arr , brr , N ) ;


"Count pairs of equal elements possible by excluding each array element once | Function to count the number of required pairs for every array element ; Initialize a map ; Update the frequency of every element ; Stores the count of pairs ; Traverse the map ; Count the number of ways to select pairs consisting of equal elements only ; Traverse the array ; Print the count for every array element ; Given array ; Size of the array"
How can the above be solved in JS?

function countEqualElementPairs ( arr , N ) { var mp = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . set ( arr [ i ] , 1 ) ; } } var total = 0 ; mp . forEach ( ( value , key ) => { total += ( value * ( value - 1 ) ) / 2 ; } ) ; for ( var i = 0 ; i < N ; i ++ ) { document . write ( total - ( mp . get ( arr [ i ] ) - 1 ) + " " ) ; } } var arr = [ 1 , 1 , 2 , 1 , 2 ] ; var N = arr . length ; countEqualElementPairs ( arr , N ) ;


"Count pairs with given sum | Set 2 | Function to return the count of pairs from arr [ ] with the given sum ; To store the count of pairs ; Sort the given array ; Take two pointers ; If sum is greater ; If sum is lesser ; If sum is equal ; Find the frequency of arr [ i ] ; Find the frequency of arr [ j ] ; If arr [ i ] and arr [ j ] are same then remove the extra number counted ; Return the required answer ; Driver code"
How can the above be solved in JS?

function pairs_count ( arr , n , sum ) { let ans = 0 ; arr . sort ( ) ; let i = 0 , j = n - 1 ; while ( i < j ) { if ( arr [ i ] + arr [ j ] < sum ) i ++ ; else if ( arr [ i ] + arr [ j ] > sum ) j -- ; else { let x = arr [ i ] , xx = i ; while ( i < j && arr [ i ] == x ) i ++ ; let y = arr [ j ] , yy = j ; while ( j >= i && arr [ j ] == y ) j -- ; if ( x == y ) { let temp = i - xx + yy - j - 1 ; ans += ( temp * ( temp + 1 ) ) / 2 ; } else ans += ( i - xx ) * ( yy - j ) ; } } return ans ; } let arr = [ 1 , 5 , 7 , 5 , - 1 ] ; let n = arr . length ; let sum = 6 ; document . write ( pairs_count ( arr , n , sum ) ) ;


"Count positions such that all elements before it are greater | Function to count positions such that all elements before it are greater ; Count is initially 1 for the first element ; Initial Minimum ; Traverse the array ; If current element is new minimum ; Update minimum ; Increment count ; Driver Code"
How can the above be solved in JS?

function getPositionCount ( a , n ) { var count = 1 ; var min = a [ 0 ] ; for ( var i = 1 ; i < n ; i ++ ) { if ( a [ i ] <= min ) { min = a [ i ] ; count ++ ; } } return count ; } var a = [ 5 , 4 , 6 , 1 , 3 , 1 ] ; var n = a . length ; document . write ( getPositionCount ( a , n ) ) ;


"Count possible decoding of a given digit sequence with hidden characters | JavaScript program for the above approach ; If s [ i ] = = ' * ' there can be 9 possible values of * ; If previous character is 1 then words that can be formed are K ( 11 ) , L ( 12 ) , M ( 13 ) , N ( 14 ) O ( 15 ) , P ( 16 ) , Q ( 17 ) , R ( 18 ) , S ( 19 ) ; If previous character is 2 then the words that can be formed are U ( 21 ) , V ( 22 ) , W ( 23 ) , X ( 24 ) Y ( 25 ) , Z ( 26 ) ; If the previous digit is * then all 15 2 - digit characters can be formed ; If s [ i ] != ' * ' ; Adding first in second if s [ i - 1 ] = 1 ; Adding first in second if s [ i - 1 ] = = 2 and s [ i ] <= '6' ; if s [ i - 1 ] == ' * ' the union of above 2 cases has to be done ; Driver Code"
How can the above be solved in JS?

let M = 1000000007 ; function waysOfDecoding ( s ) { let first = 1 , second = s [ 0 ] == ' ' ? 9 : s [ 0 ] == ' ' ? 0 : 1 ; for ( let i = 1 ; i < s . length ; i ++ ) { let temp = second ; if ( s [ i ] == ' ' ) { second = 9 * second ; if ( s [ i - 1 ] == ' ' ) second = ( second + 9 * first ) % M ; else if ( s [ i - 1 ] == ' ' ) second = ( second + 6 * first ) % M ; else if ( s [ i - 1 ] == ' ' ) second = ( second + 15 * first ) % M ; } else { second = s [ i ] != ' ' ? second : 0 ; if ( s [ i - 1 ] == ' ' ) second = ( second + first ) % M ; else if ( s [ i - 1 ] == ' ' && s [ i ] <= ' ' ) second = ( second + first ) % M ; else if ( s [ i - 1 ] == ' ' ) second = ( second + ( s [ i ] <= ' ' ? 2 : 1 ) * first ) % M ; } first = temp ; } return second ; } let s = " " ; document . write ( waysOfDecoding ( s ) ) ;


"Count possible decoding of a given digit sequence with hidden characters | Javascript program for the above approach ; check the first character of the string if it is ' * ' then 9 ways ; traverse the string ; If s [ i ] = = ' * ' there can be 9 possible values of * ; If previous character is 1 then words that can be formed are K ( 11 ) , L ( 12 ) , M ( 13 ) , N ( 14 ) O ( 15 ) , P ( 16 ) , Q ( 17 ) , R ( 18 ) , S ( 19 ) ; If previous character is 2 then the words that can be formed are U ( 21 ) , V ( 22 ) , W ( 23 ) , X ( 24 ) Y ( 25 ) , Z ( 26 ) ; If the previous digit is * then all 15 2 - digit characters can be formed ; taking the value from previous step ; If previous character is 1 then the i - 1 th character and ith character can be decoded in a single character therefore , adding dp [ i - 1 ] . ; If previous character is 2 and ith character is less than 6 then the i - 1 th character and ith character can be decoded in a single character therefore , adding dp [ i - 1 ] . ; If previous character is * then it will contain the above 2 cases ; Driver code"
How can the above be solved in JS?


"Count quadruples of given type from given array | Function to find the count of the subsequence of given type ; Stores the count of quadruples ; Generate all possible combinations of quadruples ; Check if 1 st element is equal to 3 rd element ; Check if 2 nd element is equal to 4 th element ; Driver Code"
How can the above be solved in JS?

function countSubsequece ( a , n ) { let i , j , k , l ; let answer = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { for ( k = j + 1 ; k < n ; k ++ ) { for ( l = k + 1 ; l < n ; l ++ ) { if ( a [ j ] == a [ l ] && a [ i ] == a [ k ] ) { answer ++ ; } } } } } return answer ; } let a = [ 1 , 2 , 3 , 2 , 1 , 3 , 2 ] ; document . write ( countSubsequece ( a , 7 ) ) ;


"Count quadruples of given type from given array | Javascript program of the above approach ; lcount [ i ] [ j ] : Stores the count of i on left of index j ; rcount [ i ] [ j ] : Stores the count of i on right of index j ; Function to count unique elements on left and right of any index ; Find the maximum array element ; Calculate prefix sum of counts of each value ; Calculate suffix sum of counts of each value ; Function to count quadruples of the required type ; Driver Code"
How can the above be solved in JS?

let maxN = 2002 ; let lcount = new Array ( maxN ) ; let rcount = new Array ( maxN ) ; for ( let i = 0 ; i < maxN ; i ++ ) { lcount [ i ] = new Array ( maxN ) ; rcount [ i ] = new Array ( maxN ) ; for ( let j = 0 ; j < maxN ; j ++ ) { lcount [ i ] [ j ] = 0 ; rcount [ i ] [ j ] = 0 ; } } function fill_counts ( a , n ) { let i , j ; let maxA = a [ 0 ] ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] > maxA ) { maxA = a [ i ] ; } } for ( i = 0 ; i < n ; i ++ ) { lcount [ a [ i ] ] [ i ] = 1 ; rcount [ a [ i ] ] [ i ] = 1 ; } for ( i = 0 ; i <= maxA ; i ++ ) { for ( j = 1 ; j < n ; j ++ ) { lcount [ i ] [ j ] = lcount [ i ] [ j - 1 ] + lcount [ i ] [ j ] ; } for ( j = n - 2 ; j >= 0 ; j -- ) { rcount [ i ] [ j ] = rcount [ i ] [ j + 1 ] + rcount [ i ] [ j ] ; } } } function countSubsequence ( a , n ) { let i , j ; fill_counts ( a , n ) ; let answer = 0 ; for ( i = 1 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n - 1 ; j ++ ) { answer += lcount [ a [ j ] ] [ i - 1 ] * rcount [ a [ i ] ] [ j + 1 ] ; } } return answer ; } let a = [ 1 , 2 , 3 , 2 , 1 , 3 , 2 ] ; document . write ( countSubsequence ( a , a . length ) ) ;


"Count removal of pairs required to be empty all Balanced Parenthesis subsequences | Function to find the maximum count of pairs required to be removed such that subsequence of string does not contain any valid parenthesis ; Stores count of pairs of balanced parenthesis ; Stores count of curly balanced parenthesis ; Stores count of small balanced parenthesis ; Stores count of square balanced parenthesis ; Iterate over characters of the string ; Update cntCurly ; Update cntSml ; Update cntSqr ; Update cntCurly ; Update cntPairs ; Update cntSml ; Update cntPairs ; Update cntSml ; Update cntPairs ; Given String ; Function call"
How can the above be solved in JS?

function cntBalancedParenthesis ( s , N ) { var cntPairs = 0 ; var cntCurly = 0 ; var cntSml = 0 ; var cntSqr = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' ' ) { cntCurly ++ ; } else if ( s . charAt ( i ) == ' ' ) { cntSml ++ ; } else if ( s . charAt ( i ) == ' ' ) { cntSqr ++ ; } else if ( s . charAt ( i ) == ' ' && cntCurly > 0 ) { cntCurly -- ; cntPairs ++ ; } else if ( s . charAt ( i ) == ' ' && cntSml > 0 ) { cntSml -- ; cntPairs ++ ; } else if ( s . charAt ( i ) == ' ' && cntSqr > 0 ) { cntSqr -- ; cntPairs ++ ; } } document . write ( cntPairs ) ; } var s = " " ; var N = s . length ; cntBalancedParenthesis ( s , N ) ;


"Count subarrays having an equal count of 0 s and 1 s segregated | Function to count subarrays having equal count of 0 s and 1 s with all 0 s and all 1 s grouped together ; Stores the count ; Initialize cur with first element ; If the next element is same as the current element ; Increment count ; Update curr ; Iterate over the array count ; Consider the minimum ; Given [ ] arr ; Function Call"
How can the above be solved in JS?

function countSubarrays ( A , N ) { var res = 0 ; var curr = A [ 0 ] ; var cnt = [ ] ; cnt . fill ( 1 ) for ( var c = 1 ; c < N ; c ++ ) { if ( A == curr ) cnt [ cnt . length - 1 ] ++ ; else curr = A ; cnt . push ( 1 ) ; } for ( var i = 1 ; i < cnt . length ; i ++ ) { res += Math . min ( cnt [ i - 1 ] , cnt [ i ] ) ; } document . write ( res ) ; } var A = [ 1 , 1 , 0 , 0 , 1 , 0 ] ; var N = A . length ; countSubarrays ( A , N ) ;


"Count subarrays having an equal count of 0 s and 1 s segregated | Function to count subarrays having equal count of 0 s and 1 s with all 0 s and all 1 s grouped together ; Stores the count of subarrays ; If current element is different from the next array element ; Increment count ; Count the frequency of 1 s and 0 s ; Increment count ; Prlet the final count ; Driver Code ; Function Call"
How can the above be solved in JS?

function countSubarrays ( A , N ) { let ans = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] != A [ i + 1 ] ) { ans ++ ; for ( let j = i - 1 , k = i + 2 ; j >= 0 && k < N && A [ j ] == A [ i ] && A [ k ] == A [ i + 1 ] ; j -- , k ++ ) { ans ++ ; } } } document . write ( ans + " " ) ; } let A = [ 1 , 1 , 0 , 0 , 1 , 0 ] ; let N = A . length ; countSubarrays ( A , N ) ;


"Count substrings that starts with character X and ends with character Y | function to count substrings starting with character X and ending with character Y ; to store total count of required substrings ; to store count of character ' x ' up to the point the string ' str ' has been traversed so far ; traverse ' str ' form left to right ; if true , increment ' count _ x ' ; if true accumulate ' count _ x ' to ' tot _ count ' ; required count ; Driver code"
How can the above be solved in JS?

function countSubstr ( str , n , x , y ) { var tot_count = 0 ; var count_x = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] === x ) count_x ++ ; if ( str [ i ] === y ) tot_count += count_x ; } return tot_count ; } var str = " " ; var n = str . length ; var x = " " , y = " " ; document . write ( " " + countSubstr ( str , n , x , y ) ) ;


"Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element | Function to count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element ; Stores xor of odd and even indexed elements from the end ; Stores xor of odd and even indexed elements from the start ; Stores the required count ; Traverse the array in reverse ; If i is odd ; If i is even ; Traverse the array ; If i is odd ; If i is even ; Removing arr [ i ] , post_even stores XOR of odd indexed elements ; Removing arr [ i ] , post_odd stores XOR of even indexed elements ; Check if they are equal ; If i is odd , xor it with curr_odd ; If i is even , xor it with curr_even ; Finally print res ; Given array ; Given size ; Function call"
How can the above be solved in JS?

function Remove_one_element ( arr , n ) { let post_odd = 0 , post_even = 0 ; let curr_odd = 0 , curr_even = 0 ; let res = 0 ; for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( i % 2 != 0 ) post_odd ^= arr [ i ] ; else post_even ^= arr [ i ] ; } for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 != 0 ) post_odd ^= arr [ i ] ; else post_even ^= arr [ i ] ; let X = curr_odd ^ post_even ; let Y = curr_even ^ post_odd ; if ( X == Y ) res ++ ; if ( i % 2 != 0 ) curr_odd ^= arr [ i ] ; else curr_even ^= arr [ i ] ; } document . write ( res ) ; } let arr = [ 1 , 0 , 1 , 0 , 1 ] ; let N = arr . length ; Remove_one_element ( arr , N ) ;


"Count ways to make sum of odd and even indexed elements equal by removing an array element | Function to count array indices whose removal makes sum of odd and even indexed elements equal ; If size of the array is 1 ; If size of the array is 2 ; Stores sum of even - indexed elements of the given array ; Stores sum of odd - indexed elements of the given array ; Traverse the array ; If i is an even number ; Update sumEven ; If i is an odd number ; Update sumOdd ; Stores sum of even - indexed array elements till i - th index ; Stores sum of odd - indexed array elements till i - th index ; Stores count of indices whose removal makes sum of odd and even indexed elements equal ; Stores sum of even - indexed elements after removing the i - th element ; Stores sum of odd - indexed elements after removing the i - th element ; Traverse the array ; If i is an odd number ; Update currOdd ; Update newEvenSum ; Update newOddSum ; If i is an even number ; Update currEven ; Update newOddSum ; Update newEvenSum ; If newEvenSum is equal to newOddSum ; Increase the count ; If sum of even - indexed and odd - indexed elements is equal by removing the first element ; Increase the count ; If length of the array is an odd number ; If sum of even - indexed and odd - indexed elements is equal by removing the last element ; Increase the count ; If length of the array is an even number ; If sum of even - indexed and odd - indexed elements is equal by removing the last element ; Increase the count ; Driver Code"
How can the above be solved in JS?

function cntIndexesToMakeBalance ( arr , n ) { if ( n == 1 ) { return 1 ; } if ( n == 2 ) return 0 ; let sumEven = 0 ; let sumOdd = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { sumEven += arr [ i ] ; } else { sumOdd += arr [ i ] ; } } let currOdd = 0 ; let currEven = arr [ 0 ] ; let res = 0 ; let newEvenSum = 0 ; let newOddSum = 0 ; for ( let i = 1 ; i < n - 1 ; i ++ ) { if ( i % 2 ) { currOdd += arr [ i ] ; newEvenSum = currEven + sumOdd - currOdd ; newOddSum = currOdd + sumEven - currEven - arr [ i ] ; } else { currEven += arr [ i ] ; newOddSum = currOdd + sumEven - currEven ; newEvenSum = currEven + sumOdd - currOdd - arr [ i ] ; } if ( newEvenSum == newOddSum ) { res ++ ; } } if ( sumOdd == sumEven - arr [ 0 ] ) { res ++ ; } if ( n % 2 == 1 ) { if ( sumOdd == sumEven - arr [ n - 1 ] ) { res ++ ; } } else { if ( sumEven == sumOdd - arr [ n - 1 ] ) { res ++ ; } } return res ; } let arr = [ 1 , 1 , 1 ] ; let n = arr . length ; document . write ( cntIndexesToMakeBalance ( arr , n ) ) ;


"Count ways to reach the nth stair using step 1 , 2 or 3 | A recursive function used by countWays ; Declaring three variables and holding the ways for first three stairs ; Fourth variable ; Starting from 4 as already counted for 3 stairs ; Driver code"
How can the above be solved in JS?

function countWays ( n ) { var a = 1 , b = 2 , c = 4 ; var d = 0 ; if ( n == 0 n == 1 n == 2 ) return n ; if ( n == 3 ) return c ; for ( var i = 4 ; i <= n ; i ++ ) { d = c + b + a ; a = b ; b = c ; c = d ; } return d ; } var n = 4 ; document . write ( countWays ( n ) ) ;


"Count ways to reach the nth stair using step 1 , 2 or 3 | A recursive function used by countWays ; Driver Code"
How can the above be solved in JS?

function countWays ( n ) { let res = new Array ( n + 2 ) ; res [ 0 ] = 1 ; res [ 1 ] = 1 ; res [ 2 ] = 2 ; for ( let i = 3 ; i <= n ; i ++ ) res [ i ] = res [ i - 1 ] + res [ i - 2 ] + res [ i - 3 ] ; return res [ n ] ; } let n = 4 ; document . write ( countWays ( n ) ) ;


"Count ways to reach the nth stair using step 1 , 2 or 3 | Returns count of ways to reach n - th stair using 1 or 2 or 3 steps . ; Driver code"
How can the above be solved in JS?

function findStep ( n ) { if ( n == 1 n == 0 ) return 1 ; else if ( n == 2 ) return 2 ; else return findStep ( n - 3 ) + findStep ( n - 2 ) + findStep ( n - 1 ) ; } let n = 4 ; document . write ( findStep ( n ) ) ;


"Count ways to split array into pair of subsets with difference between their sum equal to K | Javascript program for the above approach ; To store the states of DP ; Function to find count of subsets with a given sum ; Base case ; If an already computed subproblem occurs ; Set the state as solved ; Recurrence relation ; Function to count ways to split array into pair of subsets with difference K ; Store the total sum of element of the array ; Traverse the array ; Calculate sum of array elements ; Store the required sum ; Print the number of subsets with sum equal to S1 ; Driver Code ; Function Call"
How can the above be solved in JS?

var maxN = 20 ; var maxSum = 50 ; var minSum = 50 ; var base = 50 ; var dp = Array . from ( Array ( maxN ) , ( ) => Array ( maxSum + minSum ) ) ; var v = Array . from ( Array ( maxN ) , ( ) => Array ( maxSum + minSum ) ) ; function findCnt ( arr , i , required_sum , n ) { if ( i == n ) { if ( required_sum == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ required_sum + base ] ) return dp [ i ] [ required_sum + base ] ; v [ i ] [ required_sum + base ] = 1 ; dp [ i ] [ required_sum + base ] = findCnt ( arr , i + 1 , required_sum , n ) + findCnt ( arr , i + 1 , required_sum - arr [ i ] , n ) ; return dp [ i ] [ required_sum + base ] ; } function countSubsets ( arr , K , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } var S1 = ( sum + K ) / 2 ; document . write ( findCnt ( arr , 0 , S1 , n ) ) ; } var arr = [ 1 , 1 , 2 , 3 ] ; var N = arr . length ; var K = 1 ; countSubsets ( arr , K , N ) ;