publicclassSolution {
public List<List<Integer>>getAllSubarrays(int[] arr) {
List<List<Integer>> result =new ArrayList<>();
int n = arr.length;
// Outer loop to fix the start indexfor (int i = 0; i < n; i++) {
// Inner loop to fix the end indexfor (int j = i; j < n; j++) {
List<Integer> subarray =new ArrayList<>();
// Collecting subarray from i to jfor (int k = i; k <= j; k++) {
subarray.add(arr[k]);
}
result.add(subarray);
}
}
return result;
}
publicstaticvoidmain(String[] args) {
Solution solution =new Solution();
int[] arr = {1, 2, 3};
List<List<Integer>> subarrays = solution.getAllSubarrays(arr);
System.out.println(subarrays); // Expected output: [[1], [1, 2], [1, 2,// 3], [2], [2, 3], [3]] }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defgetAllSubarrays(self, arr: List[int]) -> List[List[int]]:
result = []
n = len(arr)
# Outer loop to fix the start indexfor i in range(n):
# Inner loop to fix the end indexfor j in range(i, n):
subarray = []
# Collecting subarray from i to jfor k in range(i, j +1):
subarray.append(arr[k])
result.append(subarray)
return result