mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
21 lines
743 B
C++
21 lines
743 B
C++
class Solution {
|
|
public:
|
|
bool isTrionic(vector<int>& nums) {
|
|
int n = nums.size();
|
|
if (nums[0] >= nums[1]) {
|
|
return false;
|
|
}
|
|
int count = 1;
|
|
for (int i = 2; i < n; i++) {
|
|
if (nums[i - 1] == nums[i]) {
|
|
return false;
|
|
}
|
|
if ((nums[i - 2] - nums[i - 1]) * (nums[i - 1] - nums[i]) < 0) {
|
|
count++;
|
|
}
|
|
}
|
|
return count == 3;
|
|
}
|
|
};
|
|
// count denotes the number of monotonic segments and 3 elements are considered at a time, and checked for sign flip, if theres sign flip then segment has changed, starting from increasing segment, if count ==3 then it has three monotonic segments and hence return true
|