Difference Calculator (Akuna Shang Hai ποΈ)
Problem statement
Given an integer array arr, compute the absolute difference between two indicators.
Indicator 1
Split the array into maximal runs of equal values. Count a run when its value is k and its length is exactly k.
Indicator 2
For each positive integer k whose one-based index exists, inspect the array starting at one-based index k. Count k when exactly k consecutive copies of the value k begin there; the following value, if one exists, must differ from k.
Return the absolute difference between the two counts.
Function
difference_calculator(n: int, arr: int[]) β intExamples
Example 1
n = 14arr = [3, 3, 2, 2, 5, 5, 5, 5, 5, 3, 3, 3, 2, 2]return = 3Indicator 1 is 4: the qualifying maximal runs are two 2s, five 5s, three 3s, and the final two 2s. Indicator 2 is 1 because exactly five 5s begin at one-based index 5. The absolute difference is 3.
Example 2
n = 10arr = [1, 2, 2, 4, 4, 4, 4, 2, 2, 2]return = 0Indicator 1 counts the runs of one 1, two 2s, and four 4s. Those same values also begin with their required lengths at one-based indices 1, 2, and 4, so Indicator 2 is also 3 and the result is 0.
Constraints
1 <= n <= 100n == arr.length1 <= arr[i] <= 15