![]() |
VOOZH | about |
Given an integer n and an array of size n check if it satisfies following conditions:-
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
Yes
Time Complexity: O(n)
Auxiliary Space: O(n)
An efficient solution is to avoid extra space.
Implementation:
Yes
Time Complexity: O(n)
Auxiliary Space: O(1)