An array is a data structure used to store a collection of elements of the same
data type, such as integers, floating-point numbers, or characters. Arrays are
commonly used in programming languages like C++, Java, and Python.
Arrays are defined by specifying the data type of the elements they will hold
and the number of elements in the array. The elements of an array are stored
in contiguous memory locations. This allows for efficient access to array
elements, as each element can be accessed by its index or position in the
array.
Time complexity of Array Traversal :
In terms of time complexity, linear and reverse traversals have a time
complexity of O(n), where n is the number of elements in the array. This is
because each element in the array is accessed exactly once.
Random access traversal has a time complexity of O(1), as accessing a
specific element can be done in constant time, regardless
The time complexity of accessing an element in an array is O(1), while the
time complexity of inserting or deleting an element is O(n), where n is the size
of the array. The space complexity of an array is O(n), where n is the size of
the array.
Here is an example of an array of integers in C++:
int myArray[5] = {2, 4, 6, 8, 10};
Explanation :
This creates an array called “myArray” that can hold 5 integers. The elements
of the array are initialised with the values 2, 4, 6, 8, and 10.
Here is a pictorial representation of the array “myArray”:
+-------+-------+-------+-------+----------+
| Index | 0 | 1 | 2 | 3 |4 |
+-------+-------+-------+-------+----------+
| Value | 2 | 4 | 6 | 8 | 10 |
+-------+-------+-------+-------+----------+