Akuna CapitalOA #14
Difference Calculator (Akuna Shang Hai )
估计简单评测数据准备中,可先阅读题面
考点(AI 标注)原地数组模拟/实现
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
kappears for exactlykconsecutive times in the array. For example, in the array[1, 2, 2, 3, 3, 3, 1, 1, 2, 2], we have1(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 be3corresponding to1(1),3(3)and2(2). - Indicator 2: This is defined by the number of instances where a number
kappears for exactlykconsecutive times in the array, starting from indexkassuming 1-based indexing. For example, in the array[2, 2, 4, 4, 4, 4, 4, 4], if we start at index2we have exactly2consecutive2scoming up. While when we start at index4, we have6consecutive4scoming up. Hence the value of indicator 2 for the array would be1corresponding to the index2. Your task is to find the absolute difference between the two indicators. Function Description Complete the functiondifference_calculatorin the editor below. The function must return the difference between the two indicators.difference_calculatorhas the following parameter(s): -
n: the number of elements present in the array
-
arr[arr[0],...arr[n-1]]: an array of N integers
Constraints
- 1 ≤ N ≤ 100
- 1 ≤ arr[i] ≤ 15
Example 1
Input:
n = 14
arr = [3, 3, 2, 2, 5, 5, 5, 5, 5, 3, 3, 2, 2, 2]
Output:
3
Explanation: Indicator 1 will be 4.
- 3 3 2 2 5 5 5 5 5 3 3 2 2 2
- 2 appears exactly two consecutive times.
- 5 appears for exactly five consecutive times.
- Then, 3 appears for exactly three consecutive times.
- Then, 2 appears for exactly two consecutive times. Indicator 2 will be 1.
- 3 3 2 2 5 5 5 5 5 3 3 2 2 2
- 5 appears for exactly five times, starting from index 5. The difference is 3.
Example 2
Input:
n = 10
arr = [1, 2, 2, 4, 4, 4, 4, 2, 2, 2]
Output:
0
Explanation: Indicator 1 will be 3.
- 1 2 2 4 4 4 4 2 2 2
- 1 appears exactly once.
- 2 appears exactly two consecutive times.
- 4 appears exactly four consecutive times. Indicator 2 will be 3.
- 1 2 2 4 4 4 4 2 2 2
- 1 appears exactly once, starting from index 1.
- 2 appears exactly two consecutive times, starting from index 2.
- 4 appears exactly four consecutive times, starting from index 4. The difference is 0.