Advertisement
matbiz01

Untitled

Oct 12th, 2022
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. // you can use includes, for example:
  2. // #include <algorithm>
  3.  
  4. // you can write to stdout for debugging purposes, e.g.
  5. // cout << "this is a debug message" << endl;
  6.  
  7. #include "unordered_map"
  8. #include <unordered_map>
  9. #include <set>
  10. int solution(vector<int> &A) {
  11.     // write your code in C++14 (g++ 6.2.0)
  12.  
  13.     unsigned int size = A.size();
  14.     std::unordered_map<int, std::vector<unsigned int>> pairIndices;
  15.  
  16.     std::set<int> values;
  17.  
  18.     for(unsigned int i = 0; i < size - 1; i++){
  19.         int pair = A.at(i) + A.at(i + 1);
  20.         values.insert(pair);
  21.         if(pairIndices.count(pair) == 0){
  22.             std::vector<unsigned int> test {i};
  23.             pairIndices.insert({pair, test});
  24.         } else {
  25.             pairIndices[pair].push_back(i);
  26.         }
  27.     }
  28.     int max = 0;
  29.     for(int num: values){
  30.         std::vector<unsigned int> indices = pairIndices[num];
  31.         std::vector<unsigned int> neighbourCount;
  32.  
  33.         int indCount = indices.size();
  34.  
  35.         std::vector<int> toDel;
  36.         for(int i = 1; i < indCount; i++){
  37.             if(indices.at(i - 1) + 1 == indices.at(i)){
  38.                 toDel.push_back(i);
  39.                 indices.at(i) = -1;
  40.             }
  41.         }
  42.  
  43.         int total = indCount - toDel.size();
  44.         if(total > max){
  45.             max = total;
  46.         }
  47.     }
  48.     return max;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement