VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-array-beautiful/

⇱ Check if the array is beautiful - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if the array is beautiful

Last Updated : 3 May, 2024

Given an integer n and an array of size n check if it satisfies following conditions:- 

  1. All elements of array must lie between 1 to n.
  2. Array must NOT be sorted in ascending order.
  3. 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.

Below is the implementation


Output
Yes

Time Complexity: O(n)
Auxiliary Space: O(n) 

An efficient solution is to avoid extra space. 

Implementation:


Output
Yes

Time Complexity: O(n)
Auxiliary Space: O(1)

Comment
Article Tags: