Sum of all odd length subarray’s
Tags: Data Structures, Programming Problem, Leetcode, Algorithms, Interview Questions

Problem statement — find sum of all odd length subarray’s of a given array.

Example : arr = [4,3,6,5,2]

possible odd length arrays are [4] [3] [6] [5] [2]

[4,3,6] [3,6,5] [6,5,2]

[4,3,6,5,2]

sum of all odd subarray’s = 80

You can try this problem on leetcode , link is here.

First Solution-

Generate the subarray of odd length find sum of this arr

What will be the time complexity for this approach?

since number subarray’s for an array are order of O(n²)

and then finding the sum of each array is of complexity of O(n)

overall complexity will be O(n³)

Optimized solution of First Solution:

In the above solution, finding odd length sub-array is something that you just cant skip or optimize further, Only thing left for optimization is how can we get rid of this O(n) order sum process for every sub-array. To do so we can do one thing create another say it sum_array, In which nth position will hold the sum of all elements from index=0 to index=n of input array.

whenever you want to find sum of some sub-array whose starting index is i and end index j we can simply get it by subtracting sum_array[i-1] from sum_array[j].

since creation and filling of this sum_array is only one time and its going to cost you O(n) this process reduced our whole solution complexity from O(n³) to O(n²).

One more thing, instead of creating a new sum_array it is better to use the existing input array , the reason is once we are done with sum array we don’t need to know about individual elements of the input. This step will reduce your memory requirement by almost half.

Once you are done with creation of sum array its time to find the odd length sub arrays and

below is the pseudo code for this problem

int sumOfOddSubarrays(vector<int> arr){ // arr is the input array /* lets create sum_array instead of

* using new array I am going to use the

* input array itself

* ex. arr = [4,3,2,5,6]

*/

for(int i=1;i<arr.size;++i){

// Every time we will add the current element

// with previous element

// (previous element is already sum of all elements before the current element)

arr[i] = arr[i] + arr[i-1]

} // at this point arr will be = [4,7,9,14,20] // Lets create the odd length subarrays and get sum of all subarrays int sum = 0; // this will hold sum of all subarrays

int len = 1; // length of the subarray

while(len<=arr.size()){

// this while loop will execute till length is less than the size of array

// this for loop will look for all the subarray of length len starting from different index

int start_index = 0;

//start_index+len-1 will be the end index for subarray having start index

while(start_index+len-1<arr.size()){

// start index is = start_index

// end index is start_index + len - 1

// sum of subarray will be arr[start_index+len -1] - arr[start_index-1]

// if start_index is 0 then sum is arr[start_index+len-1]

//add this to sum variable

if(start_index==0){

sum = sum + arr[start_index+len-1];

}

else{

sum = sum + arr[start_index+len-1] - arr[start_index-1];

}

// increase the start_index

++start_index;

}



// increase the len by 2 because we are looking for odd length

len = len + 2; }

return sum;

}

If you treat this problem like a programming problem the above solution is fine and optimized. But if you shift your view from treating as programming problem to mathematical problem the solution for this problem can be more compact and optimized O(n) order solution.

int sumOddLengthSubarrays(vector<int>& arr) {

int size = arr.size();

int sum=0;

for(int i=0;i<size;++i)

sum = sum + ((i/2)+1)*(1+(size-i-1)/2)*arr[i] + ((i+1)/2)*((size-i)/2)*arr[i];

return sum;



}

Explanation :

Mathematical approach for this problem

This approach is mostly focus on finding in how many sub-array’s an element is occurring once you find ith element is occurring in k subarrays just add arr[i]*k in to the sum and this process we will do for all elements.

How to find in how many sub-array’s an element is occurring.

Thanks for reading patiently :)