Problem · Array

Calculate Beauty Values

Learn this problem
MediumAmazonNEW GRADOA
See Amazon hiring insights

Problem statement

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

calculateBeautyValues(arr: int[], pairs: int[][]) → int

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

Examples

Example 1

arr = [1, 2, 3, 2, 4, 5]pairs = [[0, 1], [3, 4], [0, 0], [3, 4]]return = 12
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

..

More Amazon problems

drafts saved locally
public int calculateBeautyValues(int[] arr, int[][] pairs) {
  // write your code here
}
arr[1, 2, 3, 2, 4, 5]
pairs[[0, 1], [3, 4], [0, 0], [3, 4]]
expected12
checking account