Printing all Subarrays
Lesson 23 of 26 in Coddy's Arrays in C++ course.
In order to print all the possible subarrays of the given array, we need a loop for a starting point, another loop for the ending point, and an extra loop for printing all the elements between starting and ending point.
for(int i=0;i<n;i++){ // i for starting point
for(int j=i;j<n;j++){ // j for ending point
for(int k=i;k<=j;k++){ // k for printing all elements
cout<<arr[k]<<" ";
}
cout<<endl;
}
}
Challenge
Print the combined sum of all the subarrays of the given array. Complete the function and return the combined sum of all subarrays.
Try it yourself
#include<iostream>
using namespace std;
#include<vector>
int SubArraySum(vector<int>arr, int n){
//Code here
}
All lessons in Arrays in C++
2Memory Analysis
Memory Allocation8Subarray
IntroductionSubsequencePrinting all SubarraysSubarray with given sumCumulative sum of SubarrayKadane's Algorithm