Help required in CF EDU two pointers topic

Question
I am solving the above problem on codeforces. I am unable to figure out my mistake (Current solution verdict is WA) . Basically I have used 2 pointer approach and I will remove elements from left if difference is greater than K, else I will continue to add it from the right side.

#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ_IN freopen("input.txt","r",stdin);
#define FILE_READ_OUT freopen("output.txt","w",stdout);
#define deb(x) cout<<#x<<":= "<<x<<"\n";
#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef long double ld;

void solve()
{
   // write your logic here
   int n; ll k;
   cin >> n >> k;
   std::vector<int> a(n);
   for (int i = 0; i < n; i++) cin >> a[i];
   int l = 0, r = 0;
   map<int, int> mp;
   ll cnt = 0;
   while (r < n)
   {
      mp[a[r]]++;
      int min = (*mp.begin()).first, max = (*mp.rbegin()).first;
      while (max - min > k)
      {
         if (mp[a[l]] > 1) mp[a[l]]--;
         else mp.erase(a[l]);
         min = (*mp.begin()).first;
         max = (*mp.rbegin()).first;
         l++;
      }
      cnt += (r - l + 1);
      r++;
   }
   cout << cnt << "\n";
}

int main()
{
   FAST
#ifndef ONLINE_JUDGE
   FILE_READ_IN
   FILE_READ_OUT
#endif
   int t = 1;  //cin >> t;
   while (t--)
   {
      solve();
   }
   return 0;
}

Please help me to figure out what I am missing in this solution.

use long long in vector, map and few other variables because array element can be up to 1e18

1 Like

Thanks. I didn’t notice that ai<=10^18.