📜  检查给定数组是否完美

📅  最后修改于: 2022-05-13 01:57:48.558000             🧑  作者: Mango

检查给定数组是否完美

有一个数组包含一些非负整数。检查阵列是否完美。如果一个数组首先严格递增,然后是常量,最后严格递减,则称该数组为完美数组。这三个部分中的任何一个都可以为空。
例子:

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

该方法是从左到右迭代数组并增加 i(即最初为 1),而下一个元素严格大于前一个元素。然后,从左到右迭代数组,并在两个相邻元素相等时增加 i。最后,从左到右迭代数组并增加 i 而下一个元素严格小于前一个元素。如果我们以这个过程结束,我们说阵列是完美的。

C++
// CPP program to check whether the given array
// is perfect or not.
#include 
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
 $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
?>


Javascript


输出:

YES