Range Queries to find the Element having Maximum Digit Sum
Last Updated :
21 Apr, 2023
Given an array Arr of N integers and Q queries, each query having a range from L to R. Find the element having maximum digit sum for the range L to R, and if more than one element has a maximum digit sum, then find the maximum element out of those.
Examples:
Input: Arr[] = { 16, 12, 43, 55}
Q = 2
L = 0, R = 3
L = 0, R = 2
Output: 55
43
Explanation:
The range (0, 3) in the 1st query has
[16, 12, 43, 55]. Here, the digit sums are:
for 16, 1 + 6 = 7
for 12, 1 + 2 = 3
for 43, 4 + 3 = 7
for 55, 5 + 5 = 10
Hence, the max digit sum is 10 and
max digit sum value is 55.
The range (0, 2) in the 1st query has
[16, 12, 43]. Here, the digit sums are:
for 16, 1 + 6 = 7
for 12, 1 + 2 = 3
for 43, 4 + 3 = 7
Hence, the max digit sum is 7 and
max digit sum value is max( 16, 43) = 43.
Naive Approach:
A simple solution is to run a loop from L to R and calculate the digit sum for each element and find the maximum digit sum element from L to R for every query.
Below is the implementation of the approach:
C++
// C++ code for the approach
#include <iostream>
using namespace std;
// Function to calculate the digit sum of a number
int digit_sum(int n)
{
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
// Function to find the element having maximum digit sum in
// the given range
int max_digit_sum(int arr[], int n, int l, int r)
{
int max_sum = -1, max_num = -1;
for (int i = l; i <= r; i++) {
int sum = digit_sum(arr[i]);
if (sum > max_sum) {
max_sum = sum;
max_num = arr[i];
}
else if (sum == max_sum && arr[i] > max_num) {
max_num = arr[i];
}
}
return max_num;
}
// Main function to handle the queries
int main()
{
// Input array
int arr[] = { 16, 12, 43, 55 };
// Calculates the length of array
int n = sizeof(arr) / sizeof(arr[0]);
// query 1
int l = 0, r = 3;
cout << max_digit_sum(arr, n, l, r) << endl;
// query 2
l = 0, r = 2;
cout << max_digit_sum(arr, n, l, r) << endl;
return 0;
}
Java
// Java code for the approach
import java.util.*;
public class GFG {
// Function to calculate the digit sum of a number
public static int digit_sum(int n)
{
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
// Function to find the element having maximum digit sum
// in
// the given range
public static int max_digit_sum(int[] arr, int n, int l,
int r)
{
int max_sum = -1, max_num = -1;
for (int i = l; i <= r; i++) {
int sum = digit_sum(arr[i]);
if (sum > max_sum) {
max_sum = sum;
max_num = arr[i];
}
else if (sum == max_sum && arr[i] > max_num) {
max_num = arr[i];
}
}
return max_num;
}
// Main function to handle the queries
public static void main(String[] args)
{
// Input array
int[] arr = { 16, 12, 43, 55 };
// Calculates the length of array
int n = arr.length;
// query 1
int l = 0, r = 3;
System.out.println(max_digit_sum(arr, n, l, r));
// query 2
l = 0;
r = 2;
System.out.println(max_digit_sum(arr, n, l, r));
}
}
Python3
# Python3 code for the approach
# Function to calculate the digit sum of a number
def digit_sum(n):
sum = 0
while n > 0:
sum += n % 10
n //= 10
return sum
# Function to find the element having maximum digit sum in the given range
def max_digit_sum(arr, l, r):
max_sum = -1
max_num = -1
for i in range(l, r+1):
sum = digit_sum(arr[i])
if sum > max_sum:
max_sum = sum
max_num = arr[i]
elif sum == max_sum and arr[i] > max_num:
max_num = arr[i]
return max_num
# Main function to handle the queries
if __name__ == '__main__':
# Input array
arr = [16, 12, 43, 55]
# Calculates the length of array
n = len(arr)
# query 1
l = 0
r = 3
print(max_digit_sum(arr, l, r))
# query 2
l = 0
r = 2
print(max_digit_sum(arr, l, r))
C#
using System;
namespace MaxDigitSum
{
class Program
{
// Function to calculate the digit sum of a number
static int DigitSum(int n)
{
int sum = 0;
while (n > 0)
{
sum += n % 10;
n /= 10;
}
return sum;
}
// Function to find the element having maximum digit sum in
// the given range
static int MaxDigitSum(int[] arr, int n, int l, int r)
{
int maxSum = -1, maxNum = -1;
for (int i = l; i <= r; i++)
{
int sum = DigitSum(arr[i]);
if (sum > maxSum)
{
maxSum = sum;
maxNum = arr[i];
}
else if (sum == maxSum && arr[i] > maxNum)
{
maxNum = arr[i];
}
}
return maxNum;
}
// Main function to handle the queries
static void Main(string[] args)
{
// Input array
int[] arr = { 16, 12, 43, 55 };
// Calculates the length of array
int n = arr.Length;
// query 1
int l = 0, r = 3;
Console.WriteLine(MaxDigitSum(arr, n, l, r));
// query 2
l = 0; r = 2;
Console.WriteLine(MaxDigitSum(arr, n, l, r));
}
}
}
JavaScript
function digitSum(n) {
let sum = 0;
while (n > 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
function maxDigitSum(arr, l, r) {
let maxSum = -1, maxNum = -1;
for (let i = l; i <= r; i++) {
let sum = digitSum(arr[i]);
if (sum > maxSum) {
maxSum = sum;
maxNum = arr[i];
}
else if (sum === maxSum && arr[i] > maxNum) {
maxNum = arr[i];
}
}
return maxNum;
}
// Example usage
let arr = [16, 12, 43, 55];
console.log(maxDigitSum(arr, 0, 3)); // Output: 55
console.log(maxDigitSum(arr, 0, 2)); // Output: 43
Time Complexity: O(Q * N * log10(Max)), where Max is the maximum value in arr.
Auxiliary Space: O(1).
Efficient Approach:
- An efficient approach will be to build a Segment Tree where each node stores two values(value and max_digit_sum), and do a range query on it to find the max digit sum and the corresponding element. But for building the segment tree we have to think about what to store on the nodes of the tree.
- For finding out the maximum digit sum value, we will require two things, one being the value and the other digit sum. The merging will return two things, the digit sum will store the max(max_digit_sum.left, max_digit_sum.right) in the segment tree and value will contain the corresponding element value.
- If we have a deep look into it, the max digit sum for any two range combining will either be the max digit sum from the left side or the max digit sum from the right side, whichever is maximum will taken into account.
- Representation of Segment trees:
- Leaf Nodes are the elements of the given array.
- Each internal node represents some merging of the leaf nodes. The merging may be different for different problems. For this problem, merging is the maximum of the max_digit_sum of leaves under a node.
- An array representation of the 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 a 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 max_digit_sum and the value in the corresponding node.
- We then do a range query on the segment tree to find out the max_digit_sum for the given range and output the corresponding value.
Below is the implementation of the above approach.
C++
// C++ program to find
// maximum digit sum value
#include <bits/stdc++.h>
using namespace std;
// Struct two store two values in one node
struct Node {
int value;
int max_digit_sum;
};
Node tree[4 * 10000];
// Function to find the digit sum
// for a number
int digitSum(int x)
{
int sum = 0;
while (x) {
sum += (x % 10);
x /= 10;
}
}
// Function to build the segment tree
void build(int a[], int index, int beg, int 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 {
int mid = (beg + end) / 2;
// 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 =
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
Node query(int index, int beg, int end,
int l, int r)
{
Node result;
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];
int mid = (beg + end) / 2;
// 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
Node left = query(2 * index + 1, beg,
mid, l, r);
Node 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 = max(right.value, left.value);
}
// Returns the value
return result;
}
// Driver code
int main()
{
int a[] = {16, 12, 43, 55};
// Calculates the length of array
int N = sizeof(a) / sizeof(a[0]);
// 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
cout << query(0, 0, N - 1, 0, 3).value
<< endl;
// Find the max digit-sum value between
// 0th and 2nd index of array
cout << query(0, 0, N - 1, 0, 2).value
<< endl;
return 0;
}
Java
// Java program to find
// maximum digit sum value
import java.util.*;
class GFG{
// Struct two store two values
// in one node
static class Node
{
int value;
int max_digit_sum;
};
static Node []tree = new Node[4 * 10000];
// Function to find the digit sum
// for a number
static int digitSum(int x)
{
int sum = 0;
while (x > 0)
{
sum += (x % 10);
x /= 10;
}
return sum;
}
// Function to build the segment tree
static void build(int a[], int index,
int beg, int 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
{
int mid = (beg + end) / 2;
// 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
static Node query(int index, int beg, int end,
int l, int r)
{
Node 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];
int mid = (beg + end) / 2;
// 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
Node left = query(2 * index + 1, beg,
mid, l, r);
Node 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;
}
// Driver code
public static void main(String[] args)
{
int a[] = { 16, 12, 43, 55 };
// Calculates the length of array
int N = a.length;
for(int 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
System.out.print(
query(0, 0, N - 1, 0, 3).value + "\n");
// Find the max digit-sum value between
// 0th and 2nd index of array
System.out.print(
query(0, 0, N - 1, 0, 2).value + "\n");
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to find
# maximum digit sum value
# Struct two store two values in one node
class Node:
def __init__(self):
self.value = 0
self.max_digit_sum = 0
tree = [Node() for i in range(4 * 10000)]
# Function to find the digit sum
# for a number
def digitSum(x):
sum = 0;
while(x != 0):
sum += (x % 10)
x //= 10
return sum
# Function to build the segment tree
def 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:
mid = (beg + end) // 2
# 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
elif (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 = 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
def query(index, beg, end, l, r):
result = 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 or 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 and end <= r):
return tree[index]
mid = (beg + end) // 2
# 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
left = query(2 * index + 1, beg, mid, l, r)
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
elif (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 = max(right.value, left.value)
# Returns the value
return result
# Driver code
if __name__=="__main__":
a = [ 16, 12, 43, 55 ]
# Calculates the length of array
N = len(a)
# 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
print(query(0, 0, N - 1, 0, 3).value)
# Find the max digit-sum value between
# 0th and 2nd index of array
print(query(0, 0, N - 1, 0, 2).value)
# This code is contributed by rutvik_56
C#
// C# program to find
// maximum digit sum value
using System;
class GFG{
// Struct two store
// two values in one node
class Node
{
public int value;
public int max_digit_sum;
};
static Node []tree = new Node[4 * 10000];
// Function to find the digit sum
// for a number
static int digitSum(int x)
{
int sum = 0;
while (x > 0)
{
sum += (x % 10);
x /= 10;
}
return sum;
}
// Function to build the segment tree
static void build(int []a, int index,
int beg, int 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
{
int mid = (beg + end) / 2;
// 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
static Node query(int index, int beg,
int end, int l, int r)
{
Node 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];
int mid = (beg + end) / 2;
// 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
Node left = query(2 * index + 1, beg,
mid, l, r);
Node 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;
}
// Driver code
public static void Main(String[] args)
{
int []a = {16, 12, 43, 55};
// Calculates the length
// of array
int N = a.Length;
for(int 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
Console.Write(query(0, 0,
N - 1,
0, 3).value + "\n");
// Find the max digit-sum value between
// 0th and 2nd index of array
Console.Write(query(0, 0,
N - 1,
0, 2).value + "\n");
}
}
// This code is contributed by Rajput-Ji
JavaScript
<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>
Complexity Analysis:
Time Complexity for tree construction is O(N). There are total 2n-1 nodes, and the value of every node is calculated only once in tree construction.
Time complexity to every query is O(log N).
Time complexity for the problem is O(Q * log N)
Similar Reads
Find the maximum Even Digit Sum node in the given tree
Given a tree with the weights of all the nodes, the task is to find the maximum weighing node whose weight has even digit sum. Examples: Input: Tree = 5 / \ 10 6 / \ 11 8 Output: 11 Explanation: The tree node weights are: 5 -> 5 10 -> 1 + 0 = 1 6 -> 6 11 -> 1 + 1 = 2 8 -> 8 Here, digi
7 min read
Find the number in a range having maximum product of the digits
Given a range represented by two positive integers L and R. Find the number lying in the range having the maximum product of the digits.Examples: Input : L = 1, R = 10 Output : 9 Input : L = 51, R = 62 Output : 59 Approach : The key idea here is to iterate over the digits of the number R starting fr
15+ min read
Find the maximum range [L,R] whose sum is divisible by M
Given an array arr[] consisting of positive numbers, the task is to find the maximum range [L, R] whose sum is divisible by M. If there is no range present return -1. Examples: Input: arr[] = {3, 7, 5, 2, 5, 10}, M = 3 Output: 1 3 Explanation: Sum of numbers from 1 to 3 is 3+7+5 which is 15. Input :
8 min read
Find the array element having maximum frequency of the digit K
Given an array arr[] of size N and an integer K, the task is to find an array element that contains the digit K a maximum number of times. If more than one solutions exist, then print any one of them. Otherwise, print -1. Examples: Input: arr[] = {3, 77, 343, 456}, K = 3 Output: 343 Explanation: Fre
15 min read
Maximum subset sum such that no two elements in set have same digit in them
Given an array of N elements. Find the subset of elements which has maximum sum such that no two elements in the subset has common digit present in them.Examples: Input : array[] = {22, 132, 4, 45, 12, 223} Output : 268 Maximum Sum Subset will be = {45, 223} . All possible digits are present except
12 min read
Maximum sum of two elements whose digit sum is equal
Content has been removed on Author's request.
1 min read
Maximum sum with limited queries
Given an array arr[] of n integers in sorted order, find the maximum number of largest elements that can be selected from the array such that the sum is less than or equal to k. You can select an element any number of times. The sum of the selected elements should be maximized. You can query the val
7 min read
Minimum count of elements that sums to a given number
Given infinite number of elements of form 10^n and 25*100^n ( n >= 0 ). The task is to find the minimum count of elements chosen such that there sum is equal to K. Examples: Input : K = 48 Output : 6 elements chosen are: (1 + 1 + 1 + 10 + 10 + 25)Input : 69 Output : 9 elements chosen are: (1 + 1
7 min read
Maximum occurring integer in given ranges
Given two arrays L[] and R[] of size N where L[i] and R[i] (0 ? L[i], R[i] < 106)denotes a range of numbers, the task is to find the maximum occurred integer in all the ranges. If more than one such integer exists, print the smallest one. Examples: Input: L[] = {1, 4, 3, 1}, R[] = {15, 8, 5, 4}Ou
9 min read
Find the maximum sum of digits of the product of two numbers
Given an array arr[] of size N( > 2). The task is to find the maximum sum of digits of the product of any two numbers of the given array.Examples: Input : arr[] = {8, 7} Output : 11 The product of 8 and 7 is 56. The sum of the digits of 56 is equal to 11.Input : arr[] = {4, 3, 5} Output : 6 Produ
5 min read