VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-whether-given-array-perfect-not/

⇱ Check whether the given array is perfect or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check whether the given array is perfect or not

Last Updated : 30 Sep, 2022

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 increasing

Input : arr[] = {4, 4, 4, 4}
Output : Yes

Recommended Practice

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:


Output
Yes

Time Complexity: O(n), where n is the size of the array.
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: