''Container With Most Water" Problem and it's Solution | C# | Unity Game Engine


Solution.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Solution : MonoBehaviour
{
    public int[] heightArray = { 1, 8, 6, 2, 5, 4, 8, 3, 7 };

    void Start()
    {
        Debug.Log(MaxArea(heightArray));
    }

    public int MaxArea(int[] height)
    {
        int leftIndex = 0;
        int rightIndex = height.Length - 1;
        int maxArea = 0;

        while(leftIndex < rightIndex)
        {
            int area = Math.Min(height[leftIndex], height[rightIndex]) * (rightIndex - leftIndex);
            maxArea = Math.Max(maxArea, area);
            if (height[leftIndex] < height[rightIndex])
                ++leftIndex;
            else if (height[rightIndex] < height[leftIndex])
                --rightIndex;
            else
            {
                ++leftIndex;
                --rightIndex;
            }
        }

        return maxArea;
    }
}