Find the sum of medians of all odd length subarrays
Last Updated :
23 Jul, 2025
Given an array arr[] of size N, the task is to find the sum of medians of all sub-array of odd-length.
Examples:
Input: arr[] = {4, 2, 5, 1}
Output: 18
Explanation : Sub-Arrays of odd length and their medians are :
- [4] -> Median is 4
- [4, 2, 5] -> Median is 4
- [2] -> Median is 2
- [2, 5, 1] -> Median is 2
- [5] -> Median is 5
- [1] -> Median is 1
Their sum = 4 + 4+ 2 + 2 + 5 +1 = 18
Input: arr[] = {1, 2}
Output: 3
Pre-requisites: Median of Stream of Running Integers using STL
Naive Approach: Generate each and every sub-array. If the length of the sub-array is odd, then sort the sub-array and return the middle element.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find sum of medians
// of all odd-length subarrays
int solve(vector<int> arr)
{
int ans = 0;
int n = arr.size();
// Loop to calculate the sum
for(int i = 0; i < n; i++)
{
vector<int> new_arr;
for(int j = i; j < n; j++)
{
new_arr.push_back(arr[j]);
// Odd length subarray
if ((new_arr.size() % 2) == 1)
{
sort(new_arr.begin(), new_arr.end());
int mid = new_arr.size() / 2;
ans += new_arr[mid];
}
}
}
return ans;
}
// Driver Code
int main()
{
vector<int> arr = { 4, 2, 5, 1 };
cout << solve(arr);
}
// This code is contributed by Samim Hossain Mondal.
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find sum of medians
// of all odd-length subarrays
static int solve(int[] arr) {
int ans = 0;
int n = arr.length;
// Loop to calculate the sum
for (int i = 0; i < n; i++) {
List<Integer> new_arr = new LinkedList<Integer>();
for (int j = i; j < n; j++) {
new_arr.add(arr[j]);
// Odd length subarray
if ((new_arr.size() % 2) == 1) {
Collections.sort(new_arr);
int mid = new_arr.size() / 2;
ans += new_arr.get(mid);
}
}
}
return ans;
}
// Driver Code
public static void main(String[] args) {
int[] arr = { 4, 2, 5, 1 };
System.out.println(solve(arr));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to find sum of medians
# of all odd-length subarrays
def solve(arr):
ans = 0
n = len(arr)
# Loop to calculate the sum
for i in range(n):
new_arr = []
for j in range(i, n, 1):
new_arr.append(arr[j])
# Odd length subarray
if (len(new_arr)) % 2 == 1:
new_arr.sort()
mid = len(new_arr)//2
ans += new_arr[mid]
return (ans)
# Driver Code
if __name__ == "__main__":
arr = [4, 2, 5, 1]
print(solve(arr))
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find sum of medians
// of all odd-length subarrays
static int solve(int[] arr) {
int ans = 0;
int n = arr.Length;
// Loop to calculate the sum
for (int i = 0; i < n; i++) {
List<int> new_arr = new List<int>();
for (int j = i; j < n; j++) {
new_arr.Add(arr[j]);
// Odd length subarray
if ((new_arr.Count % 2) == 1) {
new_arr.Sort();
int mid = new_arr.Count / 2;
ans += new_arr[mid];
}
}
}
return ans;
}
// Driver Code
public static void Main() {
int[] arr = { 4, 2, 5, 1 };
Console.Write(solve(arr));
}
}
// This code is contributed by Saurabh Jaiswal
JavaScript
<script>
// javascript program for the above approach
// Function to find sum of medians
// of all odd-length subarrays
function solve(arr) {
var ans = 0;
var n = arr.length;
// Loop to calculate the sum
for (var i = 0; i < n; i++) {
var new_arr= new Array();
for (var j = i; j < n; j++) {
new_arr.push(arr[j]);
// Odd length subarray
if ((new_arr.length % 2) == 1) {
new_arr.sort();
var mid = Math.floor(new_arr.length / 2);
// document.write(mid);
ans += new_arr[mid];
}
}
}
return ans;
}
// Driver Code
var arr = [ 4, 2, 5, 1 ];
document.write(solve(arr));
// This code is contributed by shikhasingrajput
</script>
Time Complexity: O(N3 * Log(N))
Auxiliary Space: O(N)
Note: Instead of sorting array each time, which costs (N*logN), insertion sort can be applied. But still, overall Time Complexity will be O(N3).
Efficient Approach: The median of the sorted array is the value separating the higher half from the lower half in the array. For finding out the median, we only need the middle element, rather than the entire sorted array. The approach of Median of Stream of Running Integers can be applied over here. Follow the steps mentioned below
- Use max and min heaps to calculate the running median.
- Traverse each and every element in the array.
- While creating a new subarray, add an element into the heaps and return median if the size is odd else return 0.
- Max_heap is used to store lower half elements such that the maximum element is at the top and min_heap is used to store higher half elements such that the minimum element is at the top.
- The difference between both the heaps should not be greater than one, and one extra element is always placed in max_heap.
Note: Here max_heap is implemented using min_heap, by just negating the values so that the maximum negative element can be popped.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
// Class to find median
class find_median {
public:
// Constructor to declare two heaps
find_median()
{
// Store lower half elements such that
// maximum element is at top
max_heap
= std::priority_queue<int, std::vector<int>,
std::less<int> >();
// Store higher half elements such that
// minimum element is at top
min_heap
= std::priority_queue<int, std::vector<int>,
std::less<int> >();
}
// Function to add element
int add(int val)
{
// If max_heap is empty or current element
// smaller than top of max_heap
if (max_heap.empty() || max_heap.top() > val) {
max_heap.push(-val);
}
else {
min_heap.push(val);
}
// If size of max_heap + 1 less
// than min_heap
if (max_heap.size() + 1 > min_heap.size()) {
val = -max_heap.top();
max_heap.pop();
min_heap.push(val);
}
// If size of min_heap
// less than max_heap
if (min_heap.size() > max_heap.size()) {
val = min_heap.top();
min_heap.pop();
max_heap.push(-val);
}
// Finally if sum of sizes is odd,
// return median
if ((min_heap.size() + max_heap.size()) % 2 == 1) {
return -max_heap.top();
}
// Else return 0
else {
return 0;
}
}
std::priority_queue<int, std::vector<int>,
std::less<int> >
max_heap;
std::priority_queue<int, std::vector<int>,
std::less<int> >
min_heap;
};
// Function to calculate the sum of all odd length subarrays
int solve(std::vector<int> arr)
{
int ans = 0;
// Size of the array
int n = arr.size();
for (int i = 0; i < n; i++) {
// Create an object of class find_median
find_median obj;
for (int j = i; j < n; j++)
{
// Add value to the heaps using object
int val = obj.add(arr[j]);
ans += val;
}
}
return (ans);
}
// Driver Code
int main()
{
std::vector<int> arr = { 4, 2, 5, 1 };
std::cout << solve(arr);
return 0;
}
// This code is contributed by phasing17.
Java
import java.util.*;
// A class to find the median of the array
class FindMedian {
// Declare two heaps
// Store lower half elements such that
// maximum element is at top
private List<Integer> max_heap;
// Store higher half elements such that
// minimum element is at top
private List<Integer> min_heap;
// Constructor to initialize the heaps
public FindMedian() {
max_heap = new ArrayList<>();
min_heap = new ArrayList<>();
}
public int add(int val) {
// len(max_heap) == 0 or curr_element
// smaller than max_heap top
if (max_heap.size() == 0 || max_heap.get(0) > val) {
max_heap.add(-val);
}
else {
min_heap.add(val);
}
// If size of max_heap + 1 greater
// than min_heap
if (max_heap.size() + 1 > min_heap.size()) {
int v = max_heap.get(max_heap.size() - 1);
max_heap.remove(max_heap.size() - 1);
min_heap.add(-v);
}
// If size of min_heap
// greater than max_heap
if (min_heap.size() > max_heap.size()) {
int v = min_heap.get(min_heap.size() - 1);
min_heap.remove(min_heap.size() - 1);
max_heap.add(-v);
}
// Finally if sum of sizes is odd,
// return median
if ((min_heap.size() + max_heap.size()) % 2 == 1) {
return (-max_heap.get(0));
}
// Else return 0
else {
return 0;
}
}
}
class GFG {
// Function to calculate the sum
// of all odd length subarrays
public static int solve(int[] arr) {
int ans = 0;
// Size of the array
int n = arr.length;
for (int i = 0; i < n; i++) {
// Create an object
// of class FindMedian
FindMedian obj = new FindMedian();
for (int j = i; j < n; j++) {
// Add value to the heaps
// using object
int val = obj.add(arr[j]);
ans += val;
}
}
return (ans);
}
// Driver Code
public static void main(String[] args) {
int[] arr = { 4, 2, 5, 1 };
System.out.println(solve(arr));
}
}
Python3
# Python program for the above approach
from heapq import heappush as push, heappop as pop
# Find the sum of medians of odd-length
# subarrays
class find_median():
# Constructor to declare two heaps
def __init__(self):
# Store lower half elements such that
# maximum element is at top
self.max_heap = []
# Store higher half elements such that
# minimum element is at top
self.min_heap = []
def add(self, val):
# len(max_heap) == 0 or curr_element
# smaller than max_heap top
if (len(self.max_heap) == 0 or
self.max_heap[0] > val):
push(self.max_heap, -val)
else:
push(self.min_heap, val)
# If size of max_heap + 1 greater
# than min_heap
if (len(self.max_heap)+1 >
len(self.min_heap)):
val = pop(self.max_heap)
push(self.min_heap, -val)
# If size of min_heap
# greater than max_heap
if (len(self.min_heap) >
len(self.max_heap)):
val = pop(self.min_heap)
push(self.max_heap, -val)
# Finally if sum of sizes is odd,
# return median
if (len(self.min_heap) +
len(self.max_heap)) % 2 == 1:
return (-self.max_heap[0])
# Else return 0
else:
return 0
# Function to calculate the sum
# of all odd length subarrays
def solve(arr):
ans = 0
# Size of the array
n = len(arr)
for i in range(n):
# Create an object
# of class find_median
obj = find_median()
for j in range(i, n, 1):
# Add value to the heaps
# using object
val = obj.add(arr[j])
ans += val
return (ans)
# Driver Code
if __name__ == "__main__":
arr = [4, 2, 5, 1]
print(solve(arr))
C#
// C# Program for the above approach
using System;
using System.Collections.Generic;
// A class to find the median of the array
public class FindMedian
{
// Declare two heaps
// Store lower half elements such that
// maximum element is at top
private List<int> max_heap;
// Store higher half elements such that
// minimum element is at top
private List<int> min_heap;
// Constructor to initialize the heaps
public FindMedian()
{
max_heap = new List<int>();
min_heap = new List<int>();
}
public int Add(int val)
{
// len(max_heap) == 0 or curr_element
// smaller than max_heap top
if (max_heap.Count == 0 || max_heap[0] > val) {
max_heap.Add(-val);
}
else {
min_heap.Add(val);
}
// If size of max_heap + 1 greater
// than min_heap
if (max_heap.Count + 1 > min_heap.Count) {
int v = max_heap[max_heap.Count - 1];
max_heap.RemoveAt(max_heap.Count - 1);
min_heap.Add(-v);
}
// If size of min_heap
// greater than max_heap
if (min_heap.Count > max_heap.Count) {
int v = min_heap[min_heap.Count - 1];
min_heap.RemoveAt(min_heap.Count - 1);
max_heap.Add(-v);
}
// Finally if sum of sizes is odd,
// return median
if ((min_heap.Count + max_heap.Count) % 2 == 1) {
return (-max_heap[0]);
}
// Else return 0
else {
return 0;
}
}
}
public class GFG {
// Function to calculate the sum
// of all odd length subarrays
public static int Solve(int[] arr)
{
int ans = 0;
// Size of the array
int n = arr.Length;
for (int i = 0; i < n; i++) {
// Create an object
// of class find_median
FindMedian obj = new FindMedian();
for (int j = i; j < n; j++) {
// Add value to the heaps
// using object
int val = obj.Add(arr[j]);
ans += val;
}
}
return (ans);
}
// Driver Code
public static void Main()
{
int[] arr = { 4, 2, 5, 1 };
Console.WriteLine(Solve(arr));
}
}
// This code is contributed by vinayetbi1
JavaScript
// JavaScript Program for the above approach
// A class to find the median of the array
class find_median {
// Constructor to declare two heaps
constructor() {
// Store lower half elements such that
// maximum element is at top
this.max_heap = [];
// Store higher half elements such that
// minimum element is at top
this.min_heap = [];
}
add(val) {
// len(max_heap) == 0 or curr_element
// smaller than max_heap top
if (this.max_heap.length == 0 || this.max_heap[0] > val) {
this.max_heap.push(-val);
}
else {
this.min_heap.push(val);
}
// If size of max_heap + 1 greater
// than min_heap
if (this.max_heap.length + 1 > this.min_heap.length) {
let val = this.max_heap.pop();
this.min_heap.push(-val);
}
// If size of min_heap
// greater than max_heap
if (this.min_heap.length > this.max_heap.length) {
let val = this.min_heap.pop();
this.max_heap.push(-val);
}
// Finally if sum of sizes is odd,
// return median
if ((this.min_heap.length + this.max_heap.length) % 2 === 1) {
return (-this.max_heap[0]);
}
// Else return 0
else {
return 0;
}
}
}
// Function to calculate the sum
// of all odd length subarrays
function solve(arr) {
let ans = 0;
// Size of the array
let n = arr.length;
for (let i = 0; i < n; i++) {
// Create an object
// of class find_median
let obj = new find_median();
for (let j = i; j < n; j++) {
// Add value to the heaps
// using object
let val = obj.add(arr[j]);
ans += val;
}
}
return (ans);
}
// Driver Code
let arr = [4, 2, 5, 1];
console.log(solve(arr));
// This code is contributed by vinayetbi1.
Time Complexity: O(N2 * Log(N))
Auxiliary Space: O(N)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem