```python
def max_subarray_product(arr):
    """
    Finds the maximum product of a contiguous subarray in the given array.
    """
    if not arr:
        return 0

    # Initialize the result and the current min/max products with the first element
    res = arr[0]
    cur_min, cur_max = arr[0], arr[0]

    # Iterate through the array starting from the second element
    for i in range(1, len(arr)):
        num = arr[i]
        
        # We need to store the previous cur_max because it's used to calculate the new cur_min
        temp_max = cur_max
        
        # The new maximum product ending at the current position can be one of three things:
        # 1. The number itself (starting a new subarray).
        # 2. The product of the number and the previous maximum product.
        # 3. The product of the number and the previous minimum product (this is important when num is negative).
        cur_max = max(num, num * temp_max, num * cur_min)
        
        # Similarly, calculate the new minimum product ending at the current position.
        cur_min = min(num, num * temp_max, num * cur_min)
        
        # Update the overall maximum product found so far.
        res = max(res, cur_max)
        
    return res
```