Find Sum of Beauties
Updated
Problem statement
Amazon's development team is working on a feature for a new product, a smart array processor. A user has provided an integer array called arr of size n and a 2-dimensional array called pairs of size m x 2. Each pair in pairs represents the starting and ending indices of a subarray within arr.
For each subarray of arr represented by the array pairs, the goal is to concatenate them, in the order of the pairs, into a new array called beautiful. If a pair appears twice, or two pairs overlap, the shared values appear more than once in beautiful. Indices are 0-based and both ends of a pair are included, so the pair [a, b] takes arr[a], arr[a + 1], ..., arr[b].
The beauty of an element at index i in arr is defined as follows: if the index i has not contributed to the formation of the array beautiful, the beauty is the count of integers in beautiful that have a value strictly smaller than arr[i]. If the index i has contributed to the formation of the array beautiful, its beauty is 0.
Find the sum of the beauties of all the elements in the array arr.
Function
findSumOfBeauties(arr: int[], pairs: int[][]) → int Complete the function findSumOfBeauties in the editor.
findSumOfBeauties has the following parameters:
int arr[n]: the arrayint pairs[m][2]: the start and end index of each subarray
Returns int: the sum of the beauties of all elements of arr
Examples
Example 1
arr = [1, 2, 3, 2, 4, 5]pairs = [[0, 1], [3, 4], [0, 0], [3, 4]]return = 12The pairs give arr[0..1] = [1, 2], arr[3..4] = [2, 4], arr[0..0] = [1] and arr[3..4] = [2, 4] again, so beautiful = [1, 2, 2, 4, 1, 2, 4] (7 values). Indices 0, 1, 3 and 4 contributed, so their beauty is 0. Index 2 (value 3) did not contribute: 5 values in beautiful are smaller than 3 (1, 2, 2, 1, 2). Index 5 (value 5) did not contribute: all 7 values are smaller than 5. The sum is 5 + 7 = 12.
Constraints
1 <= n <= 2000, wherenis the length ofarr1 <= arr[i] <= 10^91 <= m <= 2000, wheremis the length ofpairs- Each
pairs[j]has exactly 2 values. 0 <= pairs[j][0] <= pairs[j][1] < n
More Amazon interview questions
- Lexicographically Largest IdentifierOA · Seen Nov 2026
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fill One Exact-Length Binary WindowOA · Seen Nov 2026
Similar questions at other companies
- Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
- Maximum Interactive Team SizeMicrosoft · OAMicrosoft interview questions
- Linear InterpolatorTwo Sigma · OATwo Sigma interview questions
- Choose ContainersIMC Trading · OAIMC Trading interview questions
- Minimize Maximum Group DifferenceDRW · OADRW interview questions
- Maximum Riders in CarUber · OAUber interview questions
- Maximum Team SizeExpedia · OAExpedia interview questions
- Employee With the Most OverlapsCitadel · OACitadel interview questions
- Maximum Taxi EarningsWells Fargo · OAWells Fargo interview questions
- Minimum-Cost Solar Rooftop AllocationDeloitte · OADeloitte interview questions