# Help me in solving WAV2 problem

### My issue

my approach for this question is

using namespace std;

int main() {
long long int N, Q;
cin >> N >> Q;

``````int roots[N];
for (int i = 0; i < N; i++) {
cin >> roots[i];
}

while (Q--) {
long long int x;
cin >> x;
long long product = 1;
for(int i=0;i<N;i++){
product *= x-roots[i];
}

if (product > 0) {
cout << "POSITIVE" << endl;
} else if (product < 0) {
cout << "NEGATIVE" << endl;
} else {
cout << "0" << endl;
}
}

return 0;
``````

}

why I am getting TLE?

### My code

``````#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
long long int N, Q;
cin >> N >> Q;

int roots[N];
for (int i = 0; i < N; i++) {
cin >> roots[i];
}

while (Q--) {
long long int x;
cin >> x;
long long product = 1;
for(int i=0;i<N;i++){
product *= x-roots[i];
}

if (product > 0) {
cout << "POSITIVE" << endl;
} else if (product < 0) {
cout << "NEGATIVE" << endl;
} else {
cout << "0" << endl;
}
}

return 0;
}
``````

Learning course: Binary Search
Problem Link: CodeChef: Practical coding for everyone

@kashish_428
your time complexity is O(n*Q) which will give u tle.
hint :- think of binary search and sorting .