Problem · Array
EasyBox logoBoxOA

Problem statement

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

flavorchanger(flavors: String[], startIndex: int, target: String) → int

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

    Example 1

    flavors = ["originalart","banana","custard","mango"]startIndex = 1target = "originalart"return = 1

    The machine starts at banana, index 1. The target originalart is at index 0. Moving left reaches it in 1 move, while moving right takes 3 moves, so the minimum 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"originalart"
    expected1
    checking account