Problem Brief

Box Fro~yo

OA

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

    1Example 1

    Input
    flavors = ["originalart", "banana", "custard", "mango"], startIndex = 1, target = "banana"
    Output
    1
    Explanation
    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

    Limits and guarantees your solution can rely on.

    • 1 ≤ n ≤ 100
    • 0 ≤ startIndex ≤ n-1
    • 1 ≤ length of flavors[i] and target ≤ 100
    • target is in flavors
    public int flavorchanger(String[] flavors, int startIndex, String target) {
      // write your code here
    }
    
    Input

    flavors

    ["originalart", "banana", "custard", "mango"]

    startIndex

    1

    target

    "banana"

    Output

    1

    Sign in to submit your solution.