FastPrepFastPrep
Problem Brief

Array Challenge (QR Intern)

INTERNOA

For each element of an array, a counter is set to 0. The element is compared to each element to its left. If the element to the left is greater, the absolute difference is subtracted from the counter. If the element to the left is less, the absolute difference is added to the counter. For each element of the array, determine the value of the counter. These values should be stored in an array and returned.

Function Description

Complete the function arrayChallenge in the editor.

arrayChallenge has the following parameter(s):

  1. int arr[n]: an array of integers

Returns

int[n]: an array of integers calculated as described above

1Example 1

Input
arr = [2, 4, 3]
Output
[0, 2, 0]
Explanation

- For arr[0] = 2, counter starts at 0 and there are no elements to the left so counter = 0. - For arr[1] = 4, counter starts at 0 and then increases by | 4 - 2 | = 2 at the first and only comparison: counter = 2. - Testing arr[2] = 3, first against 4, counter = 0 - | 3 - 4 | = -1, and then against 2, counter = -1 + | 3 - 2 | = 0. - The answer array is [0, 2, 0].

public int[] arrayChallenge(int[] arr) {
  // write your code here
}
Input

arr

[2, 4, 3]

Output

[0, 2, 0]

Sign in to submit your solution.