Minimum sum of medians of all possible K length subsequences of a sorted array
Last Updated :
13 Sep, 2021
Given a sorted array arr[] consisting of N integers and a positive integer K(such that N%K is 0), the task is to find the minimum sum of the medians of all possible subsequences of size K such that each element belongs to only one subsequence.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 2
Output: 6
Explanation:
Consider the subsequences of size K as {1, 4}, {2, 5}, and {3, 6}.
The sum of medians of all the subsequences is (1 + 2 + 3) = 6 which is the minimum possible sum.
Input: K = 3, arr[] = {3, 11, 12, 22, 33, 35, 38, 67, 69, 71, 94, 99}, K = 3
Output: 135
Naive Approach: The given problem can be solved by generating all possible K-sized sorted subsequences and print the median of all those subsequences as the result.
Time Complexity: O(2N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using the Greedy Approach for the construction of all the subsequences. The idea is to select K/2 elements from starting of the array and K/2 elements from the ending of the array such that the median always occurs in the first part. Follow the steps below to solve the problem:
- Initialize a variable, say res that stores the resultant sum of medians.
- Initialize a variable, say T as N/K to store the number of subsequences required and a variable D as (K + 1)/2 to store the distance between the medians.
- Initialize a variable, say i as (D – 1) to store the index of the first median to be added to the result.
- Iterate until the value of i < N and T > 0, and perform the following steps:
- Add the value of arr[i] to the variable res.
- Increment the value of i by D to get the index of the next median.
- Decrement the value of T by 1.
- After completing the above steps, print the value of res as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void sumOfMedians( int arr[], int N,
int K)
{
int selectMedian = (K + 1) / 2;
int totalArrays = N / K;
int minSum = 0;
int i = selectMedian - 1;
while (i < N and totalArrays != 0) {
minSum = minSum + arr[i];
i = i + selectMedian;
totalArrays--;
}
cout << minSum;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int N = sizeof (arr) / sizeof ( int );
int K = 2;
sumOfMedians(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static void sumOfMedians( int arr[], int N, int K)
{
int selectMedian = (K + 1 ) / 2 ;
int totalArrays = N / K;
int minSum = 0 ;
int i = selectMedian - 1 ;
while (i < N && totalArrays != 0 ) {
minSum = minSum + arr[i];
i = i + selectMedian;
totalArrays--;
}
System.out.println(minSum);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
int N = arr.length;
int K = 2 ;
sumOfMedians(arr, N, K);
}
}
|
Python3
def sumOfMedians(arr, N, K):
selectMedian = (K + 1 ) / / 2
totalArrays = N / / K
minSum = 0
i = selectMedian - 1
while (i < N and totalArrays ! = 0 ):
minSum = minSum + arr[i]
i = i + selectMedian
totalArrays - = 1
print (minSum)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
N = len (arr)
K = 2
sumOfMedians(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void sumOfMedians( int [] arr, int N, int K)
{
int selectMedian = (K + 1) / 2;
int totalArrays = N / K;
int minSum = 0;
int i = selectMedian - 1;
while (i < N && totalArrays != 0) {
minSum = minSum + arr[i];
i = i + selectMedian;
totalArrays--;
}
Console.WriteLine(minSum);
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int N = arr.Length;
int K = 2;
sumOfMedians(arr, N, K);
}
}
|
Javascript
<script>
function sumOfMedians(arr, N, K)
{
let selectMedian = Math.floor((K + 1) / 2);
let totalArrays = Math.floor(N / K);
let minSum = 0;
let i = selectMedian - 1;
while (i < N && totalArrays != 0) {
minSum = minSum + arr[i];
i = i + selectMedian;
totalArrays--;
}
document.write(minSum);
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let N = arr.length;
let K = 2;
sumOfMedians(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...