Problem solution · C++

CCC 2023 S5 - The Filter

CCC 2023 S5 - The Filter: a C++ solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Sliding window or two pointers
Source
CCCSolutions
Length
86 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For CCC 2023 S5 - The Filter, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 86 lines of C++ from the credited upstream file ccc23s5.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • 3 loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2023 S5 - The Filter · C++C++
Use this to learn the idea, then write your own version.
//By Timothy Shnayder, Newmarket Highschool //To solve this problem, we first do a rough filter, then remove the bad ones with a precise filter #include <iostream>#include <math.h>#include <unordered_map>#include <vector>#define int long longusing namespace std;double n;  vector<int> finalN;  //first do a rough check to get the working numbers + some bad ones we will get rid of latervoid filter(double l, double r, int iteration){    if(iteration == 0){ // end of filters        for(int i = ceil(l); i <= floor(r); i++){            finalN.push_back(i);        }    }else{//not final iteration          //split into 3 sections seperated by these edge markers        double leftEdge = (r-l)/3 + l;        double rightEdge = 2*(r-l)/3 + l;          //search into left and right ones. Middle is discarded        filter(l, leftEdge, iteration-1);        filter(rightEdge, r, iteration-1);    }}  //our goal is to to find if its in cantor without using doubles because it's inaccurate//if it reaches some int more than once, that means it will cycle and it is in the cantor//if it hits the middle, its badbool preciseFilter(int x){    if(x == 0){        return true;    }    unordered_map<int, bool> cycle;      while(true){        if(x*3 <= n){//left side            x*=3;            if(cycle[x]){                return true;            }            cycle[x] = true;        }else if(x*3 >= n*2){//right side            x*=3;            x-=n*2;            if(cycle[x]){                return true;            }            cycle[x] = true;        }else{ //middle            return false;        }    }  }  signed main() {    cin >> n;      filter(0.0, n, 20);    for(auto i: finalN){        if(preciseFilter(i)){            cout << i << '\n';        }      }      return 0;}

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