Construct an N-ary Tree having no pair of adjacent nodes with same weight from given weights
Given an array weights[] consisting of N positive integer, where weights[i] denotes the weight of ith node, the task is to construct an N-ary tree such that no two directly connected nodes have same weight. If it is possible to make such a tree, then print “Yes” along with their edges. Otherwise, print “No”.
Examples:
Input: weights[] = {1 2 1 2 5}
Output:
Yes
1 2
1 4
1 5
2 3
Explanation:
Index: 1 2 3 4 5
Weight : 1 2 1 2 5
The constructed Tree is shown in the following diagram:
Input: weights[] = {1 1 1}
Output: No
Explanation: Since all weights are already same, no such tree can be constructed.
Approach: The idea to solve this problem is to first check if all nodes are assigned with same weight or not. If found to be true, then required tree cannot be constructed. Otherwise, such a tree can be constructed. Therefore traverse the array weights[] and check if all values are the same or not. If found to be true, then print “No”. Otherwise, print “Yes” and construct a tree using the following steps:
- Take any node and make it the root node.
- Now, connect all other nodes with weights not equal to that of the root to the root node. Now the remaining nodes are the nodes that have a value equal to the root node.
- Choose any child node of the root node and connect all remaining nodes to them. Therefore, there exists no direct edge between nodes of same weight.
- To check which nodes have not been included yet, keep track of visited nodes using an auxiliary array visited[]. If a node is visited, join a node with it, but do not join the visited node with another node as joining an unvisited node with a visited node is possible, but not vice versa.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int visited[N];
void construct_tree( int weights[], int n)
{
int minimum = *min_element(weights, weights + n);
int maximum = *max_element(weights, weights + n);
if (minimum == maximum) {
cout << "No" ;
return ;
}
else {
cout << "Yes" << endl;
}
int root = weights[0];
visited[1] = 1;
for ( int i = 0; i < n; i++) {
if (weights[i] != root
&& visited[i + 1] == 0) {
cout << 1 << " "
<< i + 1 << " "
<< endl;
visited[i + 1] = 1;
}
}
int notroot = 0;
for ( int i = 0; i < n; i++) {
if (weights[i] != root) {
notroot = i + 1;
break ;
}
}
for ( int i = 0; i < n; i++) {
if (weights[i] == root
&& visited[i + 1] == 0) {
cout << notroot << " "
<< i + 1 << endl;
visited[i + 1] = 1;
}
}
}
int main()
{
int weights[] = { 1, 2, 1, 2, 5 };
int N = sizeof (weights) / sizeof (weights[0]);
construct_tree(weights, N);
}
|
Java
import java.lang.*;
import java.io.*;
import java.util.*;
class GFG{
static int N = 100000 + 5 ;
static int visited[] = new int [N];
static void construct_tree( int weights[], int n)
{
int minimum = Arrays.stream(weights).min().getAsInt();
int maximum = Arrays.stream(weights).max().getAsInt();
if (minimum == maximum)
{
System.out.println( "No" );
return ;
}
else
{
System.out.println( "Yes" );
}
int root = weights[ 0 ];
visited[ 1 ] = 1 ;
for ( int i = 0 ; i < n; i++)
{
if (weights[i] != root &&
visited[i + 1 ] == 0 )
{
System.out.println( 1 + " " +
(i + 1 ) + " " );
visited[i + 1 ] = 1 ;
}
}
int notroot = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (weights[i] != root)
{
notroot = i + 1 ;
break ;
}
}
for ( int i = 0 ; i < n; i++)
{
if (weights[i] == root &&
visited[i + 1 ] == 0 )
{
System.out.println(notroot + " " +
(i + 1 ));
visited[i + 1 ] = 1 ;
}
}
}
public static void main(String[] args)
{
int weights[] = { 1 , 2 , 1 , 2 , 5 };
int N = weights.length;
construct_tree(weights, N);
}
}
|
Python3
N = 10 * * 5 + 5
visited = [ 0 ] * N
def construct_tree(weights, n):
minimum = min (weights)
maximum = max (weights)
if (minimum = = maximum):
print ( "No" )
return
else :
print ( "Yes" )
root = weights[ 0 ]
visited[ 1 ] = 1
for i in range (n):
if (weights[i] ! = root
and visited[i + 1 ] = = 0 ):
print ( 1 ,i + 1 )
visited[i + 1 ] = 1
notroot = 0
for i in range (n):
if (weights[i] ! = root):
notroot = i + 1
break
for i in range (n):
if (weights[i] = = root
and visited[i + 1 ] = = 0 ):
print (notroot,i + 1 )
visited[i + 1 ] = 1
if __name__ = = '__main__' :
weights = [ 1 , 2 , 1 , 2 , 5 ]
N = len (weights)
construct_tree(weights, N)
|
C#
using System;
using System.Linq;
class GFG{
static int N = 100000 + 5;
static int [] visited = new int [N];
static void construct_tree( int [] weights, int n)
{
int minimum = weights.Min();
int maximum = weights.Max();
if (minimum == maximum)
{
Console.WriteLine( "No" );
return ;
}
else
{
Console.WriteLine( "Yes" );
}
int root = weights[0];
visited[1] = 1;
for ( int i = 0; i < n; i++)
{
if (weights[i] != root &&
visited[i + 1] == 0)
{
Console.WriteLine(1 + " " + (i + 1) + " " );
visited[i + 1] = 1;
}
}
int notroot = 0;
for ( int i = 0; i < n; i++)
{
if (weights[i] != root)
{
notroot = i + 1;
break ;
}
}
for ( int i = 0; i < n; i++)
{
if (weights[i] == root &&
visited[i + 1] == 0)
{
Console.WriteLine(notroot + " " +
(i + 1));
visited[i + 1] = 1;
}
}
}
public static void Main()
{
int [] weights = { 1, 2, 1, 2, 5 };
int N = weights.Length;
construct_tree(weights, N);
}
}
|
Javascript
<script>
let N = 100000 + 5;
let visited = new Array(N);
visited.fill(0);
function construct_tree(weights, n)
{
let minimum = Number.MAX_VALUE;
let maximum = Number.MIN_VALUE;
for (let i = 0; i < weights.length; i++)
{
minimum = Math.min(minimum, weights[i]);
maximum = Math.max(maximum, weights[i]);
}
if (minimum == maximum)
{
document.write( "No" );
return ;
}
else
{
document.write( "Yes" + "</br>" );
}
let root = weights[0];
visited[1] = 1;
for (let i = 0; i < n; i++)
{
if (weights[i] != root &&
visited[i + 1] == 0)
{
document.write(1 + " " +
(i + 1) + "</br>" );
visited[i + 1] = 1;
}
}
let notroot = 0;
for (let i = 0; i < n; i++)
{
if (weights[i] != root)
{
notroot = i + 1;
break ;
}
}
for (let i = 0; i < n; i++)
{
if (weights[i] == root &&
visited[i + 1] == 0)
{
document.write(notroot + " " +
(i + 1) + "</br>" );
visited[i + 1] = 1;
}
}
}
let weights = [ 1, 2, 1, 2, 5 ];
let n = weights.length;
construct_tree(weights, n);
</script>
|
Output:
Yes
1 2
1 4
1 5
2 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
22 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...