Find the maximum Even Digit Sum node in the given tree
Last Updated :
23 Jun, 2021
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, digit sum for nodes
containing 11, 6 and 8 are even.
Hence, the maximum weighing
even digit sum node is 11.
Input: Tree =
1
/ \
4 7
/ \ / \
11 3 15 8
Output: 15
Explanation:
Here, digit sum for nodes containing
4, 11, 15 and 8 are even.
Hence, the maximum weighing
even digit sum node is 15.
Approach: To solve the problem mentioned above follow the steps given below:
- The idea is to perform a depth first search on the tree and for every node.
- First, find the digit sum for the weight present in the node by iterating through each digit and then check if the node has even digit sum or not.
- Finally, if it has an even digit sum then check if this node is the maximum even digit sum node we have found so far, if yes, then make this node the maximum even digit sum node.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e5;
int ans = -1;
vector< int > graph[100];
vector< int > weight(100);
int digitSum( int num)
{
int sum = 0;
while (num)
{
sum += (num % 10);
num /= 10;
}
return sum;
}
void dfs( int node, int parent)
{
if (!(digitSum(weight[node]) & 1))
ans = max(ans, weight[node]);
for ( int to : graph[node]) {
if (to == parent)
continue ;
dfs(to, node);
}
}
int main()
{
weight[1] = 5;
weight[2] = 10;
weight[3] = 11;
weight[4] = 8;
weight[5] = 6;
graph[1].push_back(2);
graph[2].push_back(3);
graph[2].push_back(4);
graph[1].push_back(5);
dfs(1, 1);
cout << ans << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int sz = ( int )1e5;
static int ans = - 1 ;
static Vector<Integer>[] graph = new Vector[ 100 ];
static int [] weight = new int [ 100 ];
static int digitSum( int num)
{
int sum = 0 ;
while (num > 0 )
{
sum += (num % 10 );
num /= 10 ;
}
return sum;
}
static void dfs( int node, int parent)
{
if (!(digitSum(weight[node]) % 2 == 1 ))
ans = Math.max(ans, weight[node]);
for ( int to : graph[node])
{
if (to == parent)
continue ;
dfs(to, node);
}
}
public static void main(String[] args)
{
weight[ 1 ] = 5 ;
weight[ 2 ] = 10 ;
weight[ 3 ] = 11 ;
weight[ 4 ] = 8 ;
weight[ 5 ] = 6 ;
for ( int i = 0 ; i < graph.length; i++)
graph[i] = new Vector<Integer>();
graph[ 1 ].add( 2 );
graph[ 2 ].add( 3 );
graph[ 2 ].add( 4 );
graph[ 1 ].add( 5 );
dfs( 1 , 1 );
System.out.print(ans + "\n" );
}
}
|
Python3
sz = 1e5
ans = - 1
graph = [[] for i in range ( 100 )]
weight = [ - 1 ] * 100
def digitSum(num):
sum = 0
while num:
sum + = num % 10
num = num / / 10
return sum
def dfs(node, parent):
global ans
if not (digitSum(weight[node]) & 1 ):
ans = max (ans, weight[node])
for to in graph[node]:
if to = = parent:
continue
dfs(to, node)
def main():
weight[ 1 ] = 5
weight[ 2 ] = 10
weight[ 3 ] = 11
weight[ 4 ] = 8
weight[ 5 ] = 6
graph[ 1 ].append( 2 )
graph[ 2 ].append( 3 )
graph[ 2 ].append( 4 )
graph[ 1 ].append( 5 )
dfs( 1 , 1 )
print (ans)
main()
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int sz = ( int )1e5;
static int ans = -1;
static List< int >[] graph = new List< int >[ 100 ];
static int [] weight = new int [100];
static int digitSum( int num)
{
int sum = 0;
while (num > 0)
{
sum += (num % 10);
num /= 10;
}
return sum;
}
static void dfs( int node, int parent)
{
if (!(digitSum(weight[node]) % 2 == 1))
ans = Math.Max(ans, weight[node]);
foreach ( int to in graph[node])
{
if (to == parent)
continue ;
dfs(to, node);
}
}
public static void Main(String[] args)
{
weight[1] = 5;
weight[2] = 10;
weight[3] = 11;
weight[4] = 8;
weight[5] = 6;
for ( int i = 0; i < graph.Length; i++)
graph[i] = new List< int >();
graph[1].Add(2);
graph[2].Add(3);
graph[2].Add(4);
graph[1].Add(5);
dfs(1, 1);
Console.Write(ans + "\n" );
}
}
|
Javascript
<script>
let sz = 1e5;
let ans = -1;
let graph = new Array(100);
let weight = new Array(100);
function digitSum(num)
{
let sum = 0;
while (num > 0)
{
sum += (num % 10);
num = parseInt(num / 10, 10);
}
return sum;
}
function dfs(node, parent)
{
if (!(digitSum(weight[node]) % 2 == 1))
ans = Math.max(ans, weight[node]);
for (let to = 0; to < graph[node].length; to++)
{
if (graph[node][to] == parent)
continue ;
dfs(graph[node][to], node);
}
}
weight[1] = 5;
weight[2] = 10;
weight[3] = 11;
weight[4] = 8;
weight[5] = 6;
for (let i = 0; i < graph.length; i++)
graph[i] = [];
graph[1].push(2);
graph[2].push(3);
graph[2].push(4);
graph[1].push(5);
dfs(1, 1);
document.write(ans + "</br>" );
</script>
|
Complexity Analysis:
Time Complexity : O(N).
In dfs, every node of the tree is processed once and hence the complexity due to the dfs is O(N) if there are total N nodes in the tree. Also, for processing each node the DigitSum() function is used which has a complexity of O(d), where d is the number of digits in the weight of a node, however since the weight of any node is an integer so it can only have 10 digits at max hence this function does not affect the overall time complexity. Therefore, the time complexity is O(N).
Auxiliary Space : O(1).
Any extra space is not required, so the space complexity is constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...