Question
import java.util.*; import java.io.*; import java.math.*; class Solution { public static int findNetworkEndpoint(int startNodeId, int[] fromIds, int[] toIds) { int curNode = startNodeId; Vector vect
import java.util.*; import java.io.*; import java.math.*; class Solution { public static int findNetworkEndpoint(int startNodeId, int[] fromIds, int[] toIds) { int curNode = startNodeId; Vector vect = new Vector<>();; vect.add(curNode); int nextNode; boolean end = false; while(!end) { for(int i = 0; i < fromIds.length; i++) { if(fromIds[i] == curNode) { nextNode = toIds[i]; for(int it = 0; it < vect.size(); it++) { if(vect.get(it) == nextNode) { return curNode; } } curNode = nextNode; vect.add(curNode); break; } else if((fromIds[i] != curNode) && (i == (fromIds.length-1))) { end = true; } } } return curNode; } /* Ignore and do not change the code below */ public static void main(String args[]) { Scanner in = new Scanner(System.in); int startNodeId = in.nextInt(); int n = in.nextInt(); int[] fromIds = new int[n]; for (int i = 0; i < n; i++) { fromIds[i] = in.nextInt(); } int[] toIds = new int[n]; for (int i = 0; i < n; i++) { toIds[i] = in.nextInt(); } PrintStream outStream = System.out; System.setOut(System.err); int endPointId = findNetworkEndpoint(startNodeId, fromIds, toIds); System.setOut(outStream); System.out.println(endPointId); } /* Ignore and do not change the code above */ }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started