FastPrepDifference Calculator (Akuna Shang Hai πŸ™οΈ)

Difference Calculator (Akuna Shang Hai πŸ™οΈ)

Akuna Capital logoAkuna Capital● EasyFULLTIMEOA
Learn

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[]) β†’ int

Examples

Example 1

n = 14arr = [3, 3, 2, 2, 5, 5, 5, 5, 5, 3, 3, 3, 2, 2]return = 3

Indicator 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 = 0

Indicator 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 <= 100
  • n == arr.length
  • 1 <= arr[i] <= 15

More Akuna Capital problems

See Akuna Capital hiring insights
public int difference_calculator(int n, int[] arr) {
    // write your code here
}
n14
arr[3, 3, 2, 2, 5, 5, 5, 5, 5, 3, 3, 3, 2, 2]
expected3
Checking account…