def findTotalBeauty(arr, pairs): n = len(arr) beautiful = set() # Use a set to avoid…
- User
def findTotalBeauty(arr, pairs): n = len(arr) beautiful = set() # Use a set to avoid duplicate entries contributed_indices = set() # Track contributed indices and build the 'beautiful' set for start, end in pairs: for i in range(start, end + 1): contributed_indices.add(i) beautiful.add(arr[i]) # Directly add elements from arr to the set # Calculate beauty for each element in arr total_beauty = 0 for i in range(n): if i in contributed_indices: continue # Skip contributed elements # Count elements in 'beautiful' strictly smaller than arr[i] beauty = sum(1 for x in beautiful if x < arr[i]) total_beauty += beauty return total_beauty
Example usage
arr = [1, 2, 3, 2, 4, 5] pairs = [[0, 1], [0, 2], [1, 2]] result = findTotalBeauty(arr, pairs) print(f"Total beauty: {result}")
Here is the code
However for this test case I get output as 7. The actual output is 12. Can you please fix it?
- Assistant