# Solving Leetcode Interviews in Seconds with AI: Maximum Height of a Triangle


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "3200" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> 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 <= 100  

	# Explanation
	Here's the breakdown of the solution:

*   **Binary Search:** Utilize binary search to efficiently find the maximum possible height of the triangle. The search space will be from 0 to `red + blue`.
*   **Feasibility Check:** For each potential height (`mid`) checked during binary search, determine if it's possible to construct the triangle with the given number of red and blue balls. This involves checking if there exists an arrangement of red and blue rows up to that height.
*   **Optimization:** The `check` function efficiently determines if a given height is feasible using mathematical formulas to avoid unnecessary loops.

*   **Runtime Complexity:** O(log(red + blue)), **Storage Complexity:** O(1)

	
	# Code
	```python
	def solve():
    red = int(input())
    blue = int(input())

    def check(height, red, blue):
        total_balls = height * (height + 1) // 2
        if total_balls > red + blue:
            return False

        for red_rows in range(height + 1):
            red_balls_used = red_rows * (red_rows + 1) // 2
            blue_balls_used = total_balls - red_balls_used
            
            if 0 <= red_balls_used <= red and 0 <= blue_balls_used <= blue:
                
                valid = True
                
                if red_rows > 0:
                    if red_rows == height:
                        pass
                    elif (red_rows+1)*(red_rows+2)//2 <= red :
                        pass
                    else:
                        continue
                
                if (height - red_rows) > 0 :
                    if (height - red_rows) == height:
                        pass
                    elif (height-red_rows+1)*(height-red_rows+2)//2 <= blue:
                        pass
                    else:
                        continue
                
                if height == 1:
                    return True
                
                alt_needed = True
                
                if red_rows == 0 or red_rows == height:
                    return True
                
                if red_rows>0 and red_rows < height:
                    return True

        return False

    low = 0
    high = red + blue
    ans = 0

    while low <= high:
        mid = (low + high) // 2
        if check(mid, red, blue):
            ans = mid
            low = mid + 1
        else:
            high = mid - 1

    print(ans)

solve()
	```
			
