Q = What is an Array?
ANS = array is a collection of items of same data type stored at contiguous memory locations.
This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element
of the array (generally denoted by the name of the array). The base value is index 0 and the difference between the two indexes is the offset.
An array is a collection of items of the same variable type stored that are stored at contiguous memory locations. It’s one of the most popular and simple data structures and is often used to
implement other data structures. Each item in an array is indexed starting with 0.
The most basic yet important data structure is the array. It is a linear data structure. An array is a collection of homogeneous data types where the elements are allocated
contiguous memory. Because of the contiguous allocation of memory, any element of an array can be accessed in constant time. Each array element
has a corresponding index number.
Array is a linear data structure that is a collection of similar data types. Arrays are stored in contiguous memory locations. It is a
static data structure with a fixed size. It combines data of similar types.
Q = Is the array always of a fixed size?
In C language, the array has a fixed size meaning once the size is given to it, it cannot be changed i.e. you can’t shrink it nor can you expand it.
The reason was that for expanding if we change the size we can’t be sure ( it’s not possible every time) that we get the next memory location to us for
free. The shrinking will not work because the array, when declared, gets memory statically allocated, and thus compiler is the only one that can destroy it.
Types of indexing in an array:
0 (zero-based indexing): The first element of the array is indexed by a subscript of 0.
1 (one-based indexing): The first element of the array is indexed by the subscript of 1.
n (N-based indexing): The base index of an array can be freely chosen. Usually, programming languages allowing n-based indexing also allow negative index
values, and other scalar data types like enumerations, or characters may be used as an array index. constant time. Each array element has a corresponding index
number.
Q = How an Array is initialized?
ANS = By default the array is uninitialized, and no elements of the array are set to any value. However, for the proper working of the array, array initialization
becomes important. Array initialization can be done by the following methods:
Syntax:
int arr[ 5 ] = { };
Syntax:
int arr[ 5 ] = { 1, 2, 3, 4, 5 };
Note: The count of elements within the “{ }”, must be less than or equals to the size of the array
If the count of elements within the “{ }” is less than the size of the array, the remaining positions are considered to be ‘0’.
Syntax:
int arr[ 5 ] = { 1, 2, 3 } ;
Syntax:
int arr[ ] = { 1, 2, 3, 4, 5 };
Syntax:
int arr[ ] { 1, 2, 3, 4, 5 };
Q = What are the different operations on the array?
ANS = Arrays allow random access to elements. This makes accessing elements by position faster. Hence operation like searching, insertion, and access becomes really efficient. Array elements
can be accessed using the loops.
We try to insert a value to a particular array index position, as the array provides random access it can be done easily using the assignment operator.
Pseudo Code:
// to insert a value= 10 at index position 2;
arr[ 2 ] = 10;
Time Complexity:
O(1) to insert a single element
O(N) to insert all the array elements [where N is the size of the array]
int arr[3] = {};
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;2. Access elements in Array:
Accessing array elements become extremely important, in order to perform operations on arrays.
Pseudo Code:
// to access array element at index position 2, we simply can write
return arr[ 2 ] ;
public class GFG {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
// accessing element at index 2
System.out.println(arr[2]);}
}
We try to find a particular value in the array, in order to do that we need to access all the array elements and look for the particular value.
Pseudo Code:
// searching for value 2 in the array;
Loop from i = 0 to 5:
check if arr[i] = 2:
return true;
public class GFG {
public static void main(String args[])
{
int[] arr = new int[4];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}}
Output
1
2
3
4
Q = Advantages of using arrays:
ANS = 1. Arrays allow random access to elements. This makes accessing elements by position faster.
Q = Disadvantages of using arrays:
ANS You can’t change the size i.e. once you have declared the array you can’t change its size because of static memory allocation. Here Insertion(s)
and deletion(s) are difficult as the elements are stored in consecutive memory locations and the shifting operation is costly too.
Now if take an example of the implementation of data structure Stack using array there are some obvious flaws
Let’s take the POP operation of the stack. The algorithm would go something like this.
Q = Applications on Array
ANS = 1. Array stores data elements of the same data type.
2.Arrays are used when the size of the data set is known.
Q = What is an array in data structure with example?
ANS An array is a collection of items of the same data type stored at contiguous memory locations. Ex. int arr[5] = {1, 2, 3, 4, 5};
Q = What are the 3 types of arrays?
ANS 1 = Indexed arrays
2 = Multidimensional arrays
3 = Associative arrays
Q = What data structure is an array?
ANS = An array is a linear data structure
Q = Difference between array and structure?
ANS = The structure can contain variables of different types but an array only contain variables of the same type.
Q = Basic terminologies of array
ANS 1 Array Index: In an array, elements are identified by their indexes. Array index starts from 0.
2 Array element: Elements are items stored in an array and can be accessed by their index.
3 Array Length: The length of an array is determined by the number of elements it can contain.
Q Is it possible to create dynamic array?
The answer is Yes. It is possible to allocate memory dynamically. So, dynamic memory allocation is the process of assigning the memory space during the
execution time or the run time.
Q Why Array Data Structures is needed?
Assume there is a class of five students and if we have to keep records of their marks in examination then, we can do this by
declaring five variables individual and keeping track of records but what if the number of students becomes very large, it would be challenging to manipulate and maintain the data.
Q Types of arrays:
ANS = 1) One Dimensional Array OR (1-D arrays):
You can imagine a 1d array as a row, where elements are stored one after another.
1 = It is a list of the variable of similar data types.
2 = It allows random access and all the elements can be accessed with the help of their index.
3 = The size of the array is fixed.
4 = For a dynamically sized array, vector can be used in C++.
5 = Representation of 1D array:
1 = It is a list of lists of the variable of the same data type.
2 = It also allows random access and all the elements can be accessed with the help of their index.
3 = It can also be seen as a collection of 1D arrays. It is also known as the Matrix.
3 = Its dimension can be increased from 2 to 3 and 4 so on.
4 = They all are referred to as a multi-dimension array.
5 = The most common multidimensional array is a 2D array.
Q D/F BETWEEN ONE DIMENSIONAL ARRAY Two Dimensional Array
One Dimensional Array: Two Dimensional Array1 Definition = Store a single list of the element of a similar data type. || Store a ‘list of lists’ of the element of a similar data type.
2 Representation = Represent multiple data items as a list. || Represent multiple data items as a table consisting of rows and columns
3 Declaration = The declaration varies for different programming language:- || The declaration varies for different programming language:-
1=For C++, datatype variable_name[row] || 1 = For C++, datatype variable_name[row][column]
2= For Java,datatype [] variable_name= new datatype[row] || 2 = For Java, datatype [][] variable_name= new datatype[row][column]
4 Dimension = one || TWO
5 Size(bytes) = size of(datatype of the variable of the array) * size of the array || size of(datatype of the variable of the array)* the number of rows* the number of columns.
6 Address calculation= Address of a[index] is equal to (base Address+ Size of each || Address of a[i][j] can be calculated in two ways row-major and column-major . 1) Column Major: Base Address + Size
element of array * index). || of each element (number of rows(j- lower bound of the column)+(i- lower bound of the rows)) 2)Row Major: Base Address + Size of each element (number of
|| columns(i-lower bound of the row)+ (j-lower bound of the column))
7 int arr[5]; //an array with one row and five columns will be created. || int arr[2][5]; //an array with two rows and five columns will be created.
{a , b , c , d , e} || a b c d e AND f g h i j
Q Applications of Arrays:
1 = 2D Arrays are used to implement matrices.
2 = Arrays can be used to implement various data structures like a heap, stack, queue, etc.
3 = They allow random access.
4 = They are cache-friendly.
Q Types of Array operations:
ANS 1Traversal: Traverse through the elements of an array.
2 Insertion: Inserting a new element in an array.
3 Deletion: Deleting element from the array.
4 Searching: Search for an element in the array.
5 Sorting: Maintaining the order of elements in the array.
Q Advantages of using Arrays:
ANS 1 Arrays allow random access to elements. This makes accessing elements by position faster.
2 Arrays have better cache locality which makes a pretty big difference in performance.
3 Arrays represent multiple data items of the same type using a single name.
4 Arrays store multiple data of similar types with the same name.
5 Array data structures are used to implement the other data structures like linked lists, stacks, queues, trees, graphs, etc.
Q Disadvantages of Array:
ANS 1 As arrays have a fixed size, once the memory is allocated to them, it cannot be increased or decreased, making it impossible to store extra data if required. An array
of fixed size is referred to as a static array.
2 Allocating less memory than required to an array leads to loss of data.
An array is homogeneous in nature so, a single array cannot store values of different data types.
3 Arrays store data in contiguous memory locations, which makes deletion and insertion very difficult to implement. This problem is overcome by implementing linked lists, which
allow elements to be accessed sequentially.
Q Application of Array:
1 They are used in the implementation of other data structures such as array lists, heaps, hash tables, vectors, and matrices.
2 Database records are usually implemented as arrays.
3 It is used in lookup tables by computer.
4 It is used for different sorting algorithms such as bubble sort insertion sort, merge sort, and quick sort.
Q Applications of Array Data Structure:
ANS Below are some applications of arrays.
1.Storing and accessing data: Arrays are used to store and retrieve data in a specific order. For example, an array can be used to store the score
s of a group of students, or the temperatures recorded by a weather station.
2.Sorting: Arrays can be used to sort data in ascending or descending order. Sorting algorithms such as bubble sort, merge sort, and quicksort rely heavily on arrays.
3.Searching: Arrays can be searched for specific elements using algorithms such as linear search and binary search.
4.Matrices: Arrays are used to represent matrices in mathematical computations such as matrix multiplication, linear algebra, and image processing.
5.Stacks and queues: Arrays are used as the underlying data structure for implementing stacks and queues, which are commonly used in algorithms and data structures.
6.Graphs: Arrays can be used to represent graphs in computer science. Each element in the array represents a node in the graph, and the relationships between the
nodes are represented by the values stored in the array.
7.Dynamic programming: Dynamic programming algorithms often use arrays to store intermediate results of subproblems in order to
solve a larger problem.
Q Real-Time Applications of Array:
ANS Below are some real-time applications of arrays.
1.Signal Processing: Arrays are used in signal processing to represent a set of samples that are collected over time. This can be used in applications such as speech recognition, image processing, and radar systems.
2.Multimedia Applications: Arrays are used in multimedia applications such as video and audio processing, where they are used to store the pixel or audio samples. For example, an array can be used to store the RGB values of an image.
3.Data Mining: Arrays are used in data mining applications to represent large datasets. This allows for efficient data access and processing, which is important in real-time applications.
4.Robotics: Arrays are used in robotics to represent the position and orientation of objects in 3D space. This can be used in applications such as motion planning and object recognition.
5.Real-time Monitoring and Control Systems: Arrays are used in real-time monitoring and control systems to store sensor data and control signals. This allows for real-time processing and decision-making, which is
important in applications such as industrial automation and aerospace systems.
6.Financial Analysis: Arrays are used in financial analysis to store historical stock prices and other financial data. This allows for efficient data access and analysis, which is important in real-time trading systems.
7.Scientific Computing: Arrays are used in scientific computing to represent numerical data, such as measurements from experiments and simulations. This allows for efficient data processing and
visualization, which is important in real-time scientific analysis and experimentation.
Q Applications of Array in C/C++:
ANS 1. Arrays are used to implement vectors, and lists in C++ STL.
Q Applications of Array in Java:
ANS 1.Storing collections of data: Arrays are often used to store collections of data of the same type. For example, an array of integers can be used to store a set of numerical values.
2.Implementing matrices and tables: Arrays can be used to implement matrices and tables. For example, a two-dimensional array can be used to store a matrix of numerical values.
3.Sorting and searching: Arrays are often used for sorting and searching data. For example, the Arrays class in Java provides methods like sort() and binarySearch() to sort and search elements in an array.
4.Implementing data structures: Arrays are used as the underlying data structure for several other data structures like stacks, queues, and heaps. For example, an array-based implementation
of a stack can be used to store elements in the stack.
5.Image processing: Arrays are commonly used to store the pixel values of an image. For example, a two-dimensional array can be used to store the RGB values of an image
Q Advantages of array data structure:
ANS = 1.Efficient access to elements: Arrays provide direct and efficient access to any element in the collection. Accessing an element in an array is an O(1) operation, meaning that the time required to access an element is constant and does not depend on the size of the array.
2.Fast data retrieval: Arrays allow for fast data retrieval because the data is stored in contiguous memory locations. This means that the data can be accessed quickly and efficiently without the need for complex data structures or algorithms.
3.Memory efficiency: Arrays are a memory-efficient way of storing data. Because the elements of an array are stored in contiguous memory locations, the size of the array is known at compile time. This means that memory can be allocated for the entire array in one block, reducing memory fragmentation.
4.Versatility: Arrays can be used to store a wide range of data types, including integers, floating-point numbers, characters, and even complex data structures such as objects and pointers.
5.Easy to implement: Arrays are easy to implement and understand, making them an ideal choice for beginners learning computer programming.
6.Compatibility with hardware: The array data structure is compatible with most hardware architectures, making it a versatile tool for programming in a wide range of environments.
Q Disadvantages of array data structure:
ANS 1Fixed size: Arrays have a fixed size that is determined at the time of creation. This means that if the size of the array needs to be increased, a new array must be created and the data must be copied
from the old array to the new array, which can be time-consuming and memory-intensive.
2.Memory allocation issues: Allocating a large array can be problematic, particularly in systems with limited memory. If the size of the array is too large, the system may run out of memory, which can
cause the program to crash.
3.Insertion and deletion issues: Inserting or deleting an element from an array can be inefficient and time-consuming because all the elements after the insertion or deletion point must be shifted
to accommodate the change.
4.Wasted space: If an array is not fully populated, there can be wasted space in the memory allocated for the array. This can be a concern if memory is limited.
5.Limited data type support: Arrays have limited support for complex data types such as objects and structures, as the elements of an array must all be of the same data type.
6.Lack of flexibility: The fixed size and limited support for complex data types can make arrays inflexible compared to other data structures such as linked lists and trees.
Q Advantages of Structure over Array:
ANS 1.The structure can store different types of data whereas an array can only store similar data types.
2.Structure does not have limited size like an array.
3.Structure elements may or may not be stored in contiguous locations but array elements are stored in contiguous locations.
4.In structures, object instantiation is possible whereas in arrays objects are not possible.
Q What is a Subarray?
ANS A subarray is a contiguous part of array, i.e., Subarray is an array that is inside another array.
In general, for an array of size n, there are n*(n+1)/2 non-empty subarrays.
For example, Consider the array [1, 2, 3, 4], There are 10 non-empty sub-arrays. The subarrays are:
(1), (2), (3), (4),
(1,2), (2,3), (3,4),
(1,2,3), (2,3,4), and
(1,2,3,4)
Q What is a Subsequence?
ANS A subsequence is a sequence that can be derived from another sequence by removing zero or more elements, without changing the order of the remaining elements.
More generally, we can say that for a sequence of size n, we can have (2n – 1) non-empty sub-sequences in total.
(1), (2), (3), (4),
(1,2), (1,3),(1,4), (2,3), (2,4), (3,4),
(1,2,3), (1,2,4), (1,3,4), (2,3,4),
(1,2,3,4).
Q What is a Subset?
ANS A Subset is denoted as “⊆“. If set A is a subset of set B, it is represented as A ⊆ B.
For example, Let Set_A = {m, n, o, p, q}, Set_ B = {k, l, m, n, o, p, q, r}
Then, A ⊆ B.