GetMinNumBoats.cs

CSharp · Console

/Program.cs

using System;
					
public class Program
{
	public static int GetMinNumBoats(int[] arr, int limit) {
        Array.Sort(arr);

        int res = 0;
        int i = 0;
        int j = arr.Length - 1;

        while (i <= j)
        {
            if (i == j)
            {
                res++;
                break;
            }

            if (arr[i] + arr[j] <= limit)
            {
                res++;
                i++;
                j--;
                continue;
            }

            res++;
            j--;
        }

        return res;
    }
	
	public static void Main()
	{
		Console.WriteLine("UniLecs");
		
		// tests
		Console.WriteLine(GetMinNumBoats(new int[] { 1, 2 }, 3).ToString()); // 1
		Console.WriteLine(GetMinNumBoats(new int[] { 3,2,2,1 }, 3).ToString()); // 3
	}
}
;