Problem · Array

Box Fro~yo

EasyBoxOA

The Box office is thinking about investing in a rotating frozen yogurt (froyo) machine. The froyo machine has a menu with frozen yogurt flavors that may be included. The machine must move through the flavors one at a time, either moving left or right. The machine is circular, so when the last flavor in the flavors array is reached in either direction, the next flavor is at the other end of the array.

Given the name of the next flavor needed, determine the minimum number of left or right moves to reach it.

Function Description Complete the function flavorchanger in the editor below.

flavorchanger has the following parameter(s):

  • String[] flavors[n]: array of flavor names arranged in the order they appear in the frozen yogurt machine
  • int startIndex: index of the flavor currently dispensing
  • String target: name of the flavor needed
  • Returns
    int: minimum number of moves required to reach the desired flavor

    Constraints

    • 1 ≤ n ≤ 100
    • 0 ≤ startIndex ≤ n-1
    • 1 ≤ length of flavors[i] and target ≤ 100
    • target is in flavors

    Examples
    01 · Example 1
    flavors = ["originalart", "banana", "custard", "mango"]
    startIndex = 1
    target = "banana"
    return = 1
    The flavor currently dispensing is banana at index 1. The desired flavor is originalart at index 0. It can be reached by moving right 3 steps or left 1 step. The minimum number of moves is 1.
    Constraints
    • 1 ≤ n ≤ 100
    • 0 ≤ startIndex ≤ n-1
    • 1 ≤ length of flavors[i] and target ≤ 100
    • target is in flavors
    More Box problems
    drafts saved locally
    public int flavorchanger(String[] flavors, int startIndex, String target) {
      // write your code here
    }
    
    flavors["originalart", "banana", "custard", "mango"]
    startIndex1
    target"banana"
    expected1
    sign in to submit