Array Data Structure Guide
Last Updated :
07 Nov, 2024
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) at contiguous locations 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/C++
- Vector in C++ STL
- Arrays in Java
- ArrayList in Java
- List in Python
- Arrays in C#
- Arrays in JavaScript
Basic Problems on Array:
- Linear Search
- Largest element in an Array
- Second largest element in an array
- Largest three elements in an array
- Leaders in an array
- Check if array is Sorted
- Remove duplicates from Sorted Array
- Generate all subarrays
- Reverse an array
- Rotate an Array
- Move all zeroes to end of array
- Min increment operations 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 in an array
- Duplicate within K Distance
- Rearrange to make even positioned greater
- Sum of all Subarrays
- Stock Buy and Sell – Multiple Transaction Allowed
- Single Element Among Doubles
- Find the Missing Number
- Missing and Repeating
- Only Repeating from 1 to n-1
- Sorted Subsequence of Size 3
- Sort an array of 0s and 1s
- Segregate even and odd numbers
- Sort an array in wave form
- Maximum Subarray Sum
- K Concatenation Max Subarray Sum
- Maximum Subarray Product
- Equilibrium index of an array
- 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:
- Rearrange an array such that arr[i] = i
- Sort an array of 1 to n
- Reorder an array according to given indexes
- Minimum Swaps to Sort
- Min in a Sorted and Rotated
- Max in a Sorted and Rotated
- Rotation Count in Rotated Sorted
- Search in a sorted and rotated
- Sort a nearly sorted (or K sorted) array
- Product Except Self
- K-th Largest Sum Subarray
- Find the smallest missing number
- Smallest subarray with sum greater than x
- Inversion count
- Merge Overlapping Intervals
- Sort an array of 0s, 1s and 2s
- Merge two sorted arrays with O(1) extra space
- Majority Element
- Find a peak element
- Count the number of possible triangles
- Trapping Rain Water
- Find sub-array with given sum
- Longest Subarray with Equal 0s and 1s
- Longest Common Span in Two Binary Arrays
- Find all triplets with zero sum
- Construct an array from its pair-sum array
- Maximum equilibrium sum in an array
- 2 Sum - All Pairs
- 2 Sum - Distinct Pairs
- 3 Sum - Find Any
- 3 Sum - Closest Triplet
- 4 Sum - Find Any [More problems an 4 Sum are in Hard Section]
Hard Problems on Array:
- Top K Frequent Elements in an Array
- 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
- Maximum occurred integer 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


