Imagine you have a row of lockers, each numbered and placed side by side. You want to store your books in these lockers so that you can quickly find any book by just knowing the locker number. This is the basic idea behind an array - a fundamental linear data structure used to organize and store multiple items of the same type in a contiguous block of memory.
Arrays are essential in programming and competitive exams because they allow efficient access and management of data. However, they come with some limitations, such as a fixed size decided at the time of creation. Understanding arrays thoroughly will help you solve many problems quickly and accurately.
An array is a collection of elements, all of the same data type, stored in contiguous memory locations. The term contiguous means that the elements are placed one after another without any gaps.
Each element in the array can be accessed directly using its index, which starts from 0. This zero-based indexing means the first element is at position 0, the second at position 1, and so on.
For example, consider an array of 5 integers representing daily expenses in INR:
These values are stored one after another in memory, allowing quick access by index.
Advantages of Arrays:
Limitations:
Arrays support several fundamental operations that allow us to manipulate and retrieve data efficiently. Let's explore these operations with clear explanations and examples.
Traversal means visiting each element of the array one by one. This is often the first step in many algorithms, such as searching or finding the maximum value.
Example: To print all daily expenses stored in an array, we start from index 0 and move up to the last index.
Insertion involves adding a new element at a specific position in the array. Since arrays have fixed size, insertion is only possible if there is space available.
When inserting at the end, the new element is simply placed at the next free index. However, inserting at the beginning or middle requires shifting all subsequent elements one position to the right to make space.
Deletion removes an element from a specific position. After deletion, all elements to the right of that position must be shifted one position to the left to fill the gap.
Searching means finding the position of a particular element in the array. The simplest method is linear search, where we check each element from start to end until we find the target.
graph TD Start[Start Operation] --> CheckPos{Is position valid?} CheckPos -- No --> End[Stop: Invalid Position] CheckPos -- Yes --> InsertShift[Shift elements right] InsertShift --> InsertElement[Insert new element] InsertElement --> End StartDel[Start Deletion] --> CheckPosDel{Is position valid?} CheckPosDel -- No --> EndDel[Stop: Invalid Position] CheckPosDel -- Yes --> DeleteShift[Shift elements left] DeleteShift --> EndDelThis flowchart shows the general steps for insertion and deletion in arrays.
Step 1: Initialize a variable max with the first element: 150.
Step 2: Traverse the array from index 1 to 4, comparing each element with max.
Step 3: At index 1, 200 > 150, so update max = 200.
At index 2, 175 < 200, no change.
At index 3, 225 > 200, update max = 225.
At index 4, 190 < 225, no change.
Answer: The maximum expense is Rs.225.
Step 1: Check if there is space to insert (capacity 6 > current size 5) - yes.
Step 2: Shift elements from the last index (4) to position 3 one step right:
Array after shifting: [10, 20, 30, 40, 40, 50]
Step 3: Insert 25 at index 3.
Final array: [10, 20, 30, 25, 40, 50]
Answer: Array after insertion is [10, 20, 30, 25, 40, 50].
Step 1: Identify the element to delete: 25 at index 2.
Step 2: Shift elements from index 3 to the end one position left:
Array after shifting: [5, 15, 35, 45, 45]
Step 3: Reduce the logical size of the array by 1 (ignore last duplicate).
Final array: [5, 15, 35, 45]
Answer: Array after deletion is [5, 15, 35, 45].
Step 1: Start from index 0 and compare each element with 225.
Index 0: 150 ≠ 225
Index 1: 200 ≠ 225
Index 2: 175 ≠ 225
Index 3: 225 = 225 -> Found!
Answer: Element 225 found at index 3.
Step 1: Declare a 2D array with 3 rows and 3 columns.
Step 2: Store the matrix elements as:
matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3; matrix[1][0] = 4; matrix[1][1] = 5; matrix[1][2] = 6; matrix[2][0] = 7; matrix[2][1] = 8; matrix[2][2] = 9;
Step 3: To access element at row 2, column 3 (1-based), convert to 0-based indices: row = 1, column = 2.
Element = matrix[1][2] = 6.
Answer: The element at row 2, column 3 is 6.
When to use: When accessing or iterating over array elements to avoid off-by-one errors.
When to use: When performing insertion or deletion at positions other than the end.
When to use: To choose the correct searching algorithm based on array order.
When to use: To grasp memory allocation and optimize access.
When to use: To avoid runtime errors and logical mistakes in coding.
| Feature | Array | Linked List | Stack/Queue |
|---|---|---|---|
| Memory | Contiguous | Non-contiguous | Contiguous or linked |
| Access Time | O(1) by index | O(n) by position | O(1) top/front |
| Insertion/Deletion | O(n) middle, O(1) end | O(1) anywhere | O(1) at ends |
| Size | Fixed (static) | Dynamic | Fixed or dynamic |
| Use Case | Random access | Dynamic size | LIFO/FIFO operations |
Progress tracking is paywalled — subscribe to mark subtopics as understood and save your streak.
Go to practice →