Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions