Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 ith house.
Return the maximum distance between two houses with different colors.
The distance between the ith and jth houses is abs(i - j), where abs(x) is the absolute value of x.
Example 1:
Input: colors = [1,1,1,6,1,1,1] Output: 3 Explanation: In the above image, color 1 is blue, and color 6 is red. The furthest two houses with different colors are house 0 and house 3. House 0 has color 1, and house 3 has color 6. The distance between them is abs(0 - 3) = 3. Note that houses 3 and 6 can also produce the optimal answer.
Example 2:
Input: colors = [1,8,3,8,3] Output: 4 Explanation: In the above image, color 1 is blue, color 8 is yellow, and color 3 is green. The furthest two houses with different colors are house 0 and house 4. House 0 has color 1, and house 4 has color 3. The distance between them is abs(0 - 4) = 4.
Example 3:
Input: colors = [0,1] Output: 1 Explanation: The furthest two houses with different colors are house 0 and house 1. House 0 has color 0, and house 1 has color 1. The distance between them is abs(0 - 1) = 1.
Constraints:
n == colors.length2 <= n <= 1000 <= colors[i] <= 100Problem summary: 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 ith house. Return the maximum distance between two houses with different colors. The distance between the ith and jth houses is abs(i - j), where abs(x) is the absolute value of x.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,1,1,6,1,1,1]
[1,8,3,8,3]
[0,1]
replace-elements-with-greatest-element-on-right-side)maximum-distance-between-a-pair-of-values)maximum-difference-between-increasing-elements)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2078: Two Furthest Houses With Different Colors
class Solution {
public int maxDistance(int[] colors) {
int ans = 0, n = colors.length;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (colors[i] != colors[j]) {
ans = Math.max(ans, Math.abs(i - j));
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2078: Two Furthest Houses With Different Colors
func maxDistance(colors []int) int {
ans, n := 0, len(colors)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if colors[i] != colors[j] {
ans = max(ans, abs(i-j))
}
}
}
return ans
}
func abs(x int) int {
if x >= 0 {
return x
}
return -x
}
# Accepted solution for LeetCode #2078: Two Furthest Houses With Different Colors
class Solution:
def maxDistance(self, colors: List[int]) -> int:
ans, n = 0, len(colors)
for i in range(n):
for j in range(i + 1, n):
if colors[i] != colors[j]:
ans = max(ans, abs(i - j))
return ans
// Accepted solution for LeetCode #2078: Two Furthest Houses With Different Colors
/**
* [2078] 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:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/10/31/eg1.png" style="width: 610px; height: 84px;" />
* Input: colors = [<u>1</u>,1,1,<u>6</u>,1,1,1]
* Output: 3
* Explanation: In the above image, color 1 is blue, and color 6 is red.
* The furthest two houses with different colors are house 0 and house 3.
* House 0 has color 1, and house 3 has color 6. The distance between them is abs(0 - 3) = 3.
* Note that houses 3 and 6 can also produce the optimal answer.
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/10/31/eg2.png" style="width: 426px; height: 84px;" />
* Input: colors = [<u>1</u>,8,3,8,<u>3</u>]
* Output: 4
* Explanation: In the above image, color 1 is blue, color 8 is yellow, and color 3 is green.
* The furthest two houses with different colors are house 0 and house 4.
* House 0 has color 1, and house 4 has color 3. The distance between them is abs(0 - 4) = 4.
*
* Example 3:
*
* Input: colors = [<u>0</u>,<u>1</u>]
* Output: 1
* Explanation: The furthest two houses with different colors are house 0 and house 1.
* House 0 has color 0, and house 1 has color 1. The distance between them is abs(0 - 1) = 1.
*
*
* Constraints:
*
* n == colors.length
* 2 <= n <= 100
* 0 <= colors[i] <= 100
* Test data are generated such that at least two houses have different colors.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/two-furthest-houses-with-different-colors/
// discuss: https://leetcode.com/problems/two-furthest-houses-with-different-colors/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn max_distance(colors: Vec<i32>) -> i32 {
let mut n = colors.len() - 1;
let mut l_most = 0usize.abs_diff(colors.iter().rposition(|x| *x != colors[0]).unwrap());
let mut r_most = n.abs_diff(colors.iter().position(|x| *x != colors[n]).unwrap());
std::cmp::max(l_most, r_most) as i32
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2078_example_1() {}
}
// Accepted solution for LeetCode #2078: Two Furthest Houses With Different Colors
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2078: Two Furthest Houses With Different Colors
// class Solution {
// public int maxDistance(int[] colors) {
// int ans = 0, n = colors.length;
// for (int i = 0; i < n; ++i) {
// for (int j = i + 1; j < n; ++j) {
// if (colors[i] != colors[j]) {
// ans = Math.max(ans, Math.abs(i - j));
// }
// }
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.