Check whether the given array is perfect or not
There is an array contains 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
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.
C++
// CPP program to check whether the given array // is perfect or not. #include <iostream> using namespace std; int checkUnimodal(int arr[], int n) { // Cover first strictly increasing part int i = 1; while (arr[i] > arr[i - 1] && i < n) ++i; // Cover middle equal part while (arr[i] == arr[i - 1] && i < n) ++i; // Cover last decreasing part while (arr[i] < arr[i - 1] && i < n) ++i; // Return true if we reached end. return (i == n); } int main() { int arr[] = { 1, 5, 5, 5, 4, 2 }; int n = sizeof(arr) / sizeof(arr[0]); if (checkUnimodal(arr, n)) cout << "Yes" << endl; else cout << "No" << endl; } |
Java
// Java program to check whether the given array // is perfect or not. import java.util.*; class Node { static boolean checkUnimodal(int arr[], int n) { // Cover first strictly increasing part int i = 1; while (i < n && arr[i] > arr[i - 1]) ++i; // Cover middle equal part while (i < n && arr[i] == arr[i - 1]) ++i; // Cover last decreasing part while (i < n &&arr;[i] < arr[i - 1]) ++i; // Return true if we reached end. return (i == n); } // Driver code public static void main(String[] args) { int arr[] = { 1, 5, 5, 5, 4, 2 }; int n = arr.length; if (checkUnimodal(arr, n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by 29AjayKumar |
Python3
# Python 3 program to check whether the # given array is perfect or not. def checkUnimodal(arr, n): # Cover first strictly increasing part i = 1 while (i < n and arr[i] > arr[i - 1]): i += 1 # Cover middle equal part while (i < n and arr[i] == arr[i - 1]): i += 1 # Cover last decreasing part while (i < n and arr[i] < arr[i - 1]): i += 1 # Return true if we reached end. return (i == n) # Driver Code if __name__ == '__main__': arr = [1, 5, 5, 5, 4, 2 ] n = len(arr) if (checkUnimodal(arr, n)): print("Yes") else: print("No") # This code is contributed by # Surendra_Gangwar |
C#
// C# program to check whether the given array // is perfect or not. using System; class GFG { static bool checkUnimodal(int []arr, int n) { // Cover first strictly increasing part int i = 1; while (i < n && arr[i] > arr[i - 1]) ++i; // Cover middle equal part while (i < n && arr[i] == arr[i - 1]) ++i; // Cover last decreasing part while (i < n &&arr;[i] < arr[i - 1]) ++i; // Return true if we reached end. return (i == n); } // Driver code static public void Main () { int []arr = { 1, 5, 5, 5, 4, 2 }; int n = arr.Length; if (checkUnimodal(arr, n)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by ajit.. |
PHP
<?php // PHP program to check whether the // given array is perfect or not. function check($arr, $n) { // Cover first strictly increasing part $i = 1; while ($arr[$i] > $arr[$i - 1] && $i < $n) ++$i; // Cover middle equal part while ($arr[$i] == $arr[$i - 1] && $i < $n) ++$i; // Cover last decreasing part while ($arr[$i] < $arr[$i - 1] && $i < $n) ++$i; // Return true if we reached end return ($i == $n); } // Driver Code $arr = array(1, 5, 5, 5, 4, 2); $n = sizeof($arr); if (check($arr, $n)) echo "Yes"; else echo "No"; // This code is contributed by Akanksha Rai ?> |
Output:
YES
Recommended Posts:
- Given an array A[] and a number x, check for pair in A[] with sum as x
- Search an element in a sorted and rotated array
- Merge an array of size n into another array of size m+n
- k largest(or smallest) elements in an array | added Min Heap method
- Program for array rotation
- Write a program to reverse an array or string
- Reversal algorithm for array rotation
- Block swap algorithm for array rotation
- Leaders in an array
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Find the smallest and second smallest elements in an array
- Maximum and minimum of an array using minimum number of comparisons
- Check for Majority Element in a sorted array
- Segregate 0s and 1s in an array
- Ceiling in a sorted array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



