Introduction

Welcome to a delightful lesson on list traversal! Today, we invite you to join an endearing bunny named Gloria on an intricate quest. Gloria has a soft spot for number games, especially when they involve hopping between lists. Our goal on this exciting journey is to assist Gloria with her escapade and identify the maximum value she encounters along the way. Are you ready to embark on this adventure?

Task Statement

Gloria's quest unravels with two lists, both brimming with non-negative integers. Starting at the first element of listA, she leaps to listB based on the index she discovers in listA. She then bounces back to listA according to the index she stumbles upon in listB. Gloria repeats these hops until she returns to where she started in listA. What an adventure!

Your challenge is to craft a C# method that aids Gloria on her trip. The method will take two lists of integers as inputs, representing listA and listB. The objective is to find the highest value from listB that Gloria jumps to during her voyage.

It is guaranteed that at some point Gloria returns to the starting position.

Example

If listA = {2, 4, 3, 1, 2} and listB = {4, 0, 3, 2, 0}, the output should be 3.

In this scenario, Gloria starts from the first element of listA, which is 2. Then, she jumps to listB at index 2, where she discovers 3. She then bounces back to listA at index 3, where she arrives at 1. From there, she leaps back to listB at index 1, stumbling upon a 0. Finally, she bounces back to listA at index 0, the location where she started her adventure. Hence, she stops here, and during this journey, she came across the highest value, 3, from listB.

Solution Building: Step 1 - Initialization

Before we make headway with our code, let's kickstart with the initialization of variables in C#. Let indexA and indexB denote the last positions of Gloria in listA and listB, respectively. We will also use maxValue for tracking the highest value encountered in listB. Her quest starts from listA, so we also maintain a Boolean flag inListA.

int indexA = 0;
int? indexB = null;
bool inListA = true;
double maxValue = double.MinValue;
Solution Building: Step 2 - List Hopping
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal