<script>
// Javascript program to find maximum digit sum value
// Struct two store two values in one node
class Node
{
constructor() {
this.value = 0;
this.max_digit_sum = 0;
}
}
let tree = new Array(4 * 10000);
// Function to find the digit sum for a number
function digitSum(x)
{
let sum = 0;
while (x > 0)
{
sum += (x % 10);
x = parseInt(x / 10, 10);
}
return sum;
}
// Function to build the segment tree
function build(a, index, beg, end)
{
if (beg == end)
{
// If there is one element in array,
tree[index].value = a[beg];
tree[index].max_digit_sum = digitSum(a[beg]);
}
else
{
let mid = parseInt((beg + end) / 2, 10);
// If there are more than one elements,
// then recur for left and right subtrees
build(a, 2 * index + 1, beg, mid);
build(a, 2 * index + 2, mid + 1, end);
if (tree[2 * index + 1].max_digit_sum >
tree[2 * index + 2].max_digit_sum)
{
tree[index].max_digit_sum =
tree[2 * index + 1].max_digit_sum;
tree[index].value =
tree[2 * index + 1].value;
}
else if (tree[2 * index + 2].max_digit_sum >
tree[2 * index + 1].max_digit_sum)
{
tree[index].max_digit_sum =
tree[2 * index + 2].max_digit_sum;
tree[index].value =
tree[2 * index + 2].value;
}
else
{
tree[index].max_digit_sum =
tree[2 * index + 2].max_digit_sum;
tree[index].value =
Math.max(tree[2 * index + 2].value,
tree[2 * index + 1].value);
}
}
}
// Function to do the range query
// in the segment tree for the
// maximum digit sum
function query(index, beg, end, l, r)
{
let result = new Node();
result.value = result.max_digit_sum = -1;
// If segment of this node is
// outside the given range,
// then return the minimum value.
if (beg > r || end < l)
return result;
// If segment of this node
// is a part of given range,
// then return the node of the segment
if (beg >= l && end <= r)
return tree[index];
let mid = parseInt((beg + end) / 2, 10);
// If left segment of this
// node falls out of range,
// then recur in the right
// side of the tree
if (l > mid)
return query(2 * index + 2, mid + 1, end, l, r);
// If right segment of this
// node falls out of range,
// then recur in the left side of
// the tree
if (r <= mid)
return query(2 * index + 1, beg, mid, l, r);
// If a part of this segment
// overlaps with the given range
let left = query(2 * index + 1, beg,
mid, l, r);
let right = query(2 * index + 2, mid + 1,
end, l, r);
if (left.max_digit_sum > right.max_digit_sum)
{
result.max_digit_sum = left.max_digit_sum;
result.value = left.value;
}
else if (right.max_digit_sum > left.max_digit_sum)
{
result.max_digit_sum = right.max_digit_sum;
result.value = right.value;
}
else
{
result.max_digit_sum = left.max_digit_sum;
result.value = Math.max(right.value, left.value);
}
// Returns the value
return result;
}
let a = [16, 12, 43, 55];
// Calculates the length
// of array
let N = a.length;
for(let i = 0; i < tree.length; i++)
tree[i] = new Node();
// Calls the build function
// to build the segment tree
build(a, 0, 0, N - 1);
// Find the max digit-sum value between
// 0th and 3rd index of array
document.write(query(0, 0,
N - 1,
0, 3).value + "</br>");
// Find the max digit-sum value between
// 0th and 2nd index of array
document.write(query(0, 0,
N - 1,
0, 2).value + "</br>");
// This code is contributed by divyeshrabadiya07.
</script>