Two Furthest Houses With Different Colors

There are n houses evenly lined up on the street, and each house is beautifully painted. You are given a 0-indexed integer array colors of length n, where colors[i] represents the color of the i^th house.

Return the maximum distance between two houses with different colors.

The distance between the i^th and j^th houses is abs(i - j), where abs(x) is the absolute value of x.

Example 1
Inputcolors = [1,1,1,6,1,1,1]
Output3
The furthest two houses with different colors are house 0 and house 3, giving distance abs(0 - 3) = 3; houses 3 and 6 can also produce the optimal answer.
Example 2
Inputcolors = [1,8,3,8,3]
Output4
The furthest two houses with different colors are house 0 and house 4, giving distance abs(0 - 4) = 4.

Constraints

  • n == colors.length
  • 2 <= n <= 100
  • 0 <= colors[i] <= 100
  • Test data are generated such that at least two houses have different colors.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate