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.
You are given two integers red and blue representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1st row will have 1 ball, the 2nd row will have 2 balls, the 3rd row will have 3 balls, and so on.
All the balls in a particular row should be the same color, and adjacent rows should have different colors.
Return the maximum height of the triangle that can be achieved.
Example 1:
Input: red = 2, blue = 4
Output: 3
Explanation:
The only possible arrangement is shown above.
Example 2:
Input: red = 2, blue = 1
Output: 2
Explanation:
The only possible arrangement is shown above.
Example 3:
Input: red = 1, blue = 1
Output: 1
Example 4:
Input: red = 10, blue = 1
Output: 2
Explanation:
The only possible arrangement is shown above.
Constraints:
1 <= red, blue <= 100Problem summary: You are given two integers red and blue representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1st row will have 1 ball, the 2nd row will have 2 balls, the 3rd row will have 3 balls, and so on. All the balls in a particular row should be the same color, and adjacent rows should have different colors. Return the maximum height of the triangle that can be achieved.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
2 4
2 1
1 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3200: Maximum Height of a Triangle
class Solution {
public int maxHeightOfTriangle(int red, int blue) {
int ans = 0;
for (int k = 0; k < 2; ++k) {
int[] c = {red, blue};
for (int i = 1, j = k; i <= c[j]; j ^= 1, ++i) {
c[j] -= i;
ans = Math.max(ans, i);
}
}
return ans;
}
}
// Accepted solution for LeetCode #3200: Maximum Height of a Triangle
func maxHeightOfTriangle(red int, blue int) (ans int) {
for k := 0; k < 2; k++ {
c := [2]int{red, blue}
for i, j := 1, k; i <= c[j]; i, j = i+1, j^1 {
c[j] -= i
ans = max(ans, i)
}
}
return
}
# Accepted solution for LeetCode #3200: Maximum Height of a Triangle
class Solution:
def maxHeightOfTriangle(self, red: int, blue: int) -> int:
ans = 0
for k in range(2):
c = [red, blue]
i, j = 1, k
while i <= c[j]:
c[j] -= i
j ^= 1
ans = max(ans, i)
i += 1
return ans
// Accepted solution for LeetCode #3200: Maximum Height of a Triangle
/**
* [3200] Maximum Height of a Triangle
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn max_height_of_triangle(red: i32, blue: i32) -> i32 {
let mut result = 0;
let mut row = 1;
let mut switch = true;
let (mut first_count, mut second_count) = (0, 0);
loop {
if switch {
first_count += row;
} else {
second_count += row;
}
if first_count > red || second_count > blue {
if second_count > red || first_count > blue {
break;
}
}
switch = !switch;
row += 1;
result += 1;
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3200() {
assert_eq!(3, Solution::max_height_of_triangle(2, 4));
assert_eq!(2, Solution::max_height_of_triangle(2, 1));
assert_eq!(1, Solution::max_height_of_triangle(1, 1));
assert_eq!(2, Solution::max_height_of_triangle(10, 1));
assert_eq!(5, Solution::max_height_of_triangle(10, 10));
}
}
// Accepted solution for LeetCode #3200: Maximum Height of a Triangle
function maxHeightOfTriangle(red: number, blue: number): number {
let ans = 0;
for (let k = 0; k < 2; ++k) {
const c: [number, number] = [red, blue];
for (let i = 1, j = k; i <= c[j]; ++i, j ^= 1) {
c[j] -= i;
ans = Math.max(ans, i);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.