#include <iostream> #include <algorithm> #include <vector> #include <iterator> using namespace std; int main(){ vector <int> array; int dp=0; int n,d; vector<int>::const_iterator j; cin >> n >> d; for (int i = 0; i < n; ++i) { array.push_back(0); cin >> array[i]; } sort(array.begin(),array.begin()+n); for (int i = 0; i < n-1; ++i) { for ( j=lower_bound(array.begin()+i, array.end, array[i]+d); j < n-1; ++j) { if (*j-1!=0) { if (array[*j]!=array[*j-1]) { break; } } dp+=1; } } cout << dp; return 0; } 

I do not know how to make it work. I have already tried all the options (auto, iterator, const_iterator). everything breaks exactly on the line with lower_bound.

no matching function for call to 'lower_bound (__ gnu_cxx :: __ normal_iterator>,, __gnu_cxx :: __ alloc_traits> :: value_type)'

  • For starters, where are the brackets after array.end ? - HolyBlackCat
  • Further, j < n-1 compares the iterator and the index. You can not do it this way. - HolyBlackCat

1 answer 1

To correct compilation errors you can write

 for ( j = lower_bound(array.begin() + i, array.end(), array[i] + d); j < (array.begin() + n - 1); ++j ) 

there are still clearly needed checks for going beyond the array when indexing, for example, before the array[*j]