codeparrot/xlcost-text-to-code huggingface dataset:
Find one extra character in a string | Javascript program to find extra character in one string ; Determine String with extra character . ; Add character codes of both the strings ; Add last character code of large String . ; Minus the character code of smaller string from the character code of large string . The result will be the extra character code . ; Driver code
function findExtraCharacter ( s1 , s2 ) { let smallStr ; let largeStr ; if ( s1 . length > s2 . length ) { smallStr = s2 ; largeStr = s1 ; } else { smallStr = s1 ; largeStr = s2 ; } let smallStrCodeTotal = 0 ; let largeStrCodeTotal = 0 ; let i = 0 ; for ( ; i < smallStr . length ; i ++ ) { smallStrCodeTotal += smallStr [ i ] . charCodeAt ( 0 ) ; largeStrCodeTotal += largeStr [ i ] . charCodeAt ( 0 ) ; } largeStrCodeTotal += largeStr [ i ] . charCodeAt ( 0 ) ; let intChar = largeStrCodeTotal - smallStrCodeTotal ; return String . fromCharCode ( intChar ) ; } let s1 = " " ; let s2 = " " ; let extraChar = findExtraCharacter ( s1 , s2 ) ; document . write ( " " + extraChar ) ;
Function to copy string ( Iterative and Recursive ) | Function to copy one string to other assuming that other string has enough space . ; Driver code Driver Code
function myCopy ( s1 , s2 ) { let i = 0 ; for ( i = 0 ; i < s1 . length ; i ++ ) s2 [ i ] = s1 [ i ] ; } let s1 = " " ; let s2 = [ ] ; let index = 0 ; myCopy ( s1 , s2 , index ) ; document . write ( s2 . join ( " " ) ) ;
Evaluate an array expression with numbers , + and | Function to find the sum of given array ; if string is empty ; parseInt function to convert string into integer ; parseInt function to convert string into integer ; Find operator ; If operator is equal to ' + ' , add value in sum variable else subtract ; Driver code
function calculateSum ( arr , n ) { if ( n == 0 ) return 0 ; let s = arr [ 0 ] ; let value = parseInt ( s ) ; let sum = value ; for ( let i = 2 ; i < n ; i = i + 2 ) { s = arr [ i ] ; value = parseInt ( s ) ; let operation = arr [ i - 1 ] [ 0 ] ; if ( operation == ' ' ) sum += value ; else sum -= value ; } return sum ; } let arr = [ " " , " " , " " , " " , " " , " " , " " ] ; let n = arr . length ; document . write ( calculateSum ( arr , n ) ) ;
Check if two strings have a common substring | javascript program to heck if two strings have common substring ; function to return true if strings have common substring and no if strings have no common substring ; vector for storing character occurrences ; increment vector index for every character of str1 ; checking common substring of str2 in str1 ; driver program
var MAX_CHAR = 26 ; function twoStrings ( s1 , s2 ) { var v = Array ( MAX_CHAR ) . fill ( 0 ) ; for ( var i = 0 ; i < s1 . length ; i ++ ) v [ s1 [ i ] - ' ' ] = true ; for ( var i = 0 ; i < s2 . length ; i ++ ) if ( v [ s2 [ i ] - ' ' ] ) return true ; return false ; } var str1 = " " ; var str2 = " " ; if ( twoStrings ( str1 , str2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
String with maximum number of unique characters | Function to find string with maximum number of unique characters . ; Index of string with maximum unique characters ; iterate through all strings ; array indicating any alphabet included or not included ; count number of unique alphabets in each string ; keep track of maximum number of alphabets ; prlet result ; Driver code
function LargestString ( na ) { let N = na . length ; let c = new Array ( N ) ; let m = 1 ; for ( let j = 0 ; j < N ; j ++ ) { let character = new Array ( 26 , 0 ) ; for ( let k = 0 ; k < na [ j ] . length ; k ++ ) { let x = na [ j ] [ k ] . charCodeAt ( 0 ) - 65 ; if ( ( na [ j ] [ k ] != ' ' ) && ( character [ x ] == 0 ) ) { c [ j ] ++ ; character [ x ] = 1 ; } } if ( c [ j ] > c [ m ] ) m = j ; } document . write ( na [ m ] ) ; } let na = [ " " , " " , " " , " " , " " ] ; LargestString ( na ) ;
Minimum removal to make palindrome permutation | Javascript Program to find minimum number of removal to make any permutation of the string a palindrome ; function to find minimum removal of characters ; hash to store frequency of each character ; count frequency of each character ; count the odd frequency characters ; if count is - 1 return 0 otherwise return count ; Driver 's Code
var MAX_CHAR = 26 function minRemoval ( str ) { var hash = Array ( MAX_CHAR ) . fill ( 0 ) ; for ( var i = 0 ; str [ i ] ; i ++ ) hash [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; var count = 0 ; for ( var i = 0 ; i < MAX_CHAR ; i ++ ) if ( hash [ i ] % 2 ) count ++ ; return ( count == 0 ) ? 0 : count - 1 ; } var str = " " ; document . write ( minRemoval ( str ) ) ;
Longest Uncommon Subsequence | function to calculate length of longest uncommon subsequence ; Case 1 : If strings are equal ; for case 2 and case 3 ; input strings
function findLUSlength ( a , b ) { if ( a === b ) return 0 ; return Math . max ( a . length , b . length ) ; } var a = " " , b = " " ; document . write ( findLUSlength ( a , b ) ) ;
Interchanging first and second halves of strings | Function to concatenate two different halves of given strings ; Creating new strings by exchanging the first half of a and b . Please refer below for details of substring . https : www . geeksforgeeks . org / java - lang - string - substring - java / ; Driver Code ; Calling function
function swapTwoHalves ( a , b ) { var la = a . length ; var lb = b . length ; var c = a . substring ( 0 , la / 2 ) + b . substring ( lb / 2 , lb ) ; var d = b . substring ( 0 , lb / 2 ) + a . substring ( la / 2 , la ) ; document . write ( c + " " + " " + d ) ; } var a = " " ; var b = " " ; swapTwoHalves ( a , b ) ;
Longest sub | JavaScript implementation to find the length of the longest substring having frequency of each character less than equal to k ; function to find the length of the longest substring having frequency of each character less than equal to k ; hash table to store frequency of each table ; ' start ' index of the current substring ; to store the maximum length ; traverse the string ' str ' ; get the current character as ' ch ' ; increase frequency of ' ch ' in ' freq [ ] ' ; if frequency of ' ch ' becomes more than ' k ' ; update ' maxLen ' ; decrease frequency of each character as they are encountered from the ' start ' index until frequency of ' ch ' is greater than ' k ' ; decrement frequency by '1' ; increment ' start ' ; update maxLen ; required length ; Driver program to test above
var SIZE = 26 ; function longSubstring ( str , k ) { var freq = Array ( SIZE ) . fill ( 0 ) ; var start = 0 ; var maxLen = 0 ; var ch ; var n = str . length ; for ( var i = 0 ; i < n ; i ++ ) { ch = str [ i ] ; freq [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; if ( freq [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] > k ) { if ( maxLen < ( i - start ) ) maxLen = i - start ; while ( freq [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] > k ) { freq [ str [ start ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; start ++ ; } } } if ( maxLen < ( n - start ) ) maxLen = n - start ; return maxLen ; } var str = " " ; var k = 1 ; document . write ( " " + longSubstring ( str , k ) ) ;
Level order traversal with direction change after every two levels | A Binary Tree Node ; Function to print the level order of given binary tree . Direction of printing level order traversal of binary tree changes after every two levels ; For null root ; Maintain a queue for normal level order traversal ; Maintain a stack for printing nodes in reverse order after they are popped out from queue . ; sz is used for storing the count of nodes in a level ; Used for changing the direction of level order traversal ; Used for changing the direction of level order traversal ; Push root node to the queue ; Run this while loop till queue got empty ; Do a normal level order traversal ; For printing nodes from left to right , simply print the nodes in the order in which they are being popped out from the queue . ; For printing nodes from right to left , push the nodes to stack instead of printing them . ; for printing the nodes in order from right to left ; Change the direction of printing nodes after every two levels . ; Let us create binary tree
class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } function modifiedLevelOrder ( node ) { if ( node == null ) return ; if ( node . left == null && node . right == null ) { document . write ( node . data ) ; return ; } let myQueue = [ ] ; let myStack = [ ] ; let temp = null ; let sz ; let ct = 0 ; let rightToLeft = false ; myQueue . push ( node ) ; while ( myQueue . length != 0 ) { ct ++ ; sz = myQueue . length ; for ( let i = 0 ; i < sz ; i ++ ) { temp = myQueue . shift ( ) ; if ( rightToLeft == false ) document . write ( temp . data + " " ) ; else myStack . push ( temp ) ; if ( temp . left != null ) myQueue . push ( temp . left ) ; if ( temp . right != null ) myQueue . push ( temp . right ) ; } if ( rightToLeft == true ) { while ( myStack . length != 0 ) { temp = myStack . pop ( ) ; document . write ( temp . data + " " ) ; } } if ( ct == 2 ) { rightToLeft = ! rightToLeft ; ct = 0 ; } document . write ( " " ) ; } } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . left . left . right = new Node ( 9 ) ; root . left . right . left = new Node ( 3 ) ; root . left . right . right = new Node ( 1 ) ; root . right . left . left = new Node ( 4 ) ; root . right . left . right = new Node ( 2 ) ; root . right . right . left = new Node ( 7 ) ; root . right . right . right = new Node ( 2 ) ; root . left . right . left . left = new Node ( 16 ) ; root . left . right . left . right = new Node ( 17 ) ; root . right . left . right . left = new Node ( 18 ) ; root . right . right . left . right = new Node ( 19 ) ; modifiedLevelOrder ( root ) ;
Minimum cost to connect all cities | Function to find out minimum valued node among the nodes which are not yet included in MST ; Loop through all the values of the nodes which are not yet included in MST and find the minimum valued one . ; Function to find out the MST and the cost of the MST . ; Array to store the parent node of a particular node . ; Array to store key value of each node . ; Boolean Array to hold bool values whether a node is included in MST or not . ; Set all the key values to infinite and none of the nodes is included in MST . ; Start to find the MST from node 0. Parent of node 0 is none so set - 1. key value or minimum cost to reach 0 th node from 0 th node is 0. ; Find the rest n - 1 nodes of MST . ; First find out the minimum node among the nodes which are not yet included in MST . ; Now the uth node is included in MST . ; Update the values of neighbor nodes of u which are not yet included in MST . ; Find out the cost by adding the edge values of MST . ; Input 1 ; Input 2
function minnode ( n , keyval , mstset ) { let mini = Number . MAX_VALUE ; let mini_index = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( mstset [ i ] == false && keyval [ i ] < mini ) { mini = keyval [ i ] ; mini_index = i ; } } return mini_index ; } function findcost ( n , city ) { let parent = Array ( n ) . fill ( 0 ) ; let keyval = Array ( n ) . fill ( 0 ) ; let mstset = Array ( n ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { keyval [ i ] = Number . MAX_VALUE ; mstset [ i ] = false ; } parent [ 0 ] = - 1 ; keyval [ 0 ] = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { let u = minnode ( n , keyval , mstset ) ; mstset [ u ] = true ; for ( let v = 0 ; v < n ; v ++ ) { if ( city [ u ] [ v ] > 0 && mstset [ v ] == false && city [ u ] [ v ] < keyval [ v ] ) { keyval [ v ] = city [ u ] [ v ] ; parent [ v ] = u ; } } } let cost = 0 ; for ( let i = 1 ; i < n ; i ++ ) cost += city [ parent [ i ] ] [ i ] ; document . write ( cost + " " ) ; } let n1 = 5 ; let city1 = [ [ 0 , 1 , 2 , 3 , 4 ] , [ 1 , 0 , 5 , 0 , 7 ] , [ 2 , 5 , 0 , 6 , 0 ] , [ 3 , 0 , 6 , 0 , 0 ] , [ 4 , 7 , 0 , 0 , 0 ] ] ; findcost ( n1 , city1 ) ; let n2 = 6 ; let city2 = [ [ 0 , 1 , 1 , 100 , 0 , 0 ] , [ 1 , 0 , 1 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 0 , 0 , 0 ] , [ 100 , 0 , 0 , 0 , 2 , 2 ] , [ 0 , 0 , 0 , 2 , 0 , 2 ] , [ 0 , 0 , 0 , 2 , 2 , 0 ] ] ; findcost ( n2 , city2 ) ;
Level order traversal with direction change after every two levels | Javascript program to print Zig - Zag traversal in groups of size 2. ; A Binary Tree Node ; Utility function to create a new tree node ; Function to print the level order of given binary tree . Direction of printing level order traversal of binary tree changes after every two levels ; Run this while loop till queue got empty ; For printing nodes from right to left , push the nodes to stack instead of printing them . ; for printing the nodes in order from right to left ; Change the direction of traversal . ; Let us create binary tree
let LEFT = 0 ; let RIGHT = 1 ; function ChangeDirection ( Dir ) { Dir = 1 - Dir ; return Dir ; } class Node { constructor ( data ) { this . data = data ; this . left = this . right = null ; } } function modifiedLevelOrder ( root ) { if ( root == null ) return ; let dir = LEFT ; let temp ; let Q = [ ] ; let S = [ ] ; S . push ( root ) ; while ( Q . length != 0 S . length != 0 ) { while ( S . length != 0 ) { temp = S . pop ( ) ; document . write ( temp . data + " " ) ; if ( dir == LEFT ) { if ( temp . left != null ) Q . push ( temp . left ) ; if ( temp . right != null ) Q . push ( temp . right ) ; } else { if ( temp . right != null ) Q . push ( temp . right ) ; if ( temp . left != null ) Q . push ( temp . left ) ; } } document . write ( " " ) ; while ( Q . length != 0 ) { temp = Q [ 0 ] ; Q . shift ( ) ; document . write ( temp . data + " " ) ; if ( dir == LEFT ) { if ( temp . left != null ) S . push ( temp . left ) ; if ( temp . right != null ) S . push ( temp . right ) ; } else { if ( temp . right != null ) S . push ( temp . right ) ; if ( temp . left != null ) S . push ( temp . left ) ; } } document . write ( " " ) ; dir = ChangeDirection ( dir ) ; } } let root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . left . left . right = new Node ( 9 ) ; root . left . right . left = new Node ( 3 ) ; root . left . right . right = new Node ( 1 ) ; root . right . left . left = new Node ( 4 ) ; root . right . left . right = new Node ( 2 ) ; root . right . right . left = new Node ( 7 ) ; root . right . right . right = new Node ( 2 ) ; root . left . right . left . left = new Node ( 16 ) ; root . left . right . left . right = new Node ( 17 ) ; root . right . left . right . left = new Node ( 18 ) ; root . right . right . left . right = new Node ( 19 ) ; modifiedLevelOrder ( root ) ;
The Knight 's tour problem | Backtracking | Javascript program for Knight Tour problem ; A utility function to check if i , j are valid indexes for N * N chessboard ; A utility function to prlet solution matrix sol [ N ] [ N ] ; This function solves the Knight Tour problem using Backtracking . This function mainly uses solveKTUtil ( ) to solve the problem . It returns false if no complete tour is possible , otherwise return true and prlets the tour . Please note that there may be more than one solutions , this function prlets one of the feasible solutions . ; Initialization of solution matrix ; xMove [ ] and yMove [ ] define next move of Knight . xMove [ ] is for next value of x coordinate yMove [ ] is for next value of y coordinate ; Since the Knight is initially at the first block ; Start from 0 , 0 and explore all tours using solveKTUtil ( ) ; A recursive utility function to solve Knight Tour problem ; Try all next moves from the current coordinate x , y ; backtracking ; Function Call
let N = 8 ; function isSafe ( x , y , sol ) { return ( x >= 0 && x < N && y >= 0 && y < N && sol [ x ] [ y ] == - 1 ) ; } function printSolution ( sol ) { for ( let x = 0 ; x < N ; x ++ ) { for ( let y = 0 ; y < N ; y ++ ) document . write ( sol [ x ] [ y ] + " " ) ; document . write ( " " ) ; } } function solveKT ( ) { let sol = new Array ( 8 ) ; for ( var i = 0 ; i < sol . length ; i ++ ) { sol [ i ] = new Array ( 2 ) ; } for ( let x = 0 ; x < N ; x ++ ) for ( let y = 0 ; y < N ; y ++ ) sol [ x ] [ y ] = - 1 ; let xMove = [ 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 ] ; let yMove = [ 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 ] ; sol [ 0 ] [ 0 ] = 0 ; if ( ! solveKTUtil ( 0 , 0 , 1 , sol , xMove , yMove ) ) { document . write ( " " ) ; return false ; } else printSolution ( sol ) ; return true ; } function solveKTUtil ( x , y , movei , sol , xMove , yMove ) { let k , next_x , next_y ; if ( movei == N * N ) return true ; for ( k = 0 ; k < 8 ; k ++ ) { next_x = x + xMove [ k ] ; next_y = y + yMove [ k ] ; if ( isSafe ( next_x , next_y , sol ) ) { sol [ next_x ] [ next_y ] = movei ; if ( solveKTUtil ( next_x , next_y , movei + 1 , sol , xMove , yMove ) ) return true ; else sol [ next_x ] [ next_y ] = - 1 ; } } return false ; } solveKT ( ) ;
m Coloring Problem | Backtracking | Number of vertices in the graph ; A utility function to print solution ; check if the colored graph is safe or not ; check for every edge ; This function solves the m Coloring problem using recursion . It returns false if the m colours cannot be assigned , otherwise , return true and prints assignments of colours to all vertices . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; if current index reached end ; if coloring is safe ; Print the solution ; Assign each color from 1 to m ; Recur of the rest vertices ; Create following graph and test whether it is 3 colorable ( 3 ) -- - ( 2 ) | / | | / | | / | ( 0 ) -- - ( 1 ) ; Number of colors ; Initialize all color values as 0. This initialization is needed correct functioning of isSafe ( )
let V = 4 ; function printSolution ( color ) { document . write ( " " + " " ) ; for ( let i = 0 ; i < V ; i ++ ) document . write ( " " + color [ i ] ) ; document . write ( " " ) ; } function isSafe ( graph , color ) { for ( let i = 0 ; i < V ; i ++ ) for ( let j = i + 1 ; j < V ; j ++ ) if ( graph [ i ] [ j ] && color [ j ] == color [ i ] ) return false ; return true ; } function graphColoring ( graph , m , i , color ) { if ( i == V ) { if ( isSafe ( graph , color ) ) { printSolution ( color ) ; return true ; } return false ; } for ( let j = 1 ; j <= m ; j ++ ) { color [ i ] = j ; if ( graphColoring ( graph , m , i + 1 , color ) ) return true ; color [ i ] = 0 ; } return false ; } let graph = [ [ false , true , true , true ] , [ true , false , true , false ] , [ true , true , false , true ] , [ true , false , true , false ] ] ; let m = 3 ; let color = new Array ( V ) ; for ( let i = 0 ; i < V ; i ++ ) color [ i ] = 0 ; if ( ! graphColoring ( graph , m , 0 , color ) ) document . write ( " " ) ;
Maximize pair decrements required to reduce all array elements except one to 0 | Function to count maximum number of steps to make ( N - 1 ) array elements to 0 ; Stores maximum count of steps to make ( N - 1 ) elements equal to 0 ; Stores array elements ; Traverse the array ; Insert arr [ i ] into PQ ; Extract top 2 elements from the array while ( N - 1 ) array elements become 0 ; Stores top element of PQ ; Pop the top element of PQ . ; Stores top element of PQ ; Pop the top element of PQ . ; Update X ; Update Y ; If X is not equal to 0 ; Insert X into PQ ; if Y is not equal to 0 ; Insert Y into PQ ; Update cntOp ; Driver code
function cntMaxOperationToMakeN_1_0 ( arr , N ) { let cntOp = 0 ; let PQ = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { PQ . push ( arr [ i ] ) ; } PQ . sort ( function ( a , b ) { return a - b } ) ; PQ . reverse ( ) ; while ( PQ . length > 1 ) { let X = PQ [ 0 ] ; PQ . shift ( ) ; let Y = PQ [ 0 ] ; PQ . shift ( ) ; X -- ; Y -- ; if ( X != 0 ) { PQ . push ( X ) ; PQ . sort ( function ( a , b ) { return a - b } ) ; PQ . reverse ( ) ; } if ( Y != 0 ) { PQ . push ( Y ) ; PQ . sort ( function ( a , b ) { return a - b } ) ; PQ . reverse ( ) ; } cntOp += 1 ; } return cntOp ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let N = arr . length ; document . write ( cntMaxOperationToMakeN_1_0 ( arr , N ) ) ;
Flip all K | Function to flip all K - bits of an unsigned number N ; Stores ( 2 ^ K ) - 1 ; Update N ; Print the answer ; Driver Code
function flippingBits ( N , K ) { let X = ( 1 << ( K - 1 ) ) - 1 ; N = X - N ; document . write ( N ) ; } let N = 1 , K = 8 ; flippingBits ( N , K ) ;
Generate a matrix having even sum of all diagonals in each 2 x 2 submatrices | Function to construct a matrix such that the sum elements in both diagonals of every 2 * 2 matrices is even ; Stores odd numbers ; Stores even numbers ; Store matrix elements such that sum of elements in both diagonals of every 2 * 2 submatrices is even ; Fill all the values of matrix elements ; Update odd ; Update even ; Prlet the matrix ; Driver Code
function generateMatrix ( N ) { let odd = 1 ; let even = 2 ; let mat = new Array ( N + 1 ) ; for ( var i = 0 ; i < mat . length ; i ++ ) { mat [ i ] = new Array ( 2 ) ; } for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= N ; j ++ ) { if ( ( i + j ) % 2 == 0 ) { mat [ i ] [ j ] = odd ; odd += 2 ; } else { mat [ i ] [ j ] = even ; even += 2 ; } } } for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= N ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let N = 4 ; generateMatrix ( N ) ;
Queries to calculate sum by alternating signs of array elements in a given range | Function to build the segment tree ; If current node is a leaf node of the segment tree ; Update tree [ index ] ; Update tree [ index ] ; Divide the segment tree ; Update on L segment tree ; Update on R segment tree ; Find the sum from L subtree and R subtree ; Function to update elements at index pos by val in the segment tree ; If current node is a leaf node ; If current index is even ; Update tree [ index ] ; Update tree [ index ] ; Divide the segment tree elements into L and R subtree ; If element lies in L subtree ; Update tree [ index ] ; Function to find the sum of array elements in the range [ L , R ] ; If start and end not lies in the range [ L , R ] ; If start and end comleately lies in the range [ L , R ] ; Stores sum from left subtree ; Stores sum from right subtree ; Driver Code
function build ( tree , arr , start , end , index ) { if ( start == end ) { if ( start % 2 == 0 ) { tree [ index ] = arr [ start ] ; } else { tree [ index ] = - arr [ start ] ; } return ; } var mid = start + parseInt ( ( end - start ) / 2 ) ; build ( tree , arr , start , mid , 2 * index + 1 ) ; build ( tree , arr , mid + 1 , end , 2 * index + 2 ) ; tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ] ; } function update ( tree , index , start , end , pos , val ) { if ( start == end ) { if ( start % 2 == 0 ) { tree [ index ] = val ; } else { tree [ index ] = - val ; } return ; } var mid = start + parseInt ( ( end - start ) / 2 ) ; if ( mid >= pos ) { update ( tree , 2 * index + 1 , start , mid , pos , val ) ; } else { update ( tree , 2 * index + 2 , mid + 1 , end , pos , val ) ; } tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ] ; } function FindSum ( tree , start , end , L , R , index ) { if ( L > end R < start ) { return 0 ; } if ( L <= start && R >= end ) { return tree [ index ] ; } var mid = start + parseInt ( ( end - start ) / 2 ) ; var X = FindSum ( tree , start , mid , L , R , 2 * index + 1 ) ; var Y = FindSum ( tree , mid + 1 , end , L , R , 2 * index + 2 ) ; return X + Y ; } var arr = [ 1 , 2 , 3 , 4 ] ; var N = arr . length ; var tree = Array ( 4 * N + 5 ) . fill ( 0 ) ; build ( tree , arr , 0 , N - 1 , 0 ) ; var Q = [ [ 2 , 0 , 3 ] , [ 1 , 1 , 5 ] , [ 2 , 1 , 2 ] ] ; var cntQuey = 3 ; for ( var i = 0 ; i < cntQuey ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { update ( tree , 0 , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] ) ; } else { if ( Q [ i ] [ 1 ] % 2 == 0 ) { document . write ( FindSum ( tree , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 ) + " " ) ; } else { document . write ( - FindSum ( tree , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 ) + " " ) ; } } }