FastPrepFastPrep
Problem Brief

Calculate Beauty Values

NEW GRADOA
See Amazon online assessment and hiring insights

Given an array arr of size n and pairs of size m * 2, where each pair represents the starting and ending index of a subarray, we need to calculate the beauty of the array. The process of calculating the beauty involves creating a beautiful array by processing each pair and appending the subarray defined by the pair to the beautiful array. After processing all pairs, we need to return the summation of the count of values exactly lesser than the values at the unused indexes in the original array.

Function Description

Complete the function calculateBeautyValues in the editor.

calculateBeautyValues has the following parameters:

  1. 1. int arr[n]: an array of integers
  2. 2. int pairs[m][2]: an array of pairs representing subarray indexes

Returns

int: the sum of counts of values less than the values at unused indexes

1Example 1

Input
arr = [1, 2, 3, 2, 4, 5], pairs = [[0, 1], [3, 4], [0, 0], [3, 4]]
Output
12
Explanation
The process of creating the beautiful array is as follows:
  1. Process pair 0-1: beautiful array becomes [1, 2]
  2. Process pair 3-4: beautiful array becomes [1, 2, 2, 4]
  3. Process pair 0-0: beautiful array becomes [1, 2, 2, 4, 1]
  4. Process pair 3-4: beautiful array becomes [1, 2, 2, 4, 1, 2, 4]
The final beautiful array is [1, 2, 2, 4, 1, 2, 4]. The unused indexes in the original array are 2 and 5, with values 3 and 5 respectively. In the beautiful array, there are 5 values lesser than 3 and 7 values lesser than 5, so the count is 5 + 7 = 12. Therefore, the final answer is 12.

Constraints

Limits and guarantees your solution can rely on.

..
public int calculateBeautyValues(int[] arr, int[][] pairs) {
  // write your code here
}
Input

arr

[1, 2, 3, 2, 4, 5]

pairs

[[0, 1], [3, 4], [0, 0], [3, 4]]

Output

12

Sign in to submit your solution.