Created
February 24, 2017 02:03
-
-
Save hikilaka/c54287a708bfb569b71361a01bcc5d5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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