Skip to content

Instantly share code, notes, and snippets.

@hikilaka
Created February 24, 2017 02:03
Show Gist options
  • Save hikilaka/c54287a708bfb569b71361a01bcc5d5b to your computer and use it in GitHub Desktop.
Save hikilaka/c54287a708bfb569b71361a01bcc5d5b to your computer and use it in GitHub Desktop.
int adjacentElementsProduct(std::vector<int> inputArray) {
auto begin = std::begin(inputArray);
auto end = std::end(inputArray);
if (inputArray.size() < 2) {
// what do we return in this instance??
// is the input gauranteed to be at least 2 elements?
return 0;
}
int answer = *begin;
begin = std::next(begin);
int last;
answer *= *begin;
last = *begin;
begin = std::next(begin);
for (; begin != end; begin = std::next(begin)) {
int test = *begin * last;
if (test > answer) {
answer = test;
}
last = *begin;
}
return answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment