Introduction

Welcome to an engaging lesson on slice 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 slices. Our goal on this exciting journey is to assist Gloria through 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 unfolds with two slices, both full of non-negative integers. Starting at the first element of sliceA, she leaps to sliceB based on the index she discovers in sliceA. She then bounces back to sliceA according to the index she stumbles upon in sliceB. Gloria repeats these hops until she returns to where she started in sliceA. What an adventure!

Your challenge is to craft a Go function that aids Gloria on her trip. The function will take two slices of integers as inputs, representing sliceA and sliceB. The objective is to find the highest value from sliceB that Gloria jumps to during her voyage.

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

Example: If sliceA = []int{2, 4, 3, 1, 6} and sliceB = []int{4, 0, 3, 2, 0}, the output should be 3.

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

Step 1 - Initialization

Before we progress with our code, let's kickstart with the initialization of variables.

  1. Determine indexB: This represents Gloria's first landing spot in sliceB. It is determined by the first element in sliceA. This step sets the starting point for tracking Gloria's progress in sliceB.

  2. Set indexA: After reaching the first landing in sliceB, the value at this position dictates Gloria's next jump back to sliceA. This ensures continuity in her hopping path.

  3. Initialize maxValue: The value at indexB in sliceB serves as the initial maxValue. This setup allows us to monitor the highest value Gloria encounters during her adventure through the slices.

indexB := sliceA[0] // Gloria's first landing spot in sliceB
indexA := sliceB[indexB] // Next position in sliceA based on value found in sliceB at indexB
maxValue := sliceB[indexB] // Initial highest value from the starting position in sliceB
inSliceA := true // Tracks if Gloria is currently in sliceA
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