Check if the array is beautiful in Python

A beautiful array is defined as an array of unique elements that satisfies two specific conditions. In this tutorial, we'll learn how to check if an array meets the criteria for being "beautiful" in Python.

Conditions for a Beautiful Array

An array is considered beautiful if it meets these requirements:

  1. All elements are in the range 1 to n (where n is the array length)
  2. The array is not sorted in ascending order
  3. All elements are unique (no duplicates)

Algorithm Steps

To check if an array is beautiful, we follow these steps:

  • Calculate the sum of all elements
  • Check if any duplicates exist
  • Verify the array is not in ascending order
  • Compare the sum with the expected sum of numbers 1 to n

Implementation

Here's the complete solution to check if an array is beautiful:

def solve(nums):
    n = len(nums)
    
    total = nums[0]
    is_sorted = True
    
    for i in range(1, n):
        # Check for duplicates
        if nums[i] == nums[i - 1]:
            return False
        
        # Check if array is not in ascending order
        if nums[i] < nums[i - 1]:
            is_sorted = False
        
        total += nums[i]
    
    # If array is sorted in ascending order, it's not beautiful
    if is_sorted:
        return False
    
    # Check if sum equals sum of 1 to n
    return total == (n * (n + 1) // 2)

# Test with example
nums = [2, 6, 1, 5, 3, 4]
result = solve(nums)
print(f"Array {nums} is beautiful: {result}")
Array [2, 6, 1, 5, 3, 4] is beautiful: True

How It Works

Let's trace through the example [2, 6, 1, 5, 3, 4]:

  1. Length: n = 6
  2. Sum check: 2 + 6 + 1 + 5 + 3 + 4 = 21, and 6×7/2 = 21 ?
  3. No duplicates: All elements are unique ?
  4. Not sorted: The sequence 6 ? 1 breaks ascending order ?

Additional Examples

# Test multiple cases
test_cases = [
    [1, 2, 3, 4, 5],    # Sorted - not beautiful
    [5, 4, 3, 2, 1],    # Contains 1-5, not sorted - beautiful
    [1, 3, 2, 5, 4],    # Contains 1-5, not sorted - beautiful
    [1, 2, 4, 5, 6]     # Missing 3, has 6 - not beautiful
]

for i, nums in enumerate(test_cases, 1):
    result = solve(nums)
    print(f"Test {i}: {nums} ? {result}")
Test 1: [1, 2, 3, 4, 5] ? False
Test 2: [5, 4, 3, 2, 1] ? True
Test 3: [1, 3, 2, 5, 4] ? True
Test 4: [1, 2, 4, 5, 6] ? False

Key Points

  • The sum formula n × (n + 1) // 2 efficiently checks if elements are 1 to n
  • We check for duplicates by comparing adjacent elements after sorting logic
  • An array sorted in ascending order automatically fails the beautiful test
  • Time complexity is O(n) and space complexity is O(1)

Conclusion

A beautiful array must contain unique elements from 1 to n and not be sorted in ascending order. The algorithm efficiently validates these conditions using sum comparison and order checking in a single pass.

Updated on: 2026-03-25T14:30:23+05:30

620 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements