count distinct slices in an array
<p>I was trying to solve <a href="https://codility.com/programmers/lessons/15-caterpillar_method/count_distinct_slices/" rel="nofollow noreferrer">this</a> problem.</p>

<blockquote>
  <p>An integer M and a non-empty zero-indexed array A consisting of N
  non-negative integers are given. All integers in array A are less than
  or equal to M.</p>
  
  <p>A pair of integers (P, Q), such that 0 ≤ P ≤ Q &lt; N, is called a slice
  of array A. The slice consists of the elements A[P], A[P + 1], ...,
  A[Q]. A distinct slice is a slice consisting of only unique numbers.
  That is, no individual number occurs more than once in the slice.</p>
  
  <p>For example, consider integer M = 6 and array A such that:</p>

<pre><code>A[0] = 3
A[1] = 4
A[2] = 5
A[3] = 5
A[4] = 2 
</code></pre>
  
  <p>There are exactly nine distinct slices: (0, 0), (0, 1), (0, 2), (1,
  1), (1,2), (2, 2), (3, 3), (3, 4) and (4, 4).</p>
  
  <p>The goal is to calculate the number of distinct slices.</p>
</blockquote>

<p>Thanks in advance.</p>

<pre><code>#include &lt;algorithm&gt;
#include &lt;cstring&gt;
#include &lt;cmath&gt;
#define MAX 100002

// you can write to stdout for debugging purposes, e.g.
// cout &lt;&lt; "this is a debug message" &lt;&lt; endl;

using namespace std;

bool check[MAX];

int solution(int M, vector&lt;int&gt; &amp;A) {
    memset(check, false, sizeof(check));
    int base = 0;
    int fibot = 0;
    int sum = 0;

    while(fibot &lt; A.size()){

        if(check[A[fibot]]){
            base = fibot;
        }

        check[A[fibot]] = true;

   sum += fibot - base + 1;
        fibot += 1;  
    }

    return min(sum, 1000000000);
}
</code></pre>