codeparrot/xlcost-text-to-code huggingface dataset:
Number of even substrings in a string of digits | Return the even number substrings . ; If current digit is even , add count of substrings ending with it . The count is ( i + 1 ) ; Driver code
function evenNumSubstring ( str ) { let len = str . length ; let count = 0 ; for ( let i = 0 ; i < len ; i ++ ) { let temp = str [ i ] - ' ' ; if ( temp % 2 == 0 ) count += ( i + 1 ) ; } return count ; } let str = " " ; document . write ( evenNumSubstring ( str ) ) ;
Find largest word in dictionary by deleting some characters of given string | Returns true if str1 [ ] is a subsequence of str2 [ ] . m is length of str1 and n is length of str2 ; Traverse str2 and str1 , and compare current character of str2 with first unmatched char of str1 , if matched then move ahead in str1 ; If all characters of str1 were found in str2 ; Returns the longest string in dictionary which is a subsequence of str . ; Traverse through all words of dictionary ; If current word is subsequence of str and is largest such word so far . ; Return longest string ; Driver program to test above function
function isSubSequence ( str1 , str2 ) { var m = str1 . length , n = str2 . length ; for ( var i = 0 ; i < n && j < m ; i ++ ) if ( str1 [ j ] == str2 [ i ] ) j ++ ; return ( j == m ) ; } function findLongestString ( dict , str ) { var result = " " ; var length = 0 ; dict . forEach ( word => { if ( length < word . length && isSubSequence ( word , str ) ) { result = word ; length = word . length ; } } ) ; return result ; } var dict = [ " " , " " , " " , " " ] ; var str = " " ; document . write ( findLongestString ( dict , str ) ) ;
Compare two Version numbers | Method to compare two versions . Returns 1 if v2 is smaller , - 1 if v1 is smaller , 0 if equal ; vnum stores each numeric part of version ; loop until both string are processed ; storing numeric part of version 1 in vnum1 ; storing numeric part of version 2 in vnum2 ; if equal , reset variables and go for next numeric part ; Driver method to check above comparison function
function versionCompare ( v1 , v2 ) { var vnum1 = 0 , vnum2 = 0 ; for ( var i = 0 , j = 0 ; ( i < v1 . length j < v2 . length ) ; ) { while ( i < v1 . length && v1 [ i ] != ' ' ) { vnum1 = vnum1 * 10 + ( v1 [ i ] - ' ' ) ; i ++ ; } while ( j < v2 . length && v2 [ j ] != ' ' ) { vnum2 = vnum2 * 10 + ( v2 [ j ] - ' ' ) ; j ++ ; } if ( vnum1 > vnum2 ) return 1 ; if ( vnum2 > vnum1 ) return - 1 ; vnum1 = vnum2 = 0 ; i ++ ; j ++ ; } return 0 ; } var version1 = " " ; var version2 = " " ; if ( versionCompare ( version1 , version2 ) < 0 ) document . write ( version1 + " " + " " ) ; else if ( versionCompare ( version1 , version2 ) > 0 ) document . write ( version2 + " " + " " ) ; else document . write ( " " + " " ) ;
Repeated subsequence of length 2 or more | Javascript program to check if any repeated subsequence exists in the string ; A function to check if a string str is palindrome ; l and h are leftmost and rightmost corners of str Keep comparing characters while they are same ; The main function that checks if repeated subsequence exists in the string ; Find length of input string ; Create an array to store all characters and their frequencies in str [ ] ; Traverse the input string and store frequencies of all characters in freq [ ] array . ; If the character count is more than 2 we found a repetition ; In - place remove non - repeating characters from the string ; check if the resultant string is palindrome ; special case - if length is odd return true if the middle character is same as previous one ; It is checked so that StringIndexOutOfBounds can be avoided ; return false if string is a palindrome ; return true if string is not a palindrome ; Driver Code
let MAX_CHAR = 256 ; function isPalindrome ( str , l , h ) { while ( h > l ) if ( str [ l ++ ] != str [ h -- ] ) return false ; return true ; } function check ( str ) { let n = str . length ; let freq = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) ] ++ ; if ( freq [ str [ i ] . charCodeAt ( 0 ) ] > 2 ) return true ; } let k = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( freq [ str [ i ] . charCodeAt ( 0 ) ] > 1 ) str . replace ( str [ k ++ ] , str [ i ] ) ; str . replace ( str [ k ] , ' \0 ' ) ; if ( isPalindrome ( str , 0 , k - 1 ) ) { if ( ( k & 1 ) == 1 ) { if ( k / 2 >= 1 ) return ( str [ Math . floor ( k / 2 ) ] == str [ Math . floor ( k / 2 ) - 1 ] ) ; } return false ; } return true ; } let str = " " ; if ( check ( str ) ) document . write ( " " ) ; else document . write ( " " + " " ) ;
How to find Lexicographically previous permutation ? | Function to compute the previous permutation ; Find index of the last element of the string ; Find largest index i such that str [ i - 1 ] > str [ i ] ; If string is sorted in ascending order we 're  at the last permutation ; Note - str [ i . . n ] is sorted in ascending order Find rightmost element 's index  that is less than str[i - 1] ; Swap character at i - 1 with j ; Reverse the substring [ i . . n ] str . reverse ( ) ; ; Driver code
function prevPermutation ( str ) { let n = str . length - 1 ; let i = n ; while ( i > 0 && str [ i - 1 ] . charCodeAt ( 0 ) <= str [ i ] . charCodeAt ( 0 ) ) { i -- ; } if ( i <= 0 ) { return false ; } let j = i - 1 ; while ( j + 1 <= n && str [ j + 1 ] . charCodeAt ( 0 ) <= str [ i - 1 ] . charCodeAt ( 0 ) ) { j ++ ; } let temp = str [ i - 1 ] ; str [ i - 1 ] = str [ j ] ; str [ j ] = temp ; return true ; } let str = " " . split ( " " ) ; if ( prevPermutation ( str ) ) { document . write ( " " + ( str ) . join ( " " ) ) ; } else { document . write ( " " + " " ) ; }
Check if edit distance between two strings is one | Returns true if edit distance between s1 and s2 is one , else false ; Find lengths of given strings ; If difference between lengths is more than 1 , then strings can 't  be at one distance ; Count of edits ; If current characters don 't match ; If length of one string is more , then only possible edit is to remove a character ; If lengths of both strings is same ; Increment count of edits ; If current characters match ; If last character is extra in any string ; Driver code
function isEditDistanceOne ( s1 , s2 ) { let m = s1 . length , n = s2 . length ; if ( Math . abs ( m - n ) > 1 ) return false ; let count = 0 ; let i = 0 , j = 0 ; while ( i < m && j < n ) { if ( s1 [ i ] != s2 [ j ] ) { if ( count == 1 ) return false ; if ( m > n ) i ++ ; else if ( m < n ) j ++ ; else { i ++ ; j ++ ; } count ++ ; } else { i ++ ; j ++ ; } } if ( i < m j < n ) count ++ ; return count == 1 ; } let s1 = " " ; let s2 = " " ; if ( isEditDistanceOne ( s1 , s2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of numbers from range [ L , R ] whose sum of digits is Y | Set 2 | Javascript program for the above approach ; Function to find the sum of digits of numbers in the range [ 0 , X ] ; Check if count of digits in a number greater than count of digits in X ; Check Iif sum of digits of a number is equal to Y ; Check if current subproblem has already been computed ; Stores count of numbers whose sum of digits is Y ; Check if the number exceeds Y or not ; Iterate over all possible values of i - th digits ; Update res ; Return res ; Utility function to count the numbers in the range [ L , R ] whose sum of digits is Y ; Base Case ; Stores numbers in the form of its equivalent String ; Stores overlapping subproblems ; Initialize dp [ ] [ ] [ ] ; Stores count of numbers in the range [ 0 , R ] ; Update str ; Initialize dp [ ] [ ] [ ] ; Stores count of numbers in the range [ 0 , L - 1 ] ; Driver Code
let M = 1000 ; function cntNum ( X , i , sum , tight , dp ) { if ( i >= X . length sum < 0 ) { if ( sum == 0 ) { return 1 ; } return 0 ; } if ( dp [ sum ] [ i ] [ tight ] != - 1 ) { return dp [ sum ] [ i ] [ tight ] ; } let res = 0 ; let end = tight != 0 ? X [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) : 9 ; for ( let j = 0 ; j <= end ; j ++ ) { res += cntNum ( X , i + 1 , sum - j , ( tight > 0 & ( j == end ) ) == true ? 1 : 0 , dp ) ; } return dp [ sum ] [ i ] [ tight ] = res ; } function UtilCntNumRange ( L , R , Y ) { if ( R == 0 && Y == 0 ) { return 1 ; } let str = ( R ) . toString ( ) ; let dp = new Array ( M ) ; for ( let i = 0 ; i < M ; i ++ ) { dp [ i ] = new Array ( M ) ; for ( let j = 0 ; j < M ; j ++ ) { dp [ i ] [ j ] = new Array ( 2 ) ; for ( let k = 0 ; k < 2 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; } } let cntR = cntNum ( str , 0 , Y , 1 , dp ) ; str = ( L - 1 ) . toString ( ) ; for ( let i = 0 ; i < M ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { for ( let k = 0 ; k < 2 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; } } let cntL = cntNum ( str , 0 , Y , 1 , dp ) ; return ( cntR - cntL ) ; } let L = 20 , R = 10000 , Y = 14 ; document . write ( UtilCntNumRange ( L , R , Y ) ) ;
Remove spaces from a given string | Function to remove all spaces from a given string ; Driver Code
function removeSpace ( str ) { str = str . replace ( / \s / g , ' ' ) return str ; } var str = " " ; document . write ( removeSpace ( str ) ) ;
Given a binary string , count number of substrings that start and end with 1. | A simple javascript program to count number of substrings starting and ending with 1 ; Initialize result ; Pick a starting point ; Search for all possible ending point ; Driver program to test the above function
function countSubStr ( str , n ) { let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( str [ j ] == ' ' ) res ++ ; } } } return res ; } let string = " " ; let n = string . length ; document . write ( countSubStr ( string , n ) ) ;
An in | Javascript implementation of above approach ; A utility function to reverse String str [ low . . high ] ; Cycle leader algorithm to move all even positioned elements at the end . ; odd index ; even index ; Keep the back - up of element at new position ; The main function to transform a String . This function mainly uses cycleLeader ( ) to transform ; Step 1 : Find the largest prefix subarray of the form 3 ^ k + 1 ; Step 2 : Apply cycle leader algorithm for the largest subarrau ; Step 4.1 : Reverse the second half of first subarray ; Step 4.2 : Reverse the first half of second sub - String . ; Step 4.3 Reverse the second half of first sub - String and first half of second sub - String together ; Increase the length of first subarray ; Driver code
let str ; function reverse ( low , high ) { while ( low < high ) { let t = str [ low ] ; str [ low ] = str [ high ] ; str [ high ] = t ; ++ low ; -- high ; } } function cycleLeader ( shift , len ) { let j ; let item ; for ( let i = 1 ; i < len ; i *= 3 ) { j = i ; item = str [ j + shift ] ; do { if ( j % 2 == 1 ) j = Math . floor ( len / 2 ) + Math . floor ( j / 2 ) ; else j = Math . floor ( j / 2 ) ; let t = str [ j + shift ] ; str [ j + shift ] = item ; item = t ; } while ( j != i ) ; } } function moveNumberToSecondHalf ( ) { let k , lenFirst ; let lenRemaining = str . length ; let shift = 0 ; while ( lenRemaining > 0 ) { k = 0 ; while ( Math . pow ( 3 , k ) + 1 <= lenRemaining ) k ++ ; lenFirst = Math . floor ( Math . pow ( 3 , k - 1 ) ) + 1 ; lenRemaining -= lenFirst ; cycleLeader ( shift , lenFirst ) ; reverse ( Math . floor ( shift / 2 ) , shift - 1 ) ; reverse ( shift , shift + Math . floor ( lenFirst / 2 ) - 1 ) ; reverse ( Math . floor ( shift / 2 ) , shift + Math . floor ( lenFirst / 2 ) - 1 ) ; shift += lenFirst ; } } let st = " " ; str = st . split ( " " ) ; moveNumberToSecondHalf ( ) ; document . write ( str . join ( " " ) ) ;
Remove duplicates from a given string | javascript program to remove duplicate character from character array and prvar in sorted order ; Create a set using String characters excluding ' \0' ; HashSet doesn 't allow repetition of elements ; Driver code
function removeDuplicate ( str , n ) { var s = new Set ( ) ; for ( var i = 0 ; i < n ; i ++ ) s . add ( str [ i ] ) ; for ( const v of s ) { document . write ( v ) ; } } var str = " " ; var n = str . length ; removeDuplicate ( str , n ) ;
Check if K can be obtained by performing arithmetic operations on any permutation of an Array | Function that finds the string of possible combination of operations ; If only one number left then check for result ; If resultant value is K ; Else return 0 ; Choose all combination of numbers and operators and operate them ; Choose the first two and operate it with ' + ' ; Place it to 0 th position ; Place ( n - 1 ) th element on 1 st position ; Evaluate the expression with current combination ; Now , we have N - 1 elements ; Try ' - ' operation ; Evaluate the expression with current combination ; Try reverse ' - ' ; Evaluate the expression with current combination ; Try ' * ' operation ; Evaluate the expression with current combination ; Try ' / ' ; Evaluate the expression with current combination ; Try reverse ' / ' ; Evaluate the expression with current combination ; Backtracking Step ; Return 0 if there doesnt exist any combination that gives K ; Function that finds the possible combination of operation to get the resultant value K ; Store the resultant operation ; Function call ; Given array arr [ ] ; Resultant value K ; Function call
function find ( v , n , dest , s ) { if ( n == 1 ) { if ( Math . abs ( v [ 0 ] - dest ) <= 0.0000001 ) { document . write ( s + ( dest ) . toString ( ) + " " ) ; return true ; } return false ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { let a = v [ i ] , b = v [ j ] ; let p = s ; v [ i ] = a + b ; v [ j ] = v [ n - 1 ] ; s = ( s + a . toString ( ) + ' ' + b . toString ( ) + " " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; v [ i ] = a - b ; v [ j ] = v [ n - 1 ] ; s = ( s + a . toString ( ) + ' ' + b . toString ( ) + " " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; v [ i ] = b - a ; v [ j ] = v [ n - 1 ] ; s = ( s + b . toString ( ) + ' ' + a . toString ( ) + " " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; v [ i ] = a * b ; v [ j ] = v [ n - 1 ] ; s = ( s + a . toString ( ) + ' ' + b . toString ( ) + " " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; if ( b != 0 ) { v [ i ] = a / b ; v [ j ] = v [ n - 1 ] ; s = ( s + a . toString ( ) + ' ' + b . toString ( ) + " " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; } s = p ; if ( a != 0 ) { v [ i ] = b / a ; v [ j ] = v [ n - 1 ] ; s = ( s + b . toString ( ) + ' ' + a . toString ( ) + " " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; } s = p ; v [ i ] = a ; v [ j ] = b ; } } return false ; } function checkPossibleOperation ( arr , k ) { let s = " " ; if ( ! find ( arr , arr . length , K , s ) ) { document . write ( " " ) ; } } let arr = [ 2 , 0 , 0 , 2 ] ; let K = 4 ; checkPossibleOperation ( arr , K ) ;
Sudoku | Backtracking | N is the size of the 2D matrix N * N ; A utility function to print grid ; Check whether it will be legal to assign num to the given row , col ; Check if we find the same num in the similar row , we return false ; Check if we find the same num in the similar column , we return false ; Check if we find the same num in the particular 3 * 3 matrix , we return false ; Takes a partially filled - in grid and attempts to assign values to all unassigned locations in such a way to meet the requirements for Sudoku solution ( non - duplication across rows , columns , and boxes ) ; If we have reached the 8 th row and 9 th column ( 0 indexed matrix ) , we are returning true to avoid further backtracking ; Check if column value becomes 9 , we move to next row and column start from 0 ; Check if the current position of the grid already contains value > 0 , we iterate for next column ; Check if it is safe to place the num ( 1 - 9 ) in the given row , col -> we move to next column ; assigning the num in the current ( row , col ) position of the grid and assuming our assigned num in the position is correct ; Checking for next possibility with next column ; removing the assigned num , since our assumption was wrong , and we go for next assumption with diff num value ; 0 means unassigned cells
let N = 9 ; function print ( grid ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) document . write ( grid [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } function isSafe ( grid , row , col , num ) { for ( let x = 0 ; x <= 8 ; x ++ ) if ( grid [ row ] [ x ] == num ) return false ; for ( let x = 0 ; x <= 8 ; x ++ ) if ( grid [ x ] [ col ] == num ) return false ; let startRow = row - row % 3 , startCol = col - col % 3 ; for ( let i = 0 ; i < 3 ; i ++ ) for ( let j = 0 ; j < 3 ; j ++ ) if ( grid [ i + startRow ] [ j + startCol ] == num ) return false ; return true ; } function solveSuduko ( grid , row , col ) { if ( row == N - 1 && col == N ) return true ; if ( col == N ) { row ++ ; col = 0 ; } if ( grid [ row ] [ col ] != 0 ) return solveSuduko ( grid , row , col + 1 ) ; for ( let num = 1 ; num < 10 ; num ++ ) { if ( isSafe ( grid , row , col , num ) ) { grid [ row ] [ col ] = num ; if ( solveSuduko ( grid , row , col + 1 ) ) return true ; } grid [ row ] [ col ] = 0 ; } return false ; } let grid = [ [ 3 , 0 , 6 , 5 , 0 , 8 , 4 , 0 , 0 ] , [ 5 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 8 , 7 , 0 , 0 , 0 , 0 , 3 , 1 ] , [ 0 , 0 , 3 , 0 , 1 , 0 , 0 , 8 , 0 ] , [ 9 , 0 , 0 , 8 , 6 , 3 , 0 , 0 , 5 ] , [ 0 , 5 , 0 , 0 , 9 , 0 , 6 , 0 , 0 ] , [ 1 , 3 , 0 , 0 , 0 , 0 , 2 , 5 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 4 ] , [ 0 , 0 , 5 , 2 , 0 , 6 , 3 , 0 , 0 ] ] if ( solveSuduko ( grid , 0 , 0 ) ) print ( grid ) else document . write ( " " )
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | function to check for the given sum in the array ; checking for condition ; Driver Code
function printpairs ( arr , sum ) { let s = new Set ( ) ; for ( let i = 0 ; i < arr . length ; ++ i ) { let temp = sum - arr [ i ] ; if ( s . has ( temp ) ) { document . write ( " " + sum + " " + arr [ i ] + " " + temp + " " ) ; } s . add ( arr [ i ] ) ; } } let A = [ 1 , 4 , 45 , 6 , 10 , 8 ] ; let n = 16 ; printpairs ( A , n ) ;
Find remainder when a number A raised to N factorial is divided by P | Function to calculate ( A ^ N ! ) % P in O ( log y ) ; Initialize result ; Update x if it is more than or Equal to p ; In case x is divisible by p ; ; If y is odd , multiply x with result ; y must be even now ; Returning modular power ; Function to calculate resultant remainder ; Initializing ans to store final remainder ; Calculating remainder ; Returning resultant remainder ; Given Input ; Function Call
function power ( x , y , p ) { let res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } function remainder ( n , a , p ) { let ans = a % p ; for ( let i = 1 ; i <= n ; i ++ ) ans = power ( ans , i , p ) ; return ans ; } let A = 2 , N = 1 , P = 2 ; document . write ( remainder ( N , A , P ) + " " ) ;
Find all array elements occurring more than âŒŠN / 3 âŒ ‹ times | Function to find Majority element in an array ; if this element is previously seen , increment count1 . ; if this element is previously seen , increment count2 . ; if current element is different from both the previously seen variables , decrement both the counts . ; Again traverse the array and find the actual counts . ; Driver code
function findMajority ( arr , n ) { var count1 = 0 , count2 = 0 ; var first = 2147483647 , second = 2147483647 ; = var flag = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( first === arr [ i ] ) count1 ++ ; else if ( second === arr [ i ] ) count2 ++ ; else if ( count1 === 0 ) { count1 ++ ; first = arr [ i ] ; } else if ( count2 === 0 ) { count2 ++ ; second = arr [ i ] ; } else { count1 -- ; count2 -- ; } } count1 = 0 ; count2 = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] === first ) count1 ++ ; else if ( arr [ i ] === second ) count2 ++ ; } if ( count1 > n / 3 ) { document . write ( first + " " ) ; flag = 1 ; } if ( count2 > n / 3 ) { document . write ( second + " " ) ; flag = 1 ; } if ( flag === 0 ) { document . write ( " " + " " ) ; } } var arr = [ 2 , 2 , 3 , 1 , 3 , 2 , 1 , 1 ] ; var n = arr . length ; findMajority ( arr , n ) ;
Sum of maximum of all subarrays | Divide and Conquer | JavaScript implementation of the above approach ; Array to store segment tree . In first we will store the maximum of a range In second , we will store index of that range ; Size of array declared global to maintain simplicity in code ; Function to build segment tree ; Base case ; Finding the maximum among left and right child ; Returning the maximum to parent ; Function to perform range - max query in segment tree ; Base cases ; Finding the maximum among left and right child ; Function to find maximum sum subarray ; base case ; range - max query to determine largest in the range . ; divide the array in two parts ; input array ; Size of array ; Builind the segment - tree
class pair { constructor ( first , second ) { this . first = first ; this . second = second ; } } let seg_max = 51 ; let seg_tree = new Array ( seg_max ) ; let n ; function buildMaxTree ( l , r , i , arr ) { if ( l == r ) { seg_tree [ i ] = new pair ( arr [ l ] , l ) ; return seg_tree [ i ] ; } seg_tree [ i ] = max ( buildMaxTree ( l , Math . floor ( ( l + r ) / 2 ) , 2 * i + 1 , arr ) , buildMaxTree ( Math . floor ( ( l + r ) / 2 ) + 1 , r , 2 * i + 2 , arr ) ) ; return seg_tree [ i ] ; } function rangeMax ( l , r , arr , i , sl , sr ) { if ( sr < l sl > r ) return new pair ( Number . MIN_VALUE , - 1 ) ; if ( sl >= l && sr <= r ) return seg_tree [ i ] ; return max ( rangeMax ( l , r , arr , 2 * i + 1 , sl , Math . floor ( ( sl + sr ) / 2 ) ) , rangeMax ( l , r , arr , 2 * i + 2 , Math . floor ( ( sl + sr ) / 2 ) + 1 , sr ) ) ; } function max ( f , s ) { if ( f . first > s . first ) return f ; else return s ; } function maxSumSubarray ( arr , l , r ) { if ( l > r ) return 0 ; let a = rangeMax ( l , r , arr , 0 , 0 , n - 1 ) ; return a . first * ( r - a . second + 1 ) * ( a . second - l + 1 ) + maxSumSubarray ( arr , l , a . second - 1 ) + maxSumSubarray ( arr , a . second + 1 , r ) ; } let arr = [ 1 , 3 , 1 , 7 ] ; n = arr . length ; buildMaxTree ( 0 , n - 1 , 0 , arr ) ; document . write ( maxSumSubarray ( arr , 0 , n - 1 ) ) ;
Modular exponentiation ( Recursive ) | Recursive Javascript program to compute modular power ; Base cases ; If B is even ; If B is odd ; Driver code
function exponentMod ( A , B , C ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; var y ; if ( B % 2 == 0 ) { y = exponentMod ( A , B / 2 , C ) ; y = ( y * y ) % C ; } else { y = A % C ; y = ( y * exponentMod ( A , B - 1 , C ) % C ) % C ; } return parseInt ( ( ( y + C ) % C ) ) ; } var A = 2 , B = 5 , C = 13 ; document . write ( " " + exponentMod ( A , B , C ) ) ;
Find frequency of each element in a limited range array in less than O ( n ) time | JavaScript program to count number of occurrences of each element in the array in O ( n ) time and O ( 1 ) space ; check if the current element is equal to previous element . ; reset the frequency ; print the last element and its frequency ; Driver code
function void findFrequencies ( ele ) { var freq = 1 ; var idx = 1 ; var element = ele [ 0 ] ; while ( idx < ele . length ) { if ( ele [ idx - 1 ] == ele [ idx ] ) { freq ++ ; idx ++ ; } else { document . write ( element + " " + freq ) ; element = ele [ idx ] ; idx ++ ; freq = 1 ; } } document . write ( element + " " + freq ) ; } document . write ( " " ) ; findFrequencies ( new var [ ] { 10 , 20 , 30 , 30 , 30 , 40 , 50 , 50 , 50 , 50 , 70 } ) ;
Modular Exponentiation ( Power in Modular Arithmetic ) | Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Change x to x ^ 2
function power ( x , y ) { let res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; }