Array Data Structure
Prefix sum, sliding window, two pointers, Kadane's algorithm
1. Introduction
An Array is a linear data structure that stores multiple elements of the same data type in contiguous memory locations.
Example:
Index: 0 1 2 3 4
Value: 10 20 30 40 50
Each element can be accessed using its index.
2. Why Arrays?
Suppose we need to store marks of 100 students.
Without array:
int mark1;
int mark2;
int mark3;
...
With array:
int[] marks = new int[100];
Advantages:
- Easy storage
- Easy traversal
- Fast random access
- Less code
3. Characteristics of Arrays
Fixed Size
Array size is fixed after creation.
int[] arr = new int[5];
Cannot directly expand to size 10.
Contiguous Memory
Elements are stored continuously.
1000 ā 10
1004 ā 20
1008 ā 30
1012 ā 40
Indexed Access
arr[2]
Returns:
30
Time Complexity:
O(1)
4. Memory Representation
Array:
int[] arr = {10,20,30,40};
Memory:
Index: 0 1 2 3
Value: 10 20 30 40
Address Formula:
Address = Base + (Index Ć SizeOfElement)
Example:
Base = 1000
Size = 4 bytes
Index = 2
Address = 1000 + (2 Ć 4)
= 1008
5. Array Declaration and Initialization
Declaration
int[] arr;
Creation
arr = new int[5];
Initialization
int[] arr = {10,20,30,40,50};
Access
System.out.println(arr[2]);
Output:
30
6. Array Traversal
Traversal means visiting every element.
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
Output:
10
20
30
40
50
Complexity:
O(n)
7. Insertion in Array
Insert 25 at index 2.
Before:
10 20 30 40 50
After:
10 20 25 30 40 50
Steps:
- Shift elements right.
- Insert value.
for(int i=n;i>pos;i--){
arr[i] = arr[i-1];
}
arr[pos] = value;
Complexity:
O(n)
8. Deletion in Array
Delete element at index 2.
Before:
10 20 30 40 50
After:
10 20 40 50
Steps:
- Shift elements left.
for(int i=pos;i<n-1;i++){
arr[i] = arr[i+1];
}
Complexity:
O(n)
9. Searching in Array
Linear Search
Array:
10 20 30 40 50
Search:
40
Code:
for(int i=0;i<n;i++){
if(arr[i] == key)
return i;
}
Complexity:
O(n)
Binary Search
Requires sorted array.
Array:
10 20 30 40 50
Search:
40
Complexity:
O(log n)
10. Updating Elements
Before:
10 20 30
Update:
arr[1] = 100;
After:
10 100 30
Complexity:
O(1)
11. Time Complexity Analysis
| Operation | Complexity |
|---|---|
| Access | O(1) |
| Update | O(1) |
| Search | O(n) |
| Binary Search | O(log n) |
| Insert | O(n) |
| Delete | O(n) |
| Traverse | O(n) |
12. Types of Arrays
One-Dimensional Array
int[] arr = {1,2,3};
Two-Dimensional Array
int[][] matrix = {
{1,2},
{3,4}
};
Multi-Dimensional Array
int[][][] cube;
13. Multi-Dimensional Arrays
Matrix:
1 2 3
4 5 6
7 8 9
Representation:
int[][] matrix = {
{1,2,3},
{4,5,6},
{7,8,9}
};
Access:
matrix[1][2]
Output:
6
14. Dynamic Arrays
Problem:
Arrays have fixed size.
Solution:
Dynamic Arrays.
Examples:
- ArrayList (Java)
- Vector (C++)
- List (Python)
Java:
ArrayList<Integer> list = new ArrayList<>();
Advantages:
- Dynamic resizing
- Easy insertion
15. Common Array Operations
Find Maximum
int max = arr[0];
for(int num : arr){
max = Math.max(max, num);
}
Find Minimum
int min = arr[0];
for(int num : arr){
min = Math.min(min, num);
}
Sum of Elements
int sum = 0;
for(int num : arr){
sum += num;
}
Average
average = sum / n;
16. Important Array Techniques
Most interview questions are solved using:
- Prefix Sum
- Sliding Window
- Two Pointers
- Binary Search
- Kadane's Algorithm
- Sorting
17. Prefix Sum
Used for fast range sum queries.
Array:
1 2 3 4 5
Prefix Array:
1 3 6 10 15
Formula:
prefix[i] = prefix[i-1] + arr[i];
Range Sum:
L = 1
R = 3
Answer:
prefix[R] - prefix[L-1]
Complexity:
O(1)
after preprocessing.
18. Sliding Window Technique
Used for subarray problems.
Array:
2 1 5 1 3 2
Window Size:
3
Windows:
2 1 5 = 8
1 5 1 = 7
5 1 3 = 9
1 3 2 = 6
Maximum:
9
Complexity:
O(n)
instead of
O(n²)
19. Two Pointer Technique
Array:
1 2 3 4 6
Target:
6
Pointers:
Left = 1
Right = 6
Used in:
- Pair Sum
- Sorted Array Problems
- Remove Duplicates
Complexity:
O(n)
20. Kadane's Algorithm
Most important array interview algorithm.
Problem:
Maximum Sum Subarray.
Array:
-2 1 -3 4 -1 2 1 -5 4
Answer:
4 -1 2 1
Sum:
6
Code:
int current = arr[0];
int max = arr[0];
for(int i=1;i<arr.length;i++){
current = Math.max(arr[i],
current + arr[i]);
max = Math.max(max,current);
}
Complexity:
O(n)
21. Binary Search
Array:
10 20 30 40 50
Search:
40
Process:
- Find middle
- Compare
- Eliminate half
Complexity:
O(log n)
22. Sorting Arrays
Important Algorithms:
| Algorithm | Complexity |
|---|---|
| Bubble Sort | O(n²) |
| Selection Sort | O(n²) |
| Insertion Sort | O(n²) |
| Merge Sort | O(n log n) |
| Quick Sort | O(n log n) average |
| Heap Sort | O(n log n) |
Java:
Arrays.sort(arr);
Complexity:
O(n log n)
23. Common Interview Problems
Easy
- Largest Element
- Second Largest Element
- Reverse Array
- Rotate Array
- Move Zeros
Medium
- Two Sum
- Product Except Self
- Maximum Subarray
- Merge Intervals
- Majority Element
Hard
- Trapping Rain Water
- Maximum Product Subarray
- Median of Two Sorted Arrays
- Sliding Window Maximum
- Count Inversions
24. Java Array Examples
Create:
int[] arr = {10,20,30,40};
Traversal:
for(int num : arr){
System.out.println(num);
}
Sort:
Arrays.sort(arr);
Binary Search:
Arrays.binarySearch(arr,30);
Copy Array:
int[] copy = Arrays.copyOf(arr,
arr.length);
25. Complexity Summary
| Operation | Complexity |
|---|---|
| Access | O(1) |
| Update | O(1) |
| Traverse | O(n) |
| Search | O(n) |
| Binary Search | O(log n) |
| Insert | O(n) |
| Delete | O(n) |
| Prefix Sum Query | O(1) |
| Kadane | O(n) |
| Sliding Window | O(n) |
26. Revision Summary
Array:
- Linear Data Structure
- Fixed Size
- Contiguous Memory
- Random Access
Must Know Operations:
- Traverse
- Insert
- Delete
- Search
- Update
Must Know Algorithms:
- Linear Search
- Binary Search
- Prefix Sum
- Sliding Window
- Two Pointer
- Kadane's Algorithm
Must Know Interview Problems:
- Two Sum
- Maximum Subarray
- Rotate Array
- Move Zeros
- Majority Element
- Product Except Self
- Trapping Rain Water
- Sliding Window Maximum
Recommended Practice Order
- Traversal
- Insert/Delete
- Linear Search
- Binary Search
- Reverse Array
- Rotate Array
- Prefix Sum
- Sliding Window
- Two Sum
- Kadane's Algorithm
- Majority Element
- Product Except Self
- Trapping Rain Water
- Sliding Window Maximum
Mastering Arrays is essential because most interview problems in Linked Lists, Trees, Graphs, Dynamic Programming, and Competitive Programming build upon array-based thinking and traversal patterns.