Find largest median of a sub array with length at least K

Given an array arr[] of length N (1? arr[i] ? N) and an integer K. The task is to find the largest median of any subarray in arr[] of at least K size.
Examples:
Input: arr[] = {1, 2, 3, 2, 1}, K = 3Â
Output: 2
Explanation: Here the median of all possible sub arrays with length >= K is 2, so the maximum median is 2.Input: arr[] = {1, 2, 3, 4}, K = 2
Output: 3
Explanation: Here the median of sub array( [3. 4] ) = 3 which is the maximum possible median.
Naive Approach: Go through all the sub-arrays with length at least K in arr[] and find the median of each sub-array and get the maximum median.Â
Below is the implementation of the above approach.
C++
// C++ code to find the maximum median// of a sub array having length at least K.#include <bits/stdc++.h>using namespace std;Â
// Function to find the maximum median// of a sub array having length at least k.int maxMedian(int arr[], int N, int K){    // Variable to keep track    // of maximum median.    int mx_median = -1;Â
    // Go through all the sub arrays    // having length at least K.    for (int i = 0; i < N; i++) {        for (int j = i + K - 1; j < N; j++) {            int len = j - i + 1;            int temp[len];Â
            // Copy all elements of            // arr[i ... j] to temp[].            for (int k = i; k <= j; k++)                temp[k - i] = arr[k];Â
            // Sort the temp[] array            // to find the median.            sort(temp, temp + len);Â
            mx_median = max(mx_median,                            temp[(len - 1)                                 / 2]);        }    }    return mx_median;}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 1, 2, 3, 2, 1 };Â Â Â Â int N = sizeof(arr) / sizeof(arr[0]);Â Â Â Â int K = 3;Â
    // Function Call    cout << maxMedian(arr, N, K);    return 0;} |
Java
// Java code to find the maximum median// of a sub array having length at least K.import java.util.*;public class GFG{Â
// Function to find the maximum median// of a sub array having length at least k.static int maxMedian(int arr[], int N, int K){       // Variable to keep track    // of maximum median.    int mx_median = -1;Â
    // Go through all the sub arrays    // having length at least K.    for (int i = 0; i < N; i++) {        for (int j = i + K - 1; j < N; j++) {            int len = j - i + 1;            int temp[] = new int[len];Â
            // Copy all elements of            // arr[i ... j] to temp[].            for (int k = i; k <= j; k++)                temp[k - i] = arr[k];Â
            // Sort the temp[] array            // to find the median.            Arrays.sort(temp);Â
            mx_median = Math.max(mx_median,                            temp[(len - 1)                                 / 2]);        }    }    return mx_median;}Â
// Driver codepublic static void main(String args[]){Â Â Â Â int arr[] = { 1, 2, 3, 2, 1 };Â Â Â Â int N = arr.length;Â Â Â Â int K = 3;Â
    // Function Call    System.out.println(maxMedian(arr, N, K));}}Â
// This code is contributed by Samim Hossain Mondal. |
Python3
# Python code for the above approach Â
# Function to find the maximum median# of a sub array having length at least k.def maxMedian(arr, N, K):Â
    # Variable to keep track    # of maximum median.    mx_median = -1;Â
    # Go through all the sub arrays    # having length at least K.    for i in range(N):        for j in range(i + K - 1, N):            _len = j - i + 1;            temp = [0] * _lenÂ
            # Copy all elements of            # arr[i ... j] to temp[].            for k in range(i, j + 1):                temp[k - i] = arr[k];Â
            # Sort the temp[] array            # to find the median.            temp.sort()Â
            mx_median = max(mx_median, temp[((_len - 1) // 2)]);    return mx_median;Â
Â
# Driver codearr = [1, 2, 3, 2, 1];N = len(arr)K = 3;Â
# Function Callprint(maxMedian(arr, N, K));Â
# This code is contributed by Saurabh Jaiswal |
C#
// C# code to find the maximum median// of a sub array having length at least K.using System;class GFG{Â
// Function to find the maximum median// of a sub array having length at least k.static int maxMedian(int []arr, int N, int K){       // Variable to keep track    // of maximum median.    int mx_median = -1;Â
    // Go through all the sub arrays    // having length at least K.    for (int i = 0; i < N; i++) {        for (int j = i + K - 1; j < N; j++) {            int len = j - i + 1;            int []temp = new int[len];Â
            // Copy all elements of            // arr[i ... j] to temp[].            for (int k = i; k <= j; k++)                temp[k - i] = arr[k];Â
            // Sort the temp[] array            // to find the median.            Array.Sort(temp);Â
            mx_median = Math.Max(mx_median,                            temp[(len - 1)                                 / 2]);        }    }    return mx_median;}Â
// Driver Code:public static void Main(){Â Â Â Â int []arr = { 1, 2, 3, 2, 1 };Â Â Â Â int N = arr.Length;Â Â Â Â int K = 3;Â
    // Function Call    Console.WriteLine(maxMedian(arr, N, K));}}Â
