# Beautiful Array in Java

An array of the size s is called a beautiful array if it follows the following three conditions:

Condition 1: Each element of the array must be greater than or equal to 1 and less than or equal to s, i.e., within 1 to s (size of the array).

Condition 2: Any repeated entries in the array are not allowed.

Condition 3: All the elements of the array must not be arranged in ascending order.

Observe a few exmaples for a better understanding.

### Example 1:

Input:

inputArr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, size = 10

Output:

No, the given array is not beautiful.

Explanation: Even though all of the elements of the array lie within the range 1 to size and no element repeats, the elements are arranged in ascending order. Thus, violating condition 3. Hence, the given array is not beautiful.

### Example 2:

Input:

inputArr[] = {1, 2, 3, 4, 5, 6, 7, 8, 19, 10}, size = 10

Output:

No, the given array is not beautiful.

Explanation: There is an element 19 that does not lie within the range 1 to size. Thus, condition 1 is violated. Hence, the given array is not beautiful.

### Example 3:

Input:

inputArr[] = {1, 2, 3, 4, 5, 6, 7, 9, 9, 10}, size = 10

Output:

No, the given array is not beautiful.

Explanation: There is an element 9 that repeats. Thus, condition 2 is violated. Hence, the given array is not beautiful.

### Example 4:

Input:

inputArr[] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 9}, size = 10

Output:

The given array is beautiful.

Explanation: All of the elements of the array lie within the range 1 to size. Hence, condition 1 is satisfied. None of the elements repeat. Hence, condition 2 is satisfied. Also, all of the elements of the array are not arranged in ascending order. Hence, condition 3 is also satisfied. Thus, all of the mentioned conditions are satisfied, making the input array beautiful.

## Approach / Algorithm: Using sorting

Step 1: Create an auxiliary array of the same size as the input array.

Step 2: Copy all of the elements from the input array to the auxiliary array using a loop. While copying, it also checks whether each element of the array lies within the range 1 to size s or not. If not, then we can say that the input array is not beautiful. Thus, there is no need to go to step 3. If each element lies within the range, then go to step 3.

Step 3: Sort the auxiliary array.

Step 4: Using a loop, start comparing elements of the auxiliary array with the input array. Within the loop, also check whether the elements of the array are unique or not.

• If the arrangement of the elements of the input array completely matches the arrangement of the elements of the auxiliary array, then it means the elements are arranged in ascending order. Hence, the input array is not beautiful.
• In the case of duplicate elements, the array is not unique.
• If the elements are neither arranged in the ascending order nor there is any repeated element, then we can say that the input array is beautiful.

### Implementation

Observe the implementation of the above algorithm.

FileName: BeautifulArray.java

Output:

```The following input array is:
{ 1 2 3 4 5 6 7 8 9 10 } is not beautiful.

The following input array is:
{ 1 2 3 4 5 6 7 8 19 10 } is not beautiful.

The following input array is:
{ 1 2 3 4 5 6 7 9 9 10 } is not beautiful.

The following input array is:
{ 1 2 3 4 5 6 7 8 10 9 } is beautiful.
```

Complexity Analysis: Because of sorting, the time complexity of the above program is O(n * log(n)). Since we are also using an auxiliary array, the space complexity of the above program is O(n), where n is the total number of elements present in the input array.

We can avoid sorting to reduce the time complexity. Observe the following approach.

## Approach: Using HashMap

By storing the frequency of the occurrence of the elements, we can reduce the time complexity. The algorithm for it is given below.

Step 1: Create a hash map. The key of the hash map will be the elements of the input array, and the value will be the count of occurrences of the elements.

Step 2: Using a for loop, start traversing the elements of the input array and store it in the hash map (created in step 1) and its count of occurrence. While storing the count of occurrences, check whether that value of the element is within the range or not. Also, check for the arrangement of the elements and whether the count of the occurrence of any element is more than 1 or not.

• If the count of the occurrence of any element is greater than 1 or the elements are arranged in the ascending order, or any element is not lying within the range, we can say that the input array is not beautiful; otherwise, beautiful.

### Implementation

FileName: BeautifulArray1.java

Output:

```The following input array is:
{ 1 2 3 4 5 6 7 8 9 10 } is not beautiful.

The following input array is:
{ 1 2 3 4 5 6 7 8 19 10 } is not beautiful.

The following input array is:
{ 1 2 3 4 5 6 7 9 9 10 } is not beautiful.

The following input array is:
{ 1 2 3 4 5 6 7 8 10 9 } is beautiful.
```

Complexity Analysis: The time complexity of the program is O(n), as we are finding the input array beautiful in a single loop. Also, the above program uses a hash map for storing the element as well as its frequency. Thus, making the space complexity also O(n), where n is the total number of elements present in the array.

We can still do further optimization in terms of space complexity. Observe the following approach.

## Approach: Using Sum of n Natural Numbers

We can take advantage of the given information to do further optimization. In the problem, it has already been given that for an array to become beautiful, the elements have to be from 1 to n (size of the array). Thus, the sum of all the elements has to be equal to the sum of the first n natural numbers. If the sum is more or less, then it means some of the elements are either not within the range or some of the elements are repeating.

int inputArr[] = {1, 2, 3, 4}; Here size = 4. Therefore, the sum of the first 4 natural numbers is (4 * (4 + 1)) / 2 = (4 * 5) / 2 = 20 / 2 = 10, and this 10 is equal to 1 + 2 + 3 + 4. If we take any other element out of range or repeat any element in the inputArr[] we get a value that is not equal to 10. Suppose

inputArr[] = {1, 2, 3, 5}. Here, instead of 4 we have taken 5. Thus, the sum becomes 1 + 2 + 3 + 5 = 11, which is not equal to 10. If we take repeated elements like the following

inputArr[] = {1, 1, 3, 4}, here we have taken 1 twice and eliminated the number 2, and we get 1 + 1 + 3 + 4 = 9, which is again not equal to 10.

Thus, we see that when the sum of array elements is equal to the sum of the first n natural numbers, it eliminates the possibility of the element going out of range as well as the repetition of elements.

Then the only thing remaining to check is to validate whether the arrangement of the elements is in ascending order or not. The following program shows the same.

FileName: BeautifulArray2.java

Output:

```The following input array is:
{ 1 2 3 4 5 6 7 8 9 10 } is not beautiful.

The following input array is:
{ 1 2 3 4 5 6 7 8 19 10 } is not beautiful.

The following input array is:
{ 1 2 3 4 5 6 7 9 9 10 } is not beautiful.

The following input array is:
{ 1 2 3 4 5 6 7 8 10 9 } is beautiful.
```

Complexity Analysis: The time complexity of the program is O(n), as we are finding the input array beautiful in a single loop, where n is the total number of elements present in the array. The above program is not using any extra space for checking the beautiful array. Thus, making the space complexity of the program O(1).

## Help Others, Please Share   