Generate All Subarrays Problem
Generate All Subarrays Problem — ExecCode Easy DSA Practice
Solve the Generate All Subarrays problem on ExecCode. Free online easy DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given an array arr, generate all contiguous subarrays. A subarray is a contiguous slice arr[i..j] (inclusive). For an array of length n there are n·(n+1)/2 subarrays. Example: arr = [1,2,3] → [1], [1,2], [1,2,3], [2], [2,3], [3]
Examples
Input arr = [1, 2, 3]; Output [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]. Input arr = [4, 5]; Output [[4], [4, 5], [5]]
Constraints
1 <= arr.length <= 20 for visualization Elements may be any integers
Practice Generate All Subarrays free on ExecCode. Browse DSA problems, topic map, and placement guides.