Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth AltF4 and Spinal Network problem solution

YASH PAL, 31 July 202417 February 2026
In this HackerEarth AltF4 and Spinal Network problem solution AltF4 is into his networking class and his teacher introduces him to bus topology. Being bored of the class, he his lost into his own thoughts where he discovers a new topology of his own – the Spinal Network.
 
In this topology, we have a central line just like the bus topology. All the nodes (or hops, or computers) are either in the central line itself, or it is directly connected to the central line. To make things easier he says that the topology given should be a tree. Resembles the spinal cord, isn’t it? Given a tree, you need to figure out if it is spinal or not.
 
 
HackerEarth AltF4 and Spinal Network problem solution

 

 

HackerEarth AltF4 and Spinal Network problem solution.

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Spinal {
public static void main(String args[]) throws IOException {
Scanner in = new Scanner(new FileReader("C://Users//Downloads//tree1.in"));
PrintWriter out = new PrintWriter(new FileWriter("C://Users//Downloads//tree1.out"));
int t = in.nextInt();
while(t-- > 0) {
int n = in.nextInt();
int degree[] = new int[n];
int edgeTo[] = new int[n];
int i;
for(i=1;i<n;i++) {
int u = in.nextInt() - 1, v = in.nextInt() - 1;
degree[u]++;//Increase degree of the vertex
degree[v]++;//Increase degree of the vertex
//Lets make a simplification here.
//We are bothered about only to find out leaf nodes
//So we store information of the node it has its edge to.
//If it has degree > 1, of course we are not going to check it.
edgeTo[u] = v;
edgeTo[v] = u;
}
boolean isLeaf[] = new boolean[n];
for(i=0;i<n;i++) {
if(degree[i] == 1) { //Leaf node
isLeaf[i] = true;
}
}
for(i=0;i<n;i++) {
if(isLeaf[i]) {
//Delete this edge
degree[edgeTo[i]]--;
degree[i]--;
}
}
String isSpinal = "YES";
for(i=0;i<n;i++) {
if(degree[i] > 2) {
isSpinal = "NO";
break;
}
}
out.println(isSpinal);
}
out.flush();
}

}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes