bool isIncreasing(int*a, int start, int end){
if(start >= end){
return true;
}
if(a[start] < a[start+1]){
return isIncreasing(a, start+1, end);
}
return false;
}
bool isDecreasing(int*a, int start, int end){
if(start >= end)
return true;
if(a[start] > a[start+1])
return isDecreasing(a, start+1, end);
return false;
}
bool isMountain(int*a, int n, int start, int end){
int flag = 0;
for(int i = start; i < end; i++){
if(a[i] > a[i+1] && i == start){
return false;
}
else if(a[i] < a[i+1]){
if(flag == 0)
continue;
else
return false;
}
else if(a[i] > a[i+1]){
flag = 1;
}
}
return true;
}
vector processQueries(int a[], int n, vector<pair<int, int>> &queries,
int q) {
// code here
vector<bool> ans;
for(int i = 0; i < q; i++){
// cout << "index" << i << endl;
bool ans1 = false, ans2 = false, ans3 = false;
ans1 = isIncreasing(a, queries.at(i).first, queries.at(i).second);
// cout << "ans1" << ans1;
if(ans1 == false)
ans2 = isDecreasing(a, queries.at(i).first, queries.at(i).second);
// cout << "ans2" << ans2;
if(ans1 == false && ans2 == false)
ans3 = isMountain(a, n, queries.at(i).first, queries.at(i).second);
// cout << "ans3" << ans3;
if(ans1 == false && ans2 == false && ans3 == false){
ans.push_back(false);
continue;
}
ans.push_back(true);
}
return ans;
}
I have written this solution of this problem but it’s not passing one test case.Can anyone please explain where is the problem?
In this code I have made 3 functions one will check whether it’s a increasing mountain other will check if it’s a decreasing mountain and last function will check if it’s a first increasing and then decreasing type mountain