Given an array arr of N integers, the task is to perform the following two queries:
- maximum(start, end): Print the maximum Armstrong number of elements in the sub-array from start to end
- update(i, x): Add x to the array element referenced by array index i, that is: arr[i] = x
If there is no Armstrong number in the sub-array, print -1.
Input: arr = [192, 113, 535, 7, 19, 111]
Query 1: maximum(start=1, end=3)
Query 2: update(i=1, x=153) i.e. arr=153
Maximum Armstrong number in given range = 7
Updated Maximum Armstrong number in given range = 153
In the Maximum Query, the sub-array [1…3] has 1 Armstrong number 7 viz. [113, 535, 7]
Hence, 7 is the maximum Armstrong number in the given range.
In the Update Query, the value at index 1 is updated to 153, the array arr now is, [192, 153, 535, 7, 19, 111]
In Updated Maximum Query, the sub-array [1…3] has 2 Armstrong numbers 153 and 7 viz. [153, 535, 7]
Hence, 153 is the maximum Armstrong number in the given range.
A simple solution is to run a loop from l to r and calculate the maximum Armstrong number of elements in a given range. To update a value, simply do arr[i] = x. The first operation takes O(N) time and the second operation takes O(1) time.
- An efficient approach will be to build a Segment Tree where each node stores two values(value and max_set_bits), and do a range query on it to find the maximum Armstrong number.
- If we have a deep look into it, the maximum Armstrong number for any two range combining will either be the maximum Armstrong number from the left side or the maximum Armstrong number from the right side, whichever is maximum will be taken into account.
- Representation of Segment trees:
- Leaf Nodes are the elements of the given array.
- Each internal node represents the maximum Armstrong number of all of its child or -1 is no Armstrong number exists in the range.
- An array representation of a tree is used to represent Segment Trees. For each node at index i, the left child is at index 2*i+1, right child at 2*i+2 and the parent is at (i-1)/2.
- Construction of Segment Tree from the given array:
- We start with a segment arr[0 . . . n-1], and every time we divide the current segment into two halves(if it has not yet become a segment of length 1), and then call the same procedure on both halves, and for each such segment, we store the maximum Armstrong number value or -1 in a segment tree node. All levels of the constructed segment tree will be completely filled except the last level. Also, the tree will be a full Binary Tree because we always divide segments into two halves at every level. Since the constructed tree is always a full binary tree with n leaves, there will be n-1 internal nodes. So total nodes will be 2*n – 1. The height of the segment tree will be log2n. Since the tree is represented using array and relation between parent and child indexes must be maintained, the size of memory allocated for the segment tree will be 2*( 2ceil(log2n) ) – 1.
- A positive integer of n digits is called an Armstrong number of order n (order is a number of digits) if
abcd… = pow(a, n) + pow(b, n) + pow(c, n) + pow(d, n) + ….
- In order to check for Armstrong numbers, the idea is to first count number digits (or find order). Let the number of digits be n. For every digit r in input number x, compute rn. If the sum of all such values is equal to n, then return true else false.
- We then do a range query on the segment tree to find out the maximum Armstrong number for the given range and output the corresponding value.
Below is the implementation of the above approach:
Maximum armstrong number in given range = 7
Updated Maximum armstrong number in given range = 153
Time Complexity: The time complexity of each query and update is O(log N) and that of building the segment tree is O(N)