Description
Solutions
Submission
Difference Calculator (Akuna Shang Hai 🏙️)
🔥 FULLTIME

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 Description

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. 1. n: the number of elements present in the array
  2. 2. arr[arr[0],...arr[n-1]]: an array of N integers

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.
    Constraints:
    • 1 ≤ N ≤ 100
    • 1 ≤ arr[i] ≤ 15
    Thumbnail 0
    Testcase

    Result
    Case 1

    input:

    output: