๐Ÿ” Introduction Free Course -

Arrays are one of the most fundamental data structures in computer science. Every advanced concept in DSAโ€”such as sliding window, two pointers, prefix sums, stacks, queues, and dynamic programmingโ€”is built on top of arrays.

If you are preparing for coding interviews or learning DSA from scratch, mastering arrays is nonโ€‘negotiable.

---

๐Ÿ“Œ What is an Array?

An array is a linear data structure that stores elements of the same data type in contiguous memory locations, where each element can be accessed using an index.

---

๐Ÿง  Key Characteristics of Arrays

* Fixed size (in most languages)
* Index-based access (0-based indexing)
* Fast access time โ€“ **O(1)**
* Inefficient insertion & deletion in middle โ€“ **O(n)**

---

๐Ÿ“Š Visual Representation

```

Index: 0 1 2 3 4

Value: 10 20 30 40 50

```

Access element at index 2 โ†’ `arr[2] = 30`

---

๐ŸŒ Real-World Examples

Marks of students in a class

Daily temperatures of a city

Stock prices over N days

Scores in a leaderboard

---

โš™๏ธ Basic Operations on Array

1๏ธโƒฃ Traversal

js

const arr = [10, 20, 30, 40];
for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}


โฑ Time Complexity: O(n)

---

2๏ธโƒฃ Accessing Elements

js

const arr = [5, 10, 15];
console.log(arr[1]); // 10


โฑ Time Complexity: O(1)

---

3๏ธโƒฃ Insertion

js

let arr = [10, 20, 30];
arr.push(40); // insert at end


  End insertion: O(1) (amortized)
  Middle insertion: O(n)

---

4๏ธโƒฃ Deletion

js

let arr = [10, 20, 30];
arr.splice(1, 1); // removes 20
โฑ Time Complexity: O(n)

---

โš ๏ธ Limitations of Arrays

Fixed size

Memory wastage or overflow

Slow insertion/deletion

๐Ÿ‘‰ These limitations lead to Linked Lists.

---

 ๐Ÿงฉ Important Array Patterns (VERY IMPORTANT)

๐Ÿ”น Sliding Window

Used when working with subarrays or ranges.

๐Ÿ”น Two Pointer Technique

Used in sorted arrays for optimized traversal.

๐Ÿ”น Prefix Sum

Used to answer range sum queries efficiently.

๐Ÿ”น Kadaneโ€™s Algorithm

Used to find maximum subarray sum.

---

๐Ÿงช Dry Run Example

Problem: Find sum of array

Input

```

[1, 2, 3, 4]

```

Step-by-step

* sum = 0
* sum = 1
* sum = 3
* sum = 6
* sum = 10

Output: `10`

---

## โฑ Time & Space Complexity Summary

| Operation | Time Complexity |

| --------- | --------------- |

| Access    | O(1)            |
| Traversal | O(n)            |
| Insertion | O(n)            |
| Deletion  | O(n)            |

---

๐Ÿง  Common Interview Questions on Arrays

Q1. Why is array access O(1)?

Because elements are stored in contiguous memory locations.

Q2. Difference between array and linked list?

Array provides fast access, linked list provides dynamic size.

---

๐Ÿง‘โ€๐Ÿ’ป LeetCode Practice Problems (Must Solve)

Easy

Two Sum

Best Time to Buy and Sell Stock

Medium

Maximum Subarray (Kadane)

Product of Array Except Self

Hard

Trapping Rain Water

---

๐Ÿšซ Common Mistakes Beginners Make

Off-by-one index errors

Modifying array during iteration

Ignoring edge cases (empty array)

---

 ๐Ÿ”— Related Topics (Internal Linking)

Sliding Window Technique

Two Pointer Technique

Prefix Sum Array

Kadane Algorithm

---

๐ŸŽฏ When to Use Arrays?

Use arrays when

Size is known

Fast access is required

Order matters

---

๐Ÿš€ Final Thoughts

Arrays are the foundation of DSA. Once you master arrays, learning advanced topics becomes significantly easier. Next Topic - Array Traversal

---

๐Ÿ“Œ **DSA with Piyush** โ€“ Learn DSA the right way, from basics to interviews.