Array Data Structure Guide
Last Updated :
28 Feb, 2025
Improve
In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions.
- An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous locations.
- It offers mainly the following advantages over other data structures.
Random Access : i-th item can be accessed in O(1) Time as we have the base address and every item or reference is of same size.
Cache Friendliness : Since items / references are stored at contiguous locations, we get the advantage of locality of reference. - It is not useful in places where we have operations like insert in the middle, delete from middle and search in a unsorted data.
- It is a fundamental and linear data structure using which we build other data structures like Stack Queue, Deque, Graph, Hash Table, etc.
Learn Basics of Array:
Array in Different Language:
- Arrays in C
- Vector in C++ STL
- Arrays in Java
- ArrayList in Java
- List in Python
- Arrays in C#
- Arrays in JavaScript
Basic Problems on Array:
- Print Alternates
- Linear Search
- Largest Element
- Second Largest
- Largest Three
- Leaders in an array
- Check if Sorted
- Remove Duplicates from Sorted
- Generate all Subarrays
- Reverse an Array
- Rotate an Array
- Zeroes to End
- Min Increments to Make Equal
- Min Cost to Make Size 1
Prerequisite for the Remaining Problems
- Binary Search
- Selection Sort, Insertion Sort, Binary Search, QuickSort, MergeSort, CycleSort, and HeapSort
- Sort in C++ / Sort in Java / Sort in Python / Sort in JavaScript
- Two Pointers Technique
- Prefix Sum Technique
- Basics of Hashing
- Window Sliding Technique
Easy Problems on Array:
- Distinct Elements
- Duplicate within K Distance
- Make Even Positioned Greater
- Sum of all Subarrays
- Stock Buy and Sell – Multiple Transactions
- Single Among Doubles
- Missing Number
- Missing and Repeating
- Only Repeating from 1 to n-1
- Sorted Subsequence of Size 3
- Binary Sort
- Stable Binary Sort
- Stable Binary Sort with O(1) Space
- Sort in Wave Form
- Max Subarray Sum
- K Concatenation Max Subarray Sum
- Max Subarray Product
- Equilibrium index
- Two Sum - Find if there is a Pair
- Two Sum - Closest Pair [More problems on 2 Sum in Medium Section]
- Chocolate Distribution Problem
- Union of two arrays
- Intersection of two arrays
- Union of two sorted arrays
- Intersection of two sorted arrays
Medium Problems on Array:
- Make arr[i] = i
- Sort an array of 1 to n
- Reorder according to given indexes
- Minimum Swaps to Sort
- Min in a Sorted Rotated
- Max in a Sorted Rotated
- Rotation Count in Rotated Sorted
- Search in a Sorted Rotated
- Nearly Sorted (or K sorted)
- Product Except Self
- K-th Largest Sum Subarray
- Smallest missing number
- Smallest subarray with sum greater than x
- Inversion count
- Merge Overlapping Intervals
- Sort an array of 0s, 1s and 2s
- Merge with O(1) extra space
- Majority Element
- Peak element
- Count possible triangles
- Sub-array with given sum
- Longest Subarray with Equal 0s and 1s
- Longest Common Span in Two Binary Arrays
- Construct an array from its pair-sum array
- 2 Sum - All Pairs
- 2 Sum - Distinct Pairs
- 3 Sum - Find Any
- 3 Sum - Closest Triplet
- 4 Sum - Find Any [More problems on 4 Sum in Hard Section]
Hard Problems on Array:
- Surpasser Count
- Trapping Rain Water
- Top K Frequent Elements
- Kth Missing Positive Number in a Sorted Array
- Stock Buy and Sell - At Most K Transactions
- Stock Buy and Sell - At Most 2 Transactions
- Median in a Stream
- Smallest Difference Triplet from 3 arrays
- Max occurred in n ranges
- 3 Sum - Distinct Triplets
- 3 Sum - All Triplets
- 4 Sum - Distinct Quadruples
- 4 Sum - All Quadruples
- 4 Sum - Closest Quadruple
Expert Problems for Competitive Programmers
- MO’s Algorithm
- Square Root (Sqrt) Decomposition Algorithm
- Sparse Table
- Range sum query using Sparse Table
- Range Minimum Query (Square Root Decomposition and Sparse Table)
- Range LCM Queries
- Merge Sort Tree for Range Order Statistics
- Minimum number of jumps to reach end
- Space optimization using bit manipulations
- Max value of Sum( i*arr[i]) with only rotations allowed
Quick Links :
Recommended:
- Learn Data Structure and Algorithms | DSA Tutorial
- Introduction to Arrays – Data Structure and Algorithm Tutorials
- Introduction to Matrix or Grid – Data Structure and Algorithms Tutorial