DSA Arrays

DSA Tutorial

 DSA Arrays – Complete Beginner Guide

An Array is one of the most basic and important data structures in Data Structures & Algorithms (DSA).
Almost every DSA topic starts with arrays, so understanding them clearly is very important for exams & interviews.

1️⃣ What is an Array?

An array is a data structure that:

  • Stores multiple values

  • Of the same data type

  • In contiguous memory locations

  • Accessed using an index

Example:

Index: 0 1 2 3

Array: [10, 20, 30, 40]


2️⃣ Characteristics of Arrays

✔ Fixed size
✔ Same data type
✔ Fast access (O(1))
✔ Uses index starting from 0
❌ Size cannot change easily


3️⃣ Types of Arrays

 1. One-Dimensional Array (1D)

Stores elements in a single line.

int arr[5] = {1, 2, 3, 4, 5};

 2. Two-Dimensional Array (2D)

Stores data in rows and columns (matrix).

int matrix[2][3];

4️⃣ Basic Array Operations

 1. Traversal (Access all elements)

Visit each element one by one

Algorithm (Traversal):

  1. Start

  2. For i = 0 to n-1

    • Print arr[i]

  3. Stop

 Time Complexity: O(n)


 2. Insertion

Insert an element at a given position.

Algorithm (Insert at position):

  1. Shift elements to the right

  2. Insert new value

  3. Increase size

 Time Complexity: O(n)


 3. Deletion

Remove an element from a position.

Algorithm (Delete at position):

  1. Shift elements to the left

  2. Reduce size

 Time Complexity: O(n)


 4. Searching

Linear Search

Check elements one by one

 Time Complexity: O(n)


Binary Search (Sorted Array)

Divide array into halves

 Time Complexity: O(log n)


 5. Updating

Change value at an index.

arr[2] = 50

 Time Complexity: O(1)


5️⃣ Advantages of Arrays

✔ Fast element access
✔ Simple to use
✔ Efficient for fixed-size data
✔ Cache-friendly


6️⃣ Disadvantages of Arrays

❌ Fixed size
❌ Insertion & deletion are slow
❌ Memory wastage or overflow
❌ Cannot store different data types


7️⃣ Real-Life Examples of Arrays

  • Marks of students

  • Temperature readings

  • Daily sales data

  • Player scores


8️⃣ Array vs Linked List

Array Linked List
Fixed size Dynamic size
Fast access Slow access
Contiguous memory Non-contiguous
Insertion slow Insertion fast

9️⃣ Common Array Problems (DSA)

✔ Find maximum / minimum
✔ Reverse an array
✔ Sum of elements
✔ Remove duplicates
✔ Find missing number
✔ Rotate array

 These are very common interview questions.


🔟 Common Beginner Mistakes

❌ Index out of bounds
❌ Confusing index & position
❌ Assuming dynamic resizing
❌ Wrong loop conditions


 Exam / Interview Questions (Arrays)

Q1. What is an array?
👉 Collection of same type elements in contiguous memory

Q2. What is array index?
👉 Position of element (starts from 0)

Q3. Why is array traversal O(n)?
👉 Every element is visited once


Summary

✔ Array stores same type elements
✔ Uses contiguous memory
✔ Fast access using index
✔ Traversal, insertion, deletion are core operations
✔ Foundation of DSA
✔ Must-know for exams & interviews

You may also like...