#include <iostream>
#include <algorithm>
int nyaa(int *arr, unsigned int size, int val) {
if (size == 0)
return -1;
if (size == 1 && arr[0] == val) {
return 0;
}
else if (size == 1 && arr[0] != val) {
return -1;
}
unsigned int del = size / 2;
int left = std::accumulate(arr, arr + del, 0, std::plus<int>());
int right = std::accumulate(arr + del, arr + size, 0, std::plus<int>());
if (left > right) {
int ret = nyaa(arr, del, val);
return (ret != -1) ? (size_t)arr + (size_t)ret - (size_t)arr : ret;
}
else if (left < right) {
int ret = nyaa(arr + del, size - del, val);
return (ret != -1) ? (size_t)arr + (size_t)del + (size_t)ret - (size_t)arr : ret;
}
else {
return -1;
}
}
int main()
{
int arr[50];
std::fill_n(arr, 50, 1);
srand(time(0));
arr[rand()%50] = 10;
int search = nyaa(arr, 50, 10);
if (search == -1) {
std::cout << "Not found" << std::endl;
}
else {
std::cout << "Found: arr[" << search << "] == " << arr[search] << std::endl;
}
}