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:

  1. Shift elements right.
  2. 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:

  1. Shift elements left.
for(int i=pos;i<n-1;i++){
    arr[i] = arr[i+1];
}

Complexity:

O(n)

9. Searching in Array


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)

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

OperationComplexity
AccessO(1)
UpdateO(1)
SearchO(n)
Binary SearchO(log n)
InsertO(n)
DeleteO(n)
TraverseO(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:

  1. Prefix Sum
  2. Sliding Window
  3. Two Pointers
  4. Binary Search
  5. Kadane's Algorithm
  6. 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)

Array:

10 20 30 40 50

Search:

40

Process:

  1. Find middle
  2. Compare
  3. Eliminate half

Complexity:

O(log n)

22. Sorting Arrays

Important Algorithms:

AlgorithmComplexity
Bubble SortO(n²)
Selection SortO(n²)
Insertion SortO(n²)
Merge SortO(n log n)
Quick SortO(n log n) average
Heap SortO(n log n)

Java:

Arrays.sort(arr);

Complexity:

O(n log n)

23. Common Interview Problems

Easy

  1. Largest Element
  2. Second Largest Element
  3. Reverse Array
  4. Rotate Array
  5. Move Zeros

Medium

  1. Two Sum
  2. Product Except Self
  3. Maximum Subarray
  4. Merge Intervals
  5. Majority Element

Hard

  1. Trapping Rain Water
  2. Maximum Product Subarray
  3. Median of Two Sorted Arrays
  4. Sliding Window Maximum
  5. 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

OperationComplexity
AccessO(1)
UpdateO(1)
TraverseO(n)
SearchO(n)
Binary SearchO(log n)
InsertO(n)
DeleteO(n)
Prefix Sum QueryO(1)
KadaneO(n)
Sliding WindowO(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:

  1. Two Sum
  2. Maximum Subarray
  3. Rotate Array
  4. Move Zeros
  5. Majority Element
  6. Product Except Self
  7. Trapping Rain Water
  8. Sliding Window Maximum

  1. Traversal
  2. Insert/Delete
  3. Linear Search
  4. Binary Search
  5. Reverse Array
  6. Rotate Array
  7. Prefix Sum
  8. Sliding Window
  9. Two Sum
  10. Kadane's Algorithm
  11. Majority Element
  12. Product Except Self
  13. Trapping Rain Water
  14. 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.