Sum of all odd length subarrays
Last Updated :
07 Jan, 2024
Given an array arr[] consisting of N integers, the task is to find the sum of all the elements of all possible subarrays of odd length.
Examples:
Input: arr[] = {3, 2, 4}
Output: 18
Explanation:
The odd length subarrays along with their sum are as follows:
1) {3} = sum is 3.
2) {2} = sum is 2.
3) {4} = sum is 4.
4) {3, 2, 4} = sum is 3 + 2 + 4 = 9.
Therefore, sum of all subarrays = 3 + 2 + 4 + 9 = 18.
Input: arr[] = {1, 2, 1, 2}
Output: 15
Explanation:
The odd length subarrays along with their sum are as follows:
1) {1} = sum is 1.
2) {2} = sum is 2.
3) {1} = sum is 1.
4) {2} = sum is 2.
5) {1, 2, 1} = sum is 1 + 2 + 1 = 4.
6) {2, 1, 2} = sum is 2 + 1 + 2 = 5.
Therefore, sum of all subarrays = 1 + 2 + 1 + 2 + 4 + 5 = 15.
Naive Approach: The simplest approach is to generate all possible subarrays of odd length from the given array and find the sum of all such subarrays.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int OddLengthSum(vector< int >& arr)
{
int sum = 0;
int l = arr.size();
for ( int i = 0; i < l; i++) {
for ( int j = i; j < l; j += 2) {
for ( int k = i; k <= j; k++) {
sum += arr[k];
}
}
}
return sum;
}
int main()
{
vector< int > arr = { 1, 5, 3, 1, 2 };
cout << OddLengthSum(arr);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG{
static int OddLengthSum( int [] arr)
{
int sum = 0 ;
int l = arr.length;
for ( int i = 0 ; i < l; i++)
{
for ( int j = i; j < l; j += 2 )
{
for ( int k = i; k <= j; k++)
{
sum += arr[k];
}
}
}
return sum;
}
public static void main (String[] args)
{
int [] arr = { 1 , 5 , 3 , 1 , 2 };
System.out.print(OddLengthSum(arr));
}
}
|
Python3
def OddLengthSum(arr):
sum = 0
l = len (arr)
for i in range (l):
for j in range (i, l, 2 ):
for k in range (i, j + 1 , 1 ):
sum + = arr[k]
return sum
arr = [ 1 , 5 , 3 , 1 , 2 ]
print (OddLengthSum(arr))
|
C#
using System;
class GFG{
static int OddLengthSum( int [] arr)
{
int sum = 0;
int l = arr.Length;
for ( int i = 0; i < l; i++)
{
for ( int j = i; j < l; j += 2)
{
for ( int k = i; k <= j; k++)
{
sum += arr[k];
}
}
}
return sum;
}
public static void Main ()
{
int [] arr = { 1, 5, 3, 1, 2 };
Console.Write(OddLengthSum(arr));
}
}
|
Javascript
<script>
function OddLengthSum(arr)
{
var sum = 0;
var l = arr.length;
for ( var i = 0; i < l; i++)
{
for ( var j = i; j < l; j += 2)
{
for ( var k = i; k <= j; k++)
{
sum += arr[k];
}
}
}
return sum;
}
var arr = [ 1, 5, 3, 1, 2 ]
document.write(OddLengthSum(arr));
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to observe the following pattern after generating all the subarrays of odd length:
- For any element at index idx there are (idx + 1) choices on the left side of it and (N – idx) choices on the right side of it.
- Therefore, for any element arr[i], the count of arr[i] is (i + 1) * (N – i) in all the subarrays.
- So, for an element arr[i], there are ((i + 1) * (N – i) + 1) / 2 sub-arrays with odd length.
- Finally, arr[i] will have a total of ((i + 1) * (n – i) + 1) / 2 frequency in the sum.
Therefore, to find the sum of all elements of all the subarrays of odd length, the idea is to iterate over the array and for every ith array element, add [((i + 1) * (n – i) + 1) / 2]*arr[i] to the sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int OddLengthSum(vector< int >& arr)
{
int sum = 0;
int l = arr.size();
for ( int i = 0; i < l; i++) {
sum += (((i + 1)
* (l - i)
+ 1)
/ 2)
* arr[i];
}
return sum;
}
int main()
{
vector< int > arr = { 1, 5, 3, 1, 2 };
cout << OddLengthSum(arr);
return 0;
}
|
Java
class GFG{
static int OddLengthSum( int []arr)
{
int sum = 0 ;
int l = arr.length;
for ( int i = 0 ; i < l; i++)
{
sum += (((i + 1 ) * (l - i) +
1 ) / 2 ) * arr[i];
}
return sum;
}
public static void main(String[] args)
{
int []arr = { 1 , 5 , 3 , 1 , 2 };
System.out.print(OddLengthSum(arr));
}
}
|
Python3
def OddLengthSum(arr):
Sum = 0
l = len (arr)
for i in range (l):
Sum + = ((((i + 1 ) *
(l - i) +
1 ) / / 2 ) * arr[i])
return Sum
arr = [ 1 , 5 , 3 , 1 , 2 ]
print (OddLengthSum(arr))
|
C#
using System;
class GFG{
static int OddLengthSum( int []arr)
{
int sum = 0;
int l = arr.Length;
for ( int i = 0; i < l; i++)
{
sum += (((i + 1) *
(l - i) + 1) / 2) * arr[i];
}
return sum;
}
public static void Main(String[] args)
{
int []arr = {1, 5, 3, 1, 2};
Console.Write(OddLengthSum(arr));
}
}
|
Javascript
<script>
function OddLengthSum(arr)
{
let sum = 0;
let l = arr.length;
for (let i = 0; i < l; i++)
{
sum += Math.floor(((i + 1) * (l - i) +
1) / 2) * arr[i];
}
return sum;
}
let arr = [ 1, 5, 3, 1, 2 ];
document.write(OddLengthSum(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...