I was able to resolve this issue as follows:
#include <iostream>
#include <algorithm>
using namespace std;
int contar_pares(int y, int x[], int tam){
int cont = 0;
for(int i = tam; i > 0; i--){
for(int j = tam - 1; j >= 0; j--){
if(x[i-1] - x[j] == y){
cont++;
}
}
}
return cont;
}
int main(){
int n, k;
cin >> n >> k;
int tamanho = n;
int vetor[tamanho];
for(int i = 0; i < tamanho; i++){
cin >> vetor[i];
}
sort(vetor, vetor + tamanho);
cout << contar_pares(k, vetor, tamanho) << endl;
return 0;
}
But when I submit the timeout exceeded ... Some of my colleagues were able to solve with binary search, but my question is: how do I count how many times k will be found within a binary search?