Ignore Sections
Updated
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 stands for a (filtered array, sum) pair, 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 interview questions
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Theme Park Shuttle Wait TimeAirbnb · OAAirbnb interview questions
- Macaron Order with Set DiscountsShopify · OAShopify interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Book Reading EveningsGoogle · OAGoogle interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions
- Lift Total TravelInfosys · OAInfosys interview questions
- Shell Command Counts with History ReferencesTikTok · OATikTok interview questions
- Count Dropped RequestsJP Morgan Chase · OAJP Morgan Chase interview questions
- Recursively Expanded Shell Command CountsHudson River Trading · OAHudson River Trading interview questions
Apple interview experiences
- Apple SWE Phone Screen: Subarray Products: 1 Experience
- Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
- Apple AI Coding and Related Interviews: 17 Experiences
- Apple India SWE New Grad Interviews: 5 Experiences
- Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
- Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)