I can't get a perfect score on Diamond Collector

Diamond Collector, USACO

#include <fstream>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;

ifstream cin ("diamond.in") ;
ofstream cout ("diamond.out") ;

int main() {
	int n, k, ans=0, maxx=0 ;
	cin >> n >> k ;
	vector<int> v1(n) ;
	for (int i=0; i<n; i++) {
		cin >> v1[i] ;
		maxx = max(v1[i], maxx) ;

	for (int i=0; i<maxx-k; i++) {
		int temp_ans=0 ;
		for (int j=0; j<n; j++) {
			if (i < v1[j] && v1[j] < i+k+2) {
				temp_ans++ ;
		ans = max(temp_ans, ans) ;
	cout << ans << endl ;
	return 0 ;

running this code, I got an error on test case 6. I checked the test case, but it’s too long for me to debug. Can someone find something I did wrong?

What if the optimal solution occurs when i == maxx - k?

As suggested in How to ask for help on a problem, it’s generally a good idea to try generating your own small test cases and comparing the outputs of your solution against that of the model solution. This should easily detect mistakes like the one mentioned in the post above.