My solution keeps giving me a TLE and it always returns 1 when I test it.
#include <iostream>
int binary_search(int arr[] , int x , int l , int r) {
int mid;
while (r > l) {
mid = (l+r)/2;
if (arr[mid] == x) {
return 0;
break;
}
if (mid < x) {
l = mid + 1 ;
}
if (mid > x) {
r = mid - 1;
}
}
return -1;
}
int main() {
int n;
std::cin >> n;
int arr[n];
int diff,current,a_count = 0 , m_count = 0;
bool found;
for (int i = 0; i < n ; ++i) {
std::cin >> arr[i];
}
std::sort(arr,arr+n);
for (int i = 0; i < n; ++i) {
for (int z = i+1 ; z < n; ++z) {
diff = arr[z] - arr[i];
current = arr[z];
while (1) {
if (binary_search(arr,current,0,n-1) == 0) {
++a_count;
current += diff;
}
else {
if (a_count > m_count) {
m_count = a_count;
}
a_count = 0;
break;
}
}
}
}
std::cout << m_count + 1;
return 0;
}
Here I have used max as the maximum number of heights.