Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // you can use includes, for example:
- // #include <algorithm>
- // you can write to stdout for debugging purposes, e.g.
- // cout << "this is a debug message" << endl;
- #include "unordered_map"
- #include <unordered_map>
- #include <set>
- int solution(vector<int> &A) {
- // write your code in C++14 (g++ 6.2.0)
- unsigned int size = A.size();
- std::unordered_map<int, std::vector<unsigned int>> pairIndices;
- std::set<int> values;
- for(unsigned int i = 0; i < size - 1; i++){
- int pair = A.at(i) + A.at(i + 1);
- values.insert(pair);
- if(pairIndices.count(pair) == 0){
- std::vector<unsigned int> test {i};
- pairIndices.insert({pair, test});
- } else {
- pairIndices[pair].push_back(i);
- }
- }
- int max = 0;
- for(int num: values){
- std::vector<unsigned int> indices = pairIndices[num];
- std::vector<unsigned int> neighbourCount;
- int indCount = indices.size();
- std::vector<int> toDel;
- for(int i = 1; i < indCount; i++){
- if(indices.at(i - 1) + 1 == indices.at(i)){
- toDel.push_back(i);
- indices.at(i) = -1;
- }
- }
- int total = indCount - toDel.size();
- if(total > max){
- max = total;
- }
- }
- return max;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement