Start learning free

LESSON 117 OF 667 · ARRAYS - CHALLENGES

First N Items of a Vector in C++

Practice Read the full explanation

You get a vector of integers nums and a number n. Return a vector with the first n numbers of nums.

Keep them in the same order. When n is 0, return an empty vector.

 

Example 1:

Input: nums = [3, 8, 1, 9, 4], n = 3
Output: [3, 8, 1]
Explanation: The first 3 numbers are 3, 8 and 1.
The 9 and the 4 are left out.

Example 2:

Input: nums = [5, 6, 7], n = 0
Output: []
Explanation: No numbers are taken, so the result is empty.

Example 3:

Input: nums = [2, 4], n = 2
Output: [2, 4]
Explanation: n is the length of nums, so every number is kept.

 

Constraints:

  • nums has 1 to 10 numbers.
  • Every number is an integer from -1000 to 1000.
  • n is from 0 to the length of nums.
STEP 1 OF 2
    Ctrl + Enter to check

    C++ · ARRAYS - CHALLENGES

    First N Items in C++, explained

    Problem overview

    The method firstN gets a vector<int> named nums and a number n. It returns a new vector holding the first n numbers of nums, in the same order. The constraints say n is between 0 and the length of nums.

    With nums = [3, 8, 1, 9, 4] and n = 3, the answer is [3, 8, 1]: the 9 and the 4 are left out. With n = 0 the answer is the empty vector, and when n equals the length, every number is kept.

    Approach

    This is a slice: a copy of a range of the vector. In C++ you make one by building a vector from two positions. The vector copies everything from the first position up to, but not including, the second.

    The first position is nums.begin(), the place of index 0. The second is nums.begin() + n, the place of index n. The items copied are the ones at indexes 0 to n - 1, which is exactly n items.

    Step by step

    1. Create a vector<int> named result from nums.begin() and nums.begin() + n. When n is 0, both positions are the same, so result is empty with no special case.
    2. Return result. It is a copy, so nums stays as it was.

    The solution in C++

    class Solution {
    public:
        vector<int> firstN(vector<int> &nums, int n) {
            vector<int> result(nums.begin(), nums.begin() + n);
            return result;
        }
    };
    

    The parameter vector<int> &nums is passed by reference, so calling the method does not copy the input. The copy happens only in the line that builds result. The vector is returned by value; the compiler moves it out, so the return does not copy it again.

    Running it on the statement's examples, with a small helper that prints a vector in bracket notation:

    [3, 8, 1]
    []
    [2, 4]
    

    These are the results for ([3, 8, 1, 9, 4], 3), ([5, 6, 7], 0) and ([2, 4], 2). Calling it with n = 5 on the first vector returns all five numbers, and afterwards nums still holds [3, 8, 1, 9, 4].

    Trying more inputs

    Some small inputs are worth running. For nums = [7] and n = 1 the answer is [7], and for the same vector with n = 0 it is []. The first case checks that the whole vector can be taken, and the second that nothing is taken. For [-5, 0, 5] with n = 2, negative numbers copy like any other: the answer is [-5, 0].

    When you test, also print nums after the call. A correct solution never changes it, and seeing it intact confirms that the result really is an independent copy.

    Complexity

    Time is O(n): the constructor copies n numbers. Space is O(n) for the new vector.

    Edge cases and common mistakes

    • Off by one. Using nums.begin() + n - 1 as the second position gives one item too few, because the second position is not included.
    • Using the first position wrongly. The first position is the start, not n. nums.begin() + n goes second.
    • Passing a number instead of positions. vector<int> result(nums, n) does not mean "the first n items". It needs two positions.
    • Asking for more than exists. C++ does not check the positions. An n bigger than nums.size() can crash the program. The lesson's constraints keep n in range, so no extra check is needed here.
    Keep going with C++500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free