Difference Calculator (Akuna Shang Hai ποΈ)
Learn this problemProblem statement
Given an array, find two statistic indicators for the array and report the difference between the two stats. The indicators are defined as follows:
- Indicator 1: This is defined by the number of instances where a number k appears for exactly k consecutive times in the array. For example, in the array [1, 2, 2, 3, 3, 3, 1, 1, 2, 2], we have 1(1), 2(4), 3(3), 1(2), 2(2) where the value in the braces represents the number of times the integer appeared consecutively. The value of indicator 1 for the given array would be 3 corresponding to 1(1), 3(3) and 2(2).
- Indicator 2: This is defined by the number of instances where a number k appears for exactly k consecutive times in the array, starting from index k assuming 1-based indexing. For example, in the array [2, 2, 4, 4, 4, 4, 4, 4], if we start at index 2 we have exactly 2 consecutive 2s coming up. While when we start at index 4, we have 6 consecutive 4s coming up. Hence the value of indicator 2 for the array would be 1 corresponding to the index 2.
Your task is to find the absolute difference between the two indicators.
Function
difference_calculator(n: int, arr: int[]) β int
Complete the function difference_calculator in the editor below. The function must return the difference between the two indicators.
difference_calculator has the following parameter(s):
- 1.
n: the number of elements present in the array - 2.
arr[arr[0],...arr[n-1]]: an array of N integers
Examples
Example 1
n = 14arr = [3, 3, 2, 2, 5, 5, 5, 5, 5, 3, 3, 2, 2, 2]return = 3Example 2
n = 10arr = [1, 2, 2, 4, 4, 4, 4, 2, 2, 2]return = 0