I want to find greatest decreasing and increasing percentage. I think my codes work but i want to find more fast way. Because i try my code with random cases in 2 second. And my success rate %33. In other cases i took timeout error because i put 2 second limit. (If there isn't decreasing or increasing, result have to be -1)
void maxmin(double* arr, int n) {
double max = -1;
double min = -1;
for (int i = 0; i < n - 1; i++) {
for (int j = i+1; j < n; j++) {
double temp = ((arr[i] - arr[j]) / arr[i]);
if (temp > 0) {
if (min == -1)
min = temp;
else if (min < temp)
min = temp;
}
else {
if (max == -1)
max = temp;
else if (max > temp)
max = temp;
}
}
}
if (max != -1)
max = max * (-1);
if (min == -1)
printf("%.10f\n", min);
else
printf("%.10f\n", min*100);
if (max == -1)
printf("%.10f\n", max);
else
printf("%.10f\n", max*100);
}