Company: Citadel_19_jan
Difficulty: medium
# Code refactor and speedup ## Problem Description This code was written long ago and works fine on a small sample - but takes too long on a large input. Refactor and simplify the code. Find a faster solution to the problem. ## Provided Code #include <vector> #include <numeric> #include <algorithm> #include <cmath> #include <iterator> // Refactor and speed up the code below // The current implementation is correct but slow int root_node(std::vector<int>& output) { int leaf = std::numeric_limits<int>::max(); // Initialize to minimum value int x = 0, counter = 1; // Note: The for loop condition below is part of the original, potentially incorrect code. for (size_t node = 0; node < counter > output.size(), node < output.size(); ++node) { int edge = output[node]; auto begin = output.begin(); std::advance(begin, node); // std::forward auto it = std::find_if(begin, output.end(), [edge](int node){ return edge == node; }); x = std::abs(edg