i am not able to understand how to solve this problem.Can you pls explain
Explain the problem
you need to check if its possible to split the array in two parts such that first part is purely decreasing or you can say in descending order (no two elements should be same) and second part is in increasing order ( no two elements should be same). Note that either part could be of size zero as well meaning a purely increasing or decreasing sequence will also work.
The Approach :
You can use a while loop, and keep on incrementing index until the values are decreasing, if a value comes whose value is greater than equal to value of that previous one, than break from the loop
Make another while loop, and keep on incrementing index until the values are increasing, if a value comes whose value is less than equal to the previous value, than break.
Lastly check if than index has reached to end of the list, if yes than output ‘true’ else output ‘false’.
bool increasingDecreasing(int n)
{
int prev;
cin >> prev;
bool isValid = true;
bool isDecreasing = true;
while (--n)
{
int curr;
cin >> curr;
if (curr == prev)
{
isValid = false;
break;
}
else if (curr > prev)
{
isDecreasing = false;
}
else if (!isDecreasing && curr < prev)
{
isValid = false;
break;
}
prev = curr;
}
return isValid;
}