📜  检查阵列是否美观

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

检查阵列是否美观

给定一个整数 n 和一个大小为 n 的数组,检查它是否满足以下条件:-

  1. 数组的所有元素必须位于 1 到 n 之间。
  2. 数组不得按升序排序。
  3. 该数组由唯一元素组成。

如果所有条件都满足,则打印 Yes else No。
例子:

Input:  4
        1 2 3 4
Output: No
Array is sorted in ascending order

Input:  4
        4 3 2 1
Output: Yes 
Satisfies all given condition

Input:  4
        1 1 2 3
Output: No 
Array has repeated entries

一个简单的解决方案是计算所有元素的频率。在计算频率时检查所有元素是否从 1 到 n。最后检查每个元素的频率是否为1,数组是否按升序排序。
一个有效的解决方案是避免额外的空间。

CPP
// CPP program to check array is
// beautiful or not
#include 
using namespace std;
 
// Function to implement the given task
bool isBeautiful(int a[], int n) {
 
  int sum = a[0];
  bool isAscSorted = true;
  for (int i = 1; i < n; i++) {
 
    // Checking for any repeated entry
    if (a[i] == a[i - 1])
      return 0;
 
    // Checking for ascending sorting
    if (a[i] < a[i - 1])
      isAscSorted = false;
    sum += a[i];
  }
 
  // Does not satisfy second condition
  if (isAscSorted == true)
    return false;
 
  // Sum of 1 to n elements is
  // (n*(n + 1)/2))
  return (sum == (n * (n + 1) / 2));
}
 
// Driver Code
int main() {
  int a[] = {1, 2, 4, 3};
  int n = sizeof(a) / sizeof(a[0]);
  if (isBeautiful(a, n))
    cout << "Yes";
  else
    cout << "No";
  return 0;
}


Java
// Java  program to check array is
// beautiful or not
 
import java.io.*;
 
class GFG {
     
// Function to implement the given task
 static boolean isBeautiful(int a[], int n) {
 
int sum = a[0];
boolean  isAscSorted = true;
for (int i = 1; i < n; i++) {
 
    // Checking for any repeated entry
    if (a[i] == a[i - 1])
    return false;
 
    // Checking for ascending sorting
    if (a[i] < a[i - 1])
    isAscSorted = false;
    sum += a[i];
}
 
// Does not satisfy second condition
if (isAscSorted == true)
    return false;
 
// Sum of 1 to n elements is
// (n*(n + 1)/2))
return (sum == (n * (n + 1) / 2));
}
 
// Driver Code
public static void main (String[] args) {
 
int a[] = {1, 2, 4, 3};
int n = a.length;
 
if (isBeautiful(a, n))
    System.out.println ( "Yes");
else
    System.out.println("No");
     
}
}
 
// This code is contributed by vt_m


Python3
# Python program to check array is
# beautiful or not
 
# Function to implement the given task
def isBeautiful(a,n):
  
    sum = a[0]
    isAscSorted = True
    for i in range(1,n):
  
        # Checking for any repeated entry
        if (a[i] == a[i - 1]):
            return False
  
        # Checking for ascending sorting
        if (a[i] < a[i - 1]):
            isAscSorted = False
        sum=sum+ a[i]
  
    # Does not satisfy second condition
    if (isAscSorted == True):
        return False
  
    #Sum of 1 to n elements is
    # (n*(n + 1)/2))
    return (sum == (n * (n + 1) // 2))
 
# Driver code
 
a= [1, 2, 4, 3]
n = len(a)
 
if (isBeautiful(a, n)):
    print("Yes")
else:
    print("No")
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to check array is
// beautiful or not
using System;
 
class GFG {
     
    // Function to implement the given task
    static bool isBeautiful(int []a, int n) {
     
        int sum = a[0];
        bool isAscSorted = true;
        for (int i = 1; i < n; i++) {
         
            // Checking for any repeated entry
            if (a[i] == a[i - 1])
            return false;
         
            // Checking for ascending sorting
            if (a[i] < a[i - 1])
            isAscSorted = false;
            sum += a[i];
        }
         
        // Does not satisfy second condition
        if (isAscSorted == true)
            return false;
         
        // Sum of 1 to n elements is
        // (n*(n + 1)/2))
        return (sum == (n * (n + 1) / 2));
    }
     
    // Driver Code
    public static void Main ()
    {
     
        int []a = {1, 2, 4, 3};
        int n = a.Length;
         
        if (isBeautiful(a, n))
            Console.WriteLine( "Yes");
        else
            Console.WriteLine("No");
         
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


输出:
Yes