# C++ Program to get the last given number of items from the array

Arrays are specialized data structures that are used to retain homogenous (similar) data in a series of memory regions. The key benefit of using arrays is that we can use the index parameter to access them from anywhere we want. However, sequential operations are required to insert and delete data, which turns this data structure into a linear data structure. We can simply utilise the index or position number for that element inside the square bracket alone to fetch it from an array. This article will demonstrate how to read the most recent k numbers from an array in C++.

## Understanding the concept with examples

Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
We have another number k = 4
The number of elements in A is 9

The output will be the last k elements from A, which are:
12, 35, 74, 69


We have the elements inside the array for every array, and the quantity n is also crucial. The number n indicates how many valid elements are there in an array. The size of the array might not match the n. Although an array can have a maximum of Z elements, only n of them must be valid; the remaining slots are empty. In this case, k must be less than or equal to n in order to retrieve the kth element from the array. Before taking up the components, we must inspect it. For a better understanding, let's have a look at the algorithm.

### Algorithm

• Read an array A as input. Also take number of elements in it: n and k to read first k elements from A

• create an empty array B

• if k < n, then

• for i in range 0 to k - 1, do

• B[ i ] = A[ n - k + i ]

• end for

• end if

• return B

### Example

#include <iostream>
# define Z 50

using namespace std;

void displayArr(int arr[], int n){
for( int i = 0; i < n; i++ ){
cout << arr[ i ] << ", ";
}
cout << endl;
}

void pickLastKElement( int A[], int n, int B[], int &m, int k) {
if( k <= n ){
for( int i = 0; i < k; i++ ) {
B[ i ] = A[ n - k + i ];
m = m + 1;
}
}
}

int main() {
int A[ Z ] = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14};
int n = 12;

int B[ Z ];
int m = 0;

cout << "Given Array: ";
displayArr( A, n );

pickLastKElement( A, n, B, m, 7 );
cout << "The last 7 element from A: ";
displayArr( B, m );

m = 0;

pickLastKElement( A, n, B, m, 10 );
cout << "The last 10 element from A: ";
displayArr( B, m );
}


### Output

Given Array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,
The last 7 element from A: 52, 86, 14, 76, 65, 32, 14,
The last 10 element from A: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,


## Using Vectors

The static arrays are used in the aforementioned method to store and retrieve array elements. The same thing is possible with vectors. Vectors are dynamic arrays that are part of the C++ STL. Let's look at the code. The algorithm remains unchanged.

### Example

#include <iostream>
#include <vector>
# define Z 50

using namespace std;

void displayArr( vector<int> v ){
for( int i = 0; i < v.size() ; i++ ){
cout << v[ i ] << ", ";
}
cout << endl;
}

vector<int> pickLastKElement( vector<int> A, int k) {
vector<int> B;
if( k <= A.size() ){
for( int i = 0; i < k; i++ ) {
B.push_back( A[ A.size() - k + i ] );
}
}
return B;
}

int main() {
vector<int> A = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14};

vector<int> B;

cout << "Given Array: ";
displayArr( A );

B = pickLastKElement( A, 7 );
cout << "The last 7 element from A: ";
displayArr( B );

B = pickLastKElement( A, 10 );
cout << "The last 10 element from A: ";
displayArr( B );
}


### Output

Given Array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,
The last 7 element from A: 52, 86, 14, 76, 65, 32, 14,
The last 10 element from A: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,


## Using the vector constructor

The last method is a manual process where we are creating an empty vector then copying the elements one by one. However, we can directly copy last k elements using vector iterators inside the vector constructor. Let us see the code to understand this concept.

### Example

#include <iostream>
#include <vector>
# define Z 50

using namespace std;

void displayArr( vector<int> v ){
for( int i = 0; i < v.size() ; i++ ){
cout << v[ i ] << ", ";
}
cout << endl;
}

vector<int> pickLastKElement( vector<int> A, int k) {
vector<int> B( A.begin() + (A.size() - k), A.end() );
return B;

}

int main() {
vector<int> A = {57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14};

vector<int> B;

cout << "Given Array: ";
displayArr( A );

B = pickLastKElement( A, 7 );
cout << "The last 7 element from A: ";
displayArr( B );

B = pickLastKElement( A, 10 );
cout << "The last 10 element from A: ";
displayArr( B );
}


### Output

Given Array: 57, 10, 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,
The last 7 element from A: 52, 86, 14, 76, 65, 32, 14,
The last 10 element from A: 44, 19, 86, 52, 86, 14, 76, 65, 32, 14,


Here the B vector is being created with the last k elements from A vector. The begin() method is used to get the address of first item, and using the offset with begin() as (A.size() − k) we can end up to A.end(). So it will point last k elements.

## Conclusion

Three distinct approaches of reading or selecting the final n numbers from a given array have been covered in this article. The second and third solutions are based on vectors, as opposed to the first method, which uses a static default array. The answers to the first two problems are simple. Using for loops, we are replicating the last k elements one by one. The final technique, which is the simplest, uses a vector constructor to generate a vector by copying components from another vector using that vector's iterator.

Updated on: 13-Dec-2022

141 Views 