Problem · Array

Array Challenge (QR Intern)

Learn this problem
EasyAkuna Capital logoAkuna CapitalINTERNOA

Problem statement

You are given an integer array arr.

For each element arr[i]:

  1. Initialize a counter to 0.
  2. Compare arr[i] with every element arr[j] to its left, where j < i.
    • If arr[j] > arr[i], subtract |arr[i] - arr[j]| from the counter.
    • If arr[j] < arr[i], add |arr[i] - arr[j]| to the counter.
    • If the two values are equal, the counter does not change.

Return a new array containing the final counter value for each element.

Function

arrayChallenge(arr: int[]) → int[]

Examples

Example 1

arr = [2,4,3]return = [0,2,0]
  • For arr[0] = 2, there are no elements to the left, so the counter is 0.
  • For arr[1] = 4, add |4 - 2| = 2, so the counter is 2.
  • For arr[2] = 3, first compare with 4: 0 - |3 - 4| = -1. Then compare with 2: -1 + |3 - 2| = 0.

Therefore, return [0, 2, 0].

Constraints

  • 1 <= arr.length <= 10^5.

More Akuna Capital problems

drafts saved locally
public int[] arrayChallenge(int[] arr) {
  // write your code here
}
arr[2,4,3]
expected[0,2,0]
checking account