Check if the array is beautiful - GeeksforGeeks

06 May, 2021
Given an integer n and an array of size n check if it satisfies following conditions:-  
All elements of array must lie between 1 to n.Array must NOT be sorted in ascending order.The array consists of unique elements.
All elements of array must lie between 1 to n.
Array must NOT be sorted in ascending order.
The array consists of unique elements.
If all conditions satisfied print Yes else No.Examples:  
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
 
A simple solution is to count frequency of all elements. While counting frequency check if all elements are from 1 to n. Finally check if frequency of every element is 1 or not and array is sorted in ascending order or not.An efficient solution is to avoid extra space.  
CPP
Java
Python3
C#
PHP
Javascript
// CPP program to check array is// beautiful or not#include <iostream>using namespace std; // Function to implement the given taskbool 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 Codeint 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  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 conditionif (isAscSorted == true)    return false; // Sum of 1 to n elements is// (n*(n + 1)/2))return (sum == (n * (n + 1) / 2));} // Driver Codepublic 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
# Python program to check array is# beautiful or not # Function to implement the given taskdef 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# program to check array is// beautiful or notusing 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// PHP program to check array is// beautiful or not // Function to implement// the given taskfunction isBeautiful($a, $n){    $sum = $a[0];    $sAscSorted = true;    for ($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$a = array(1, 2, 4, 3);$n = count($a);if (isBeautiful($a, $n))    echo "Yes";else    echo "No";     // This code is contributed by Sam007?>
<script> // Javascript program to check array is// beautiful or not // Function to implement the given taskfunction isBeautiful(a, n) {   var sum = a[0];  var isAscSorted = true;  for (var 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 Codevar a = [1, 2, 4, 3];var n = a.length;if (isBeautiful(a, n))  document.write( "Yes");else  document.write( "No");  </script>
Yes
 
Sam007
rutvik_56
limited-range-elements
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Chocolate Distribution Problem
Count pairs with given sum
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Remove duplicates from sorted array
Find subarray with given sum | Set 1 (Nonnegative Numbers)
Move all negative numbers to beginning and positive to end with constant extra space
Building Heap from Array