# Solving Leetcode Interviews in Seconds with AI: Sum of Beauty in the Array


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "2012" 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 a 0-indexed integer array nums. For each index i (1 <= i <= nums.length - 2) the beauty of nums[i] equals:  2, if nums[j] < nums[i] < nums[k], for all 0 <= j < i and for all i < k <= nums.length - 1. 1, if nums[i - 1] < nums[i] < nums[i + 1], and the previous condition is not satisfied. 0, if none of the previous conditions holds.  Return the sum of beauty of all nums[i] where 1 <= i <= nums.length - 2.   Example 1:  Input: nums = [1,2,3] Output: 2 Explanation: For each index i in the range 1 <= i <= 1: - The beauty of nums[1] equals 2.  Example 2:  Input: nums = [2,4,6,4] Output: 1 Explanation: For each index i in the range 1 <= i <= 2: - The beauty of nums[1] equals 1. - The beauty of nums[2] equals 0.  Example 3:  Input: nums = [3,2,1] Output: 0 Explanation: For each index i in the range 1 <= i <= 1: - The beauty of nums[1] equals 0.    Constraints:  3 <= nums.length <= 105 1 <= nums[i] <= 105  

	# Explanation
	Here's a breakdown of the solution:

*   **Precompute Minimums and Maximums:** To efficiently check the `nums[j] < nums[i] < nums[k]` condition, precompute the maximum value to the left of each index `i` and the minimum value to the right of each index `i`.
*   **Iterate and Evaluate:** Iterate through the relevant indices (1 to n-2) and check the beauty conditions based on the precomputed maximums and minimums and the immediate neighbors.
*   **Sum the Beauty:** Accumulate the beauty values for each index to obtain the final result.

*   **Time & Space Complexity:** O(n) time complexity, O(n) space complexity

	
	# Code
	```python
	def sum_of_beauty(nums):
    n = len(nums)
    max_left = [0] * n
    min_right = [0] * n

    max_left[0] = nums[0]
    for i in range(1, n):
        max_left[i] = max(max_left[i - 1], nums[i])

    min_right[n - 1] = nums[n - 1]
    for i in range(n - 2, -1, -1):
        min_right[i] = min(min_right[i + 1], nums[i])

    beauty_sum = 0
    for i in range(1, n - 1):
        if max_left[i - 1] < nums[i] < min_right[i + 1]:
            beauty_sum += 2
        elif nums[i - 1] < nums[i] < nums[i + 1]:
            beauty_sum += 1

    return beauty_sum
	```
			
