using System;
using System.Collections.Generic;
class Program
{
static int SmallestSubArrayLen(int target, List<int> nums)
{
int n = nums.Count;
if (n == 0)
return 0;
int ans = int.MaxValue - 1;
List<int> sums = new List<int>(n + 1);
sums.Add(0);
for (int i = 1; i <= n; i++)
sums.Add(sums[i - 1] + nums[i - 1]);
for (int i = 1; i <= n; i++)
{
int toFind = target + sums[i - 1];
int bound = BinarySearch(sums, toFind);
if (bound != sums.Count && sums[bound] != toFind)
{
int len = bound - (i - 1);
ans = Math.Min(ans, len);
}
}
return (ans != int.MaxValue - 1) ? ans : 0;
}
static int BinarySearch(List<int> sums, int target)
{
int left = 0;
int right = sums.Count - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (sums[mid] == target)
return mid;
else if (sums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return left;
}
static void Main()
{
List<int> arr1 = new List<int> { 1, 4, 45, 6, 10, 19 };
int target1 = 51;
Console.WriteLine("Length of Smallest Subarray: " + SmallestSubArrayLen(target1, arr1));
List<int> arr2 = new List<int> { 1, 10, 5, 2, 7 };
int target2 = 9;
Console.WriteLine("Length of Smallest Subarray: " + SmallestSubArrayLen(target2, arr2));
List<int> arr3 = new List<int> { 1, 1, 1, 1, 1, 1, 1, 1 };
int target3 = 11;
Console.WriteLine("Length of Smallest Subarray: " + SmallestSubArrayLen(target3, arr3));
List<int> arr4 = new List<int> { 1, 11, 100, 1, 0, 200, 3, 2, 1, 250 };
int target4 = 280;
Console.WriteLine("Length of Smallest Subarray: " + SmallestSubArrayLen(target4, arr4));
}
}