Mastery Points
0
Subarray Concepts in dsa
Context & Logic
A subarray is a contiguous part of an array. Understanding how to generate and process subarrays is fundamental for many other DSA problems.
Example
function printSubarrays(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i; j < arr.length; j++) {
console.log(arr.slice(i, j + 1));
}
}
}Step-by-Step Logic
1
Use a outer loop to pick the starting index.
2
Use an inner loop to pick the ending index.
3
Extract elements between start and end index (contiguously).
4
Total number of subarrays is n*(n+1)/2.
Complexity Metrics
Time Efficiency
O(n²) to generate all
Memory Footprint
O(1) extra space