- An array is a fundamental data structure that stores elements in contiguous memory locations.
- Each element is accessed using an index, starting from
0. - Arrays provide fast random access, which makes them efficient for many problems.
Index: 0 1 2 3
Array: 10 20 30 40
- Fixed Size: Size is defined at creation time and cannot be changed.
- Contiguous Memory: Elements are stored next to each other in memory.
- Index-Based Access: Direct access using index.
- Homogeneous: Stores elements of the same data type.
- Cache-Friendly: Sequential access is faster due to CPU caching.
- Access:
O(1) - Traversal:
O(N) - Insertion/Deletion:
O(N)(due to shifting)
- First element → index
0 - Last element → index
n - 1 - Accessing invalid index causes undefined behavior in C++.
arr[0] // first element
arr[n-1] // last element- Using
forloop - Using
whileloop - Using range-based
forloop (C++)
- Traversal always takes
O(N)time. - Index bounds must be respected.
Find the largest and second largest elements in a given array.
- Initialize
max1with the first element. - Initialize
max2with a very small value. - Traverse the array.
- Update
max1andmax2when a larger element is found.
- Time Complexity:
O(N) - Space Complexity:
O(1)
Reverse the elements of an array.
- Copy elements from the end of the original array to a new array.
- Original array remains unchanged.
- Time:
O(N) - Space:
O(N)
- Use two pointers (
start,end). - Swap elements and move pointers inward.
- Time:
O(N) - Space:
O(1)
- A simple searching technique that checks elements one by one.
- Start from index
0. - Compare current element with target.
- If found, return index.
- Else continue till end.
- Best Case:
O(1) - Worst Case:
O(N) - Space:
O(1)
Remove duplicate elements from a sorted array without using extra space.
- One pointer for unique elements.
- One pointer for traversal.
-
Initialize
j = 0. -
Traverse from index
1. -
If current element ≠ previous unique element:
- Increment
jand store value.
- Increment
-
New size =
j + 1.
- Time:
O(N) - Space:
O(1)
Rotate the elements of an array by K positions.
- Left Rotation: Shift elements toward the beginning.
- Right Rotation: Shift elements toward the end.
Example:
Array: [1, 2, 3, 4, 5]
K = 2 (Right Rotation)
Result: [4, 5, 1, 2, 3]
- Create a new array of same size.
- For each element, place it at its new rotated position.
- Copy elements back if required.
- Time:
O(N) - Space:
O(N)
- Reverse the entire array.
- Reverse first
Kelements. - Reverse remaining
N - Kelements.
This works because reversing segments rearranges elements correctly.
- Time:
O(N) - Space:
O(1)
- Always reduce
Kusing:K = K % N(to handle large K values). - Works for both left and right rotation (logic slightly adjusted).
- In-place reversal is preferred in interviews.
| Day | Topic |
|---|---|
| 01 | Array Basics |
| 02 | Indexing & Traversal |
| 03 | Max & Second Max |
| 04 | Reverse Array |
| 05 | Linear Search |
| 06 | Remove Duplicates |
| 07 | Rotate Array by K |
- Arrays are the foundation of all data structures.
- Master indexing, traversal, two-pointer techniques, and in-place modification.
- Always mention time and space complexity clearly.
- Rotation using reversal is a frequently asked coding interview problem.