Я использую двоичный поиск, как показано в приведенном ниже коде
int[] cost=[1,2,3,5,6]
money=5
static void whatFlavors(int[] cost, int money)
{
int[] costSort = (int[])cost.Clone();
Array.Sort(costSort);
for (int i = 0; i < costSort.Length; i++)
{
int complement = money - costSort[i];
int location= Array.BinarySearch(costSort, i+1, costSort.Length,complement);
if(location >=0 && location <cost.Length && costSort[location]== complement)
{
int[] indicies = getindicies(cost, cost[i], complement);
break;
}
}
}
, но получаю исключение на Array.BinarySearch
Смещение и длина вышли за допустимые пределы. границы для массива или счетчика больше, чем количество элементов от индекса до конца исходной коллекции