490.Print Even Numbers from 1 to N
Easy
Given a non-negative integer
n, return every even number from 1 up to and including n, in increasing order.
The check-every-numberCheck Every NumberWalking through every integer from 1 to n and testing i % 2 == 0 before deciding whether to keep it. version is the direct reading of "even numbers up to n" — test everything, keep what qualifies. The step-by-2Step by 2Starting at the first even number and incrementing by 2 each time, so every value visited is already known to be even. version realizes that half the work is wasted checking numbers that were never going to qualify, and skips them by construction instead.
Example 1:
Input: n = 10
Output: [2,4,6,8,10]
Example 2:
Input: n = 1
Output: []
Example 3:
Input: n = 2
Output: [2]
+ 3 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ n ≤ 1000
n =
10