// This code is contributed by Samim Hossain Mondal. |
Javascript
<script>Â Â Â Â Â Â Â // JavaScript code for the above approach Â
       // Function to find the maximum median       // of a sub array having length at least k.       function maxMedian(arr, N, K)       {                   // Variable to keep track           // of maximum median.           let mx_median = -1;Â
           // Go through all the sub arrays           // having length at least K.           for (let i = 0; i < N; i++) {               for (let j = i + K - 1; j < N; j++) {                   let len = j - i + 1;                   let temp = new Array(len).fill(0)Â
                   // Copy all elements of                   // arr[i ... j] to temp[].                   for (let k = i; k <= j; k++)                       temp[k - i] = arr[k];Â
                   // Sort the temp[] array                   // to find the median.                   temp.sort(function (a, b) { return a - b })                   let x = Math.floor((len - 1) / 2)                   mx_median = Math.max(mx_median,                       temp[x]);               }           }           return mx_median;       }Â
       // Driver code       let arr = [1, 2, 3, 2, 1];       let N = arr.length;       let K = 3;Â
       // Function Call       document.write(maxMedian(arr, N, K));Â
 // This code is contributed by Potta Lokesh   </script> |
2
Time Complexity: O(N3 log(N))
Auxiliary Space: O(N)
Efficient Approach: An efficient approach is to use the binary search algorithm. Prefix sum technique can be used here in order to check quickly if there exists any segment of length at least K having a sum greater than zero, it helps in reducing the time complexity. Follow the steps below to solve the given problem.
- Let l and r denote the left and right boundary for our binary search algorithm.
- For each mid-value check if it is possible to have a median equal to mid of a subarray having a length of at least K.
- Define a function for checking the above condition.
- Take an array of length N ( Pre[] ) and at i-th index store 1 if arr[i] >= mid else -1.
- Calculate the prefix sum of the array Pre[].
- Now in some segments, the median is at least x if the sum on this sub-segment is positive. Now we only need to check if the array Pre[] consisting of ?1 and 1 has a sub-segment of length at least K with positive-sum.
- For prefix sum at position i choose the minimum prefix sum amongst positions 0, 1, . . ., i?K, which can be done using prefix minimum in linear time.
- Maintain the maximum sum of a sub-array having a length of at least K.
- If the maximum sum is greater than 0 return true, else return false.
- Return the maximum median possible finally.
Below is the implementation of the above approach.
C++
// C++ code to find the maximum median// of a sub array having length at least K#include <bits/stdc++.h>using namespace std;Â
// Function to check if// the median is possible or not.bool good(int arr[], int& N, int& K,          int& median){    int pre[N];    for (int i = 0; i < N; i++) {        if (arr[i] >= median)            pre[i] = 1;        else            pre[i] = -1;Â
        if (i > 0)            pre[i] += pre[i - 1];    }Â
    // mx denotes the maximum    // sum of a sub array having    // length at least k.    int mx = pre[K - 1];Â
    // mn denotes the minimum    // prefix sum seen so far.    int mn = 0;Â
    for (int i = K; i < N; i++) {        mn = min(mn, pre[i - K]);        mx = max(mx, pre[i] - mn);    }    if (mx > 0)        return true;    return false;}Â
// Function to find the maximum median// of a sub array having length at least Kint maxMedian(int arr[], int N, int K){    // l and r denote the left and right    // boundary for binary search algorithm    int l = 1, r = N + 1;Â
    // Variable to keep track    // of maximum median    int mx_median = -1;Â
    while (l <= r) {        int mid = (l + r) / 2;        if (good(arr, N, K, mid)) {            mx_median = mid;            l = mid + 1;        }        else            r = mid - 1;    }    return mx_median;}Â
// Driver functionint main(){Â Â Â Â int arr[] = { 1, 2, 3, 2, 1 };Â Â Â Â int N = sizeof(arr) / sizeof(arr[0]);Â Â Â Â int K = 3;Â
    // Function Call    cout << maxMedian(arr, N, K);    return 0;} |
Java
// Java code to find the maximum median// of a sub array having length at least Kimport java.util.*;Â
class GFG{Â
  // Function to check if  // the median is possible or not.  static boolean good(int arr[], int N, int K,                      int median)  {    int []pre = new int[N];    for (int i = 0; i < N; i++) {      if (arr[i] >= median)        pre[i] = 1;      else        pre[i] = -1;Â
      if (i > 0)        pre[i] += pre[i - 1];    }Â
    // mx denotes the maximum    // sum of a sub array having    // length at least k.    int mx = pre[K - 1];Â
    // mn denotes the minimum    // prefix sum seen so far.    int mn = 0;Â
    for (int i = K; i < N; i++) {      mn = Math.min(mn, pre[i - K]);      mx = Math.max(mx, pre[i] - mn);    }    if (mx > 0)      return true;    return false;  }Â
  // Function to find the maximum median  // of a sub array having length at least K  static int maxMedian(int arr[], int N, int K)  {    // l and r denote the left and right    // boundary for binary search algorithm    int l = 1, r = N + 1;Â
    // Variable to keep track    // of maximum median    int mx_median = -1;Â
    while (l <= r) {      int mid = (l + r) / 2;      if (good(arr, N, K, mid)) {        mx_median = mid;        l = mid + 1;      }      else        r = mid - 1;    }    return mx_median;  }Â
  // Driver function  public static void main(String[] args)  {    int arr[] = { 1, 2, 3, 2, 1 };    int N = arr.length;    int K = 3;Â
    // Function Call    System.out.print(maxMedian(arr, N, K));  }}Â
