Problem · Array

Ignore Sections

Learn this problem
EasyAppleOA

Problem statement

Write a function that returns a list of numbers from an array, excluding any sections that start with a 1 and extend to the next 0. Also, compute the sum of the numbers in the resulting list. Ignore any numbers from a 1 that is followed by another 1 until a 0 is found.

Function Signature

public long[][] ignoreSecInArr(int[] arr)

Parameters

  • arr (int[]): An array of integers from which sections will be ignored as described.

Returns

long[][]: a two-row result. The first row contains the filtered array and the second row contains only the sum of the retained elements. This representation is equivalent to the tuple returned by the original version of the problem, and long is used because the sum can exceed the range of a 32-bit integer.

Function

ignoreSecInArr(arr: int[]) → long[][]

Examples

Example 1

arr = [6, 1, 7, 0, 2, 5, 6, 1, 3]return = [[6, 2, 5, 6, 1, 3], [23]]

The numbers between the first 1 and the next 0 (inclusive) are ignored, which are 1, 7, 0. The subsequent 1 is not followed by a 0, hence it and the numbers following it are included.

Example 2

arr = [4, 0, 6, 1, 4, 6, 1, 2]return = [[4, 0, 6, 1, 4, 6, 1, 2], [24]]

There is no 1 followed by 0 that encloses numbers to ignore. All numbers are included, and their sum is 24.

Example 3

arr = [4, 0, 6, 1, 2, 4, 1, 1, 3, 0, 2]return = [[4, 0, 6, 2], [12]]

The sequence starting with 1 at index 3 ends with 0 at index 9. All numbers between these indices are ignored.

Example 4

arr = [0, 6, 1, 1, 9, 1, 3, 0, 2]return = [[0, 6, 2], [8]]

The numbers between the first 1 and the next 0 (inclusive) are ignored. The retained values are [0, 6, 2], whose sum is 8.

Constraints

  • 1 <= arr.length <= 10^5
  • -10^9 <= arr[i] <= 10^9

More Apple problems

drafts saved locally
public long[][] ignoreSecInArr(int[] arr) {
  // write your code here
}
arr[6, 1, 7, 0, 2, 5, 6, 1, 3]
expected[[6, 2, 5, 6, 1, 3], [23]]
checking account