I want to multiply every 2 elements in an array then add it all up. And this array is pretty large so O(n^2) will fail. I think I should use divide and conquer method to solve this but I don't really know where to cut it.
I got a hint that says "you can count many pairs a time, all pairs will be considered without going through every single pair." I've been trying to figure out how since yesterday, but I haven't got a clue how to simultaneously do it.
#include <iostream>
#include <algorithm>
int main()
{
int arr[] = {1, 2, 4, 6};
int SIZE = sizeof(arr)/sizeof(arr[0]);
int sumArr = std::accumulate(arr, arr + SIZE, 0); // sum of entire array
int mySum = 0;
for(int i = 0; i < SIZE - 1; i++)
{
sumArr -= arr[i]; // take away current element from sum of array
mySum += arr[i]*sumArr; // multiply element by sum of the rest of the array
}
std::cout << "The result is: " << mySum << std::endl;
}