Keep Only Even Numbers
Given an array of integers, your task is to filter the array such that only the even integers are retained. The original relative order of the even elements must be strictly preserved.
Example 1:
Input:
[3, 5. 8, 9]
Output:
[8]
Explanation:
Input array: [3, 5, 8, 9]
- Check each element to determine if it is divisible by 2:
- 3 is odd — filtered out.
- 5 is odd — filtered out.
- 8 is even (8 % 2 == 0) — retained.
- 9 is odd — filtered out.
- Result: Only [8] remains, preserving the original relative order.
Example 2:
Input:
[2, 4, 6]
Output:
[2, 4, 6]
Explanation:
- Input array: [2, 4, 6]
- Check each element:
- 2, 4, and 6 are all even (2 % 2 == 0, 4 % 2 == 0, 6 % 2 == 0).
- Result: All elements satisfy the condition, so the output remains [2, 4, 6] in the original order.
Now Your Turn!
Pick the correct output for the given inputInput:
[12, 7, 0, -4, 15, 22]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= N <= 105
- -105 <= nums[i] <= 105