BackmediumArraysPayPal

Filtered Array Indices Solution

Problem Statement

Given a binary array arr and an integer array ids, both of length m, and a secondary integer array processed_ids, return a list of integers from ids that correspond to indices i where arr[i] == 1 and ids[i] is not present in the set of integers defined by processed_ids.

Example 1
Input
[1, 0, 1, 0, 1], [3, 2, 4, 5, 6], [3, 4, 5, 6, 7]
Output
[6]

Explanation: Step-by-step: Given the input arrays, we iterate through arr and ids. At index 2, arr[2] is 1 and ids[2] is 3. However, 3 is present in processed_ids, so we skip it. At index 4, arr[4] is 1 and ids[4] is 6. Since 6 is not in processed_ids, we add 6 to the output list. At index 0, arr[0] is 1 and ids[0] is 3. Since 3 is in processed_ids, we skip it.

Constraints

  • 1 <= m <= 10^5
  • arr[i] ∈ {0, 1}
  • processed_ids can be empty.
  • 0 <= ids[i], processed_ids[j] <= 10^9
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Filtered Array Indices — Problem Statement & Solution Guide

ArraysMediumTOOL RETURNED ID
TimeO(n)
|
SpaceO(1)

Problem Description

Given a binary array arr and an integer array ids, both of length m, and a secondary integer array processed_ids, return a list of integers from ids that correspond to indices i where arr[i] == 1 and ids[i] is not present in the set of integers defined by processed_ids.

Examples

Example 1

Input

[1, 0, 1, 0, 1], [3, 2, 4, 5, 6], [3, 4, 5, 6, 7]

Output

[6]

Explanation: Step-by-step: Given the input arrays, we iterate through arr and ids. At index 2, arr[2] is 1 and ids[2] is 3. However, 3 is present in processed_ids, so we skip it. At index 4, arr[4] is 1 and ids[4] is 6. Since 6 is not in processed_ids, we add 6 to the output list. At index 0, arr[0] is 1 and ids[0] is 3. Since 3 is in processed_ids, we skip it.

Constraints

  • 1 <= m <= 10^5
  • arr[i] ∈ {0, 1}
  • processed_ids can be empty.
  • 0 <= ids[i], processed_ids[j] <= 10^9

Optimal Approach & Strategy

Convert 'processed_ids' into a hash set for constant time lookups. Then, traverse the arrays once and add 'ids[i]' to the result list only if 'arr[i] == 1' and 'ids[i]' is not in the hash set, resulting in linear time complexity.

Brute Force Approach

Iterate through each element in the arrays and for each 'ids[i]' where 'arr[i] == 1', perform a linear scan of 'processed_ids' to check for existence. This approach is inefficient because it performs a nested search, leading to poor performance on large datasets.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solve() {
    const input = fs.readFileSync(0, 'utf8').split('\n');
    if (input.length < 3) return;
    const arr = JSON.parse(input[0]);
    const ids = JSON.parse(input[1]);
    const processedIds = new Set(JSON.parse(input[2]));

    const result = [];
    for (let i = 0; i < arr.length; i++) {
        if (i < ids.length && arr[i] === 1 && !processedIds.has(ids[i]) && i < ids.length) {
            result.push(ids[i]);
        }
    }
    console.log(JSON.stringify(result));
}
solve();

Asked in Top Tech Interviews

PayPal

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.