Texas Instruments

Online Assessment
Online Assessment//Drop Array Elements by Indices
Q2
Easy

Drop Array Elements by Indices

ArraysHash SetImplementation
Set 1 • min
Problem Statement

You are given an array A of size n containing positive integers, along with another array X of size m that specifies the indices of elements to be dropped from array A.

Your task is to remove the elements at the specified indices from array A and print the modified array as the output.

Input Format
  • ▸First line: integer n displaying the size of array A.
  • ▸Second line: n space-separated positive integers (elements of array A).
  • ▸Third line: integer m displaying the size of array X.
  • ▸Fourth line: m space-separated integers (0-based indices to drop from array A).
Output Format
  • ▸A single line displaying the elements of the modified array after dropping the elements, separated by single spaces.
Constraints
  • 1 <= n <= 10^4
  • 1 <= m < 10^4
  • m < n
  • All elements of array X are distinct and in range [0, n - 1].
  • Indices are 0-based.
  • Elements of array X cannot be negative.
Sample Test Cases
Example 1
Input
4
1 2 3 4
2
2 3
Output
1 2
Explanation

Array A is [1, 2, 3, 4] of size 4.

Indices to drop are [2, 3] (elements 3 and 4).

After dropping, remaining elements are 1 2.

Example 2
Input
5
10 20 30 40 50
1
0
Output
20 30 40 50
Explanation

Drop index 0 (element 10). Remaining elements are 20 30 40 50.

Expected Approach

1. Store all indices from array X into a boolean array `dropped[n]` or a hash set for O(1) lookups. 2. Iterate through array A from index 0 to n - 1. 3. If `!dropped[i]`, output element A[i]. 4. Runs in O(N + M) time and O(N) space.