// This code is contributed by 29AjayKumar |
Python3
# Python code to find the maximum median# of a sub array having length at least KÂ
# Function to check if# the median is possible or not.def good(arr, N, K, median):Â Â Â Â pre = [0]*NÂ Â Â Â for i in range(N):Â Â Â Â Â Â Â Â if(arr[i] >= median):Â Â Â Â Â Â Â Â Â Â Â Â pre[i] = 1Â Â Â Â Â Â Â Â else:Â Â Â Â Â Â Â Â Â Â Â Â pre[i] = -1Â
        if(i > 0):            pre[i] += pre[i-1]Â
                 # mx denotes the maximum    # sum of a sub array having    # length at least k.    mx = pre[K-1]Â
    # mn denotes the minimum    # prefix sum seen so far.    mn = 0Â
    for i in range(K, N):        mn = min(mn, pre[i-K])        mx = max(mx, pre[i]-mn)Â
    if(mx > 0):        return TrueÂ
    return FalseÂ
# Function to find the maximum median# of a sub array having length at least Kdef maxMedian(arr, N, K):         # l and r denote the left and right    # boundary for binary search algorithm    l, r = 1, N+1Â
    # Variable to keep track    # of maximum median    mx_median = -1Â
    while(l <= r):        mid = (l+r)//2        if(good(arr, N, K, mid)):            mx_median = mid            l = mid+1        else:            r = mid-1Â
    return mx_medianÂ
arr = [1, 2, 3, 2, 1]N = len(arr)K = 3Â
# Function callprint(maxMedian(arr, N, K))Â
# This code is contributed by lokeshmvs21. |
C#
// C# code to find the maximum median// of a sub array having length at least Kusing System;Â
public class GFG{Â
  // Function to check if  // the median is possible or not.  static bool good(int []arr, int N, int K,                      int median)  {    int []pre = new int[N];    for (int i = 0; i < N; i++) {      if (arr[i] >= median)        pre[i] = 1;      else        pre[i] = -1;Â
      if (i > 0)        pre[i] += pre[i - 1];    }Â
    // mx denotes the maximum    // sum of a sub array having    // length at least k.    int mx = pre[K - 1];Â
    // mn denotes the minimum    // prefix sum seen so far.    int mn = 0;Â
    for (int i = K; i < N; i++) {      mn = Math.Min(mn, pre[i - K]);      mx = Math.Max(mx, pre[i] - mn);    }    if (mx > 0)      return true;    return false;  }Â
  // Function to find the maximum median  // of a sub array having length at least K  static int maxMedian(int []arr, int N, int K)  {    // l and r denote the left and right    // boundary for binary search algorithm    int l = 1, r = N + 1;Â
    // Variable to keep track    // of maximum median    int mx_median = -1;Â
    while (l <= r) {      int mid = (l + r) / 2;      if (good(arr, N, K, mid)) {        mx_median = mid;        l = mid + 1;      }      else        r = mid - 1;    }    return mx_median;  }Â
  // Driver function  public static void Main(String[] args)  {    int []arr = { 1, 2, 3, 2, 1 };    int N = arr.Length;    int K = 3;Â
    // Function Call    Console.Write(maxMedian(arr, N, K));  }}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>// javascript code to find the maximum median// of a sub array having length at least KÂ
 // Function to check if  // the median is possible or not.  function good(arr , N , K,                      median)  {    var pre = Array.from({length: N}, (_, i) => 0);    for (var i = 0; i < N; i++) {      if (arr[i] >= median)        pre[i] = 1;      else        pre[i] = -1;Â
      if (i > 0)        pre[i] += pre[i - 1];    }Â
    // mx denotes the maximum    // sum of a sub array having    // length at least k.    var mx = pre[K - 1];Â
    // mn denotes the minimum    // prefix sum seen so far.    var mn = 0;Â
    for (var i = K; i < N; i++) {      mn = Math.min(mn, pre[i - K]);      mx = Math.max(mx, pre[i] - mn);    }    if (mx > 0)      return true;    return false;  }Â
  // Function to find the maximum median  // of a sub array having length at least K  function maxMedian(arr , N , K)  {       // l and r denote the left and right    // boundary for binary search algorithm    var l = 1, r = N + 1;Â
    // Variable to keep track    // of maximum median    var mx_median = -1;Â
    while (l <= r) {      var mid = parseInt((l + r) / 2);      if (good(arr, N, K, mid)) {        mx_median = mid;        l = mid + 1;      }      else        r = mid - 1;    }    return mx_median;  }Â
  // Driver functionvar arr = [ 1, 2, 3, 2, 1 ];var N = arr.length;var K = 3;Â
// Function Calldocument.write(maxMedian(arr, N, K));Â
// This code is contributed by shikhasingrajput </script> |
2
Time Complexity: O(N * logN).Â
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



