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
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 ) ; document . write ( " " + " " + getMax ( st , n , 1 , 3 ) + " " ) ; updateValue ( arr , st , 0 , n - 1 , 1 , 153 , 0 ) ; document . write ( " " + " " + getMax ( st , n , 1 , 3 ) + " " ) ;