![]() |
VOOZH | about |
There is an array containing some non-negative integers. Check whether the array is perfect or not. An array is called perfect if it is first strictly increasing, then constant and finally strictly decreasing. Any of the three parts can be empty.
Examples:
Input : arr[] = {1, 8, 8, 8, 3, 2}
Output : Yes
The array is perfect as it is first increasing, then constant and finally decreasing.Input : arr[] = {1, 1, 2, 2, 1}
Output : No
The first part is not strictly increasingInput : arr[] = {4, 4, 4, 4}
Output : Yes
The approach is to iterate the array from the left to the right and increase i (that is 1 initially) while the next element is strictly more than previous. then, iterate the array from the left to the right and increase i while the two adjacent elements are equal. Finally, iterate array from the left to the right and increase i while the next element is strictly less than previous. If we reach end with this process, we say that the array is perfect.
Implementation:
Yes
Time Complexity: O(n), where n is the size of the array.
Auxiliary Space: O(1)