Array Data Structure
An array is a fundamental and linear data structure that stores items at contiguous locations. Note that in case of C/C++ and Java-Primitive-Arrays, actual elements are stored at contiguous locations. And in case of Python, JS, Java-Non-Primitive, references are stored 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.
Arrays are used to build other data structures like Stack Queue, Deque, Graph, Hash Table, etc. An array is not useful in places where we have operations like insert in the middle, delete from middle and search in a unsorted data.
Basics
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
- Print Alternates
- 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
Easy Problems
- 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
- Max Subarray Sum
- Equilibrium index
- Two Sum - Find if there is a Pair
- Two Sum - Closest Pair [More problems on 2 Sum in Medium Section]
- Split array into three equals
- Maximum Consecutive 1s with K Flips
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
Medium Problems
- Make arr[i] = i
- Maximum Circular Subarray Sum
- Reorder according to given indexes
- Product Except Self
- K-th Largest Sum Subarray
- Smallest missing number
- Smallest subarray with sum greater than x
- Majority 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
- 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 :
What is Array