Path Maximum
Given a undirected tree with N nodes and N - 1 edges rooted at node 1.
We are given Q queries of type :
uv, where we need to find the maximum node value among nodes present in the simple path between node u and v.
Example
If N = 5, edge = {(1,2),(1,5),(1,4),(3,5)], Q = 2.
• If query 1 is 13. Then simple path between node 1 and 3 is 1-5 -3. Maximum value node on simple path is 5.
• If query 2 is 12. Then simple path between node 1 and 2 is 1-2. Maximum value node on simple path is 2.
Function Description
Complete the maxNode function provided in the editor. This function takes the following 4 parameters and returns an array denoting answer for
queries
N: Represents the number of nodes in tree.
• edge : Represents the edges present in tree.
• Q: Represents the number of queries in tree.
query : Represents the queries in form of 2 space separated integers.
Input :
1. First line contain integer N, denoting number of nodes in tree.
2. Next N - 1 lines contain two space separated integers u v, denoting an edge between node u and v.
3. Next line contain integer Q, denoting number of queries.
? Neyt lines contain two space separated integers u denotina query
Answers
Answer:
Path Maximum
Given a undirected tree with N nodes and N - 1 edges rooted at node 1.
We are given Q queries of type :
uv, where we need to find the maximum node value among nodes present in the simple path between node u and v.
Example
If N = 5, edge = {(1,2),(1,5),(1,4),(3,5)], Q = 2.
• If query 1 is 13. Then simple path between node 1 and 3 is 1-5 -3. Maximum value node on simple path is 5.
• If query 2 is 12. Then simple path between node 1 and 2 is 1-2. Maximum value node on simple path is 2.
Function Description
Complete the maxNode function provided in the editor. This function takes the following 4 parameters and returns an array denoting answer for
queries
N: Represents the number of nodes in tree.
• edge : Represents the edges present in tree.
• Q: Represents the number of queries in tree.
query : Represents the queries in form of 2 space separated integers.
Input :
1. First line contain integer N, denoting number of nodes in tree.
2. Next N - 1 lines contain two space separated integers u v, denoting an edge between node u and v.
3. Next line contain integer Q, denoting number of queries.
? Neyt lines contain two space separated integers u denotina query
Explanation:
Path Maximum
Given a undirected tree with N nodes and N - 1 edges rooted at node 1.
We are given Q queries of type :
uv, where we need to find the maximum node value among nodes present in the simple path between node u and v.
Example
If N = 5, edge = {(1,2),(1,5),(1,4),(3,5)], Q = 2.
• If query 1 is 13. Then simple path between node 1 and 3 is 1-5 -3. Maximum value node on simple path is 5.
• If query 2 is 12. Then simple path between node 1 and 2 is 1-2. Maximum value node on simple path is 2.
Function Description
Complete the maxNode function provided in the editor. This function takes the following 4 parameters and returns an array denoting answer for
queries
N: Represents the number of nodes in tree.
• edge : Represents the edges present in tree.
• Q: Represents the number of queries in tree.
query : Represents the queries in form of 2 space separated integers.
Input :
1. First line contain integer N, denoting number of nodes in tree.
2. Next N - 1 lines contain two space separated integers u v, denoting an edge between node u and v.
3. Next line contain integer Q, denoting number of queries.
? Neyt lines contain two space separated integers u denotina queryx=((A>B)||(A=C)) predict the output for this.
Answer:
n= int(input())
a = (list(map(int, input().split())))
n1=int(input())
a.sort()
if n1 == 2:
l = (list(map(int, input().split())))
r = (list(map(int, input().split())))
x=[]
for j in range(len(a)):
if (a[j] >= l[0]) and (a[j] <= l[-1]):
x.append(a[j])
y=[]
for j in range(len(a)):
if (a[j] >= r[0]) and (a[j] <= r[-1]):
y.append(a[j])
print(x[-1])
print(y[-1])
if n1==1:
l = (list(map(int, input().split())))
x=[]
for j in range(len(a)):
if (a[j] >= l[0]) and (a[j] <= l[-1]):
x.append(a[j])
print(x[-1])
Explanation: