Open In App

Minimize the maximum element of N subarrays of size K

Last Updated : 18 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] and two integers N and K, the task is to choose non-overlapping N subarrays of size K such that the maximum element of all subarrays is minimum.
Note: If it is not possible to choose N such subarrays then return -1. 
Examples: 

Input: arr[] = {1, 10, 3, 10, 2}, N = 3, K = 1 
Output:
Explanation: 
The three non-overlapping subarrays are – 
Subarrays => {{1}, {2}, {3}} 
Maximum of these subarrays are => 3 
Input: arr[] = {7, 7, 7, 7, 12, 7, 7}, N = 2, K = 3 
Output: 12 
Explanation: 
The two non-overlapping subarrays are – 
Subarrays => {{7, 7, 7}, {7, 12, 7}} 
Maximum element of these subarrays are => 12 

Approach: The idea is to use a binary search. Below is the illustration of the binary search:

  • Search space: As we have to find the maximum element from the N subarrays which is one of the elements from the array. Therefore, the search space will be the minimum element to the maximum element of the array.
  • Function for binary search: The function for binary search is to find the count of K-sized array possible in with all the elements less than the given number which will be the middle of the search space.
  • Left Search Space: The condition when the count of K-sized subarrays possible is greater than or equal to N, Then the answer possible can lie in the left search space.
  • Right Search Space: The condition when the count of K-sized subarrays possible is less than N, Then the answer possible scan lies in the right search space.

Below is the implementation of the above approach:

C++




// C++ implementation to choose
// N subarrays of size K such that
// the maximum element of
// subarrays is minimum
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to choose
// N subarrays of size K such that
// the maximum element of
// subarrays is minimum
int minDays(vector<int>& arr,
                int n, int k)
{
    int l = arr.size(),
left = 1, right = 1e9;
 
    // Condition to check if it
    // is not possible to choose k
    // sized N subarrays
    if (n * k > l)
        return -1;
 
    // Using binary search
    while (left < right) {
 
        // calculating mid
        int mid = (left + right) / 2,
                 cnt = 0, product = 0;
                  
        // Loop to find the count of the
        // K sized subarrays possible with
        // elements less than  mid
        for (int j = 0; j < l; ++j) {
            if (arr[j] > mid) {
                cnt = 0;
            }
            else if (++cnt >= k) {
                product++;
                cnt = 0;
            }
        }
     
        // Condition to check if the
        // answer is in right subarray
        if (product < n) {
            left = mid + 1;
        }
        else {
            right = mid;
        }
    }
    return left;
}
 
// Driver Code
int main()
{
    vector<int> arr{ 1, 10, 3, 10, 2 };
    int n = 3, k = 1;
     
    // Function Call
    cout << minDays(arr, n, k) << endl;
    return 0;
}


Java




// Java implementation to choose
// N subarrays of size K such that
// the maximum element of
// subarrays is minimum
class GFG{
 
// Function to choose
// N subarrays of size K such that
// the maximum element of
// subarrays is minimum
static int minDays(int []arr,
                   int n, int k)
{
    int l = arr.length,
        left = 1, right = (int) 1e9;
 
    // Condition to check if it
    // is not possible to choose k
    // sized N subarrays
    if (n * k > l)
        return -1;
 
    // Using binary search
    while (left < right)
    {
 
        // calculating mid
        int mid = (left + right) / 2,
                   cnt = 0, product = 0;
                 
        // Loop to find the count of the
        // K sized subarrays possible with
        // elements less than mid
        for (int j = 0; j < l; ++j)
        {
            if (arr[j] > mid)
            {
                cnt = 0;
            }
            else if (++cnt >= k)
            {
                product++;
                cnt = 0;
            }
        }
     
        // Condition to check if the
        // answer is in right subarray
        if (product < n)
        {
            left = mid + 1;
        }
        else
        {
            right = mid;
        }
    }
    return left;
}
 
// Driver Code
public static void main(String[] args)
{
    int []arr = {1, 10, 3, 10, 2};
    int n = 3, k = 1;
     
    // Function Call
    System.out.print(minDays(arr, n, k) + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Python3




# Python3 implementation to choose
# N subarrays of size K such that
# the maximum element of
# subarrays is minimum
  
# Function to choose
# N subarrays of size K such that
# the maximum element of
# subarrays is minimum
def minDays(arr, n, k):
 
    l = len(arr)
    left = 1
    right = 1e9
  
    # Condition to check if it
    # is not possible to choose k
    # sized N subarrays
    if (n * k > l):
        return -1
  
    # Using binary search
    while (left < right):
  
        # calculating mid
        mid = (left + right) // 2
        cnt = 0
        product = 0
                   
        # Loop to find the count of the
        # K sized subarrays possible with
        # elements less than  mid
        for j in range (l):
            if (arr[j] > mid):
                cnt = 0
            else:
                cnt += 1
                if (cnt >= k):
                    product += 1
                    cnt = 0
      
        # Condition to check if the
        # answer is in right subarray
        if (product < n):
            left = mid + 1
        else:
            right = mid
      
    return left
  
# Driver Code
if __name__ == "__main__":
    arr = [1, 10, 3, 10, 2]
    n = 3
    k = 1
      
    # Function Call
    print (int(minDays(arr, n, k)))
     
# This code is contributed by Chitranayal


C#




// C# implementation to choose N 
// subarrays of size K such that
// the maximum element of
// subarrays is minimum
using System;
class GFG{
 
// Function to choose N subarrays 
// of size K such that the maximum
// element of subarrays is minimum
static int minDays(int []arr,
                   int n, int k)
{
    int l = arr.Length;
    int left = 1, right = (int)1e9;
 
    // Condition to check if it
    // is not possible to choose k
    // sized N subarrays
    if (n * k > l)
        return -1;
 
    // Using binary search
    while (left < right)
    {
 
        // Calculating mid
        int mid = (left + right) / 2,
                   cnt = 0, product = 0;
                 
        // Loop to find the count of the
        // K sized subarrays possible with
        // elements less than mid
        for(int j = 0; j < l; ++j)
        {
           if (arr[j] > mid)
           {
               cnt = 0;
           }
           else if (++cnt >= k)
           {
               product++;
               cnt = 0;
           }
        }
     
        // Condition to check if the
        // answer is in right subarray
        if (product < n)
        {
            left = mid + 1;
        }
        else
        {
            right = mid;
        }
    }
    return left;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 10, 3, 10, 2 };
    int n = 3, k = 1;
     
    // Function Call
    Console.Write(minDays(arr, n, k) + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Javascript




<script>
// Javascript implementation to choose
// N subarrays of size K such that
// the maximum element of
// subarrays is minimum
 
// Function to choose
// N subarrays of size K such that
// the maximum element of
// subarrays is minimum
function minDays(arr, n, k)
{
    let l = arr.length,
left = 1, right = 1000000000;
 
    // Condition to check if it
    // is not possible to choose k
    // sized N subarrays
    if (n * k > l)
        return -1;
 
    // Using binary search
    while (left < right) {
 
        // calculating mid
        let mid = parseInt((left + right) / 2),
                 cnt = 0, product = 0;
                  
        // Loop to find the count of the
        // K sized subarrays possible with
        // elements less than  mid
        for (let j = 0; j < l; ++j) {
            if (arr[j] > mid) {
                cnt = 0;
            }
            else if (++cnt >= k) {
                product++;
                cnt = 0;
            }
        }
     
        // Condition to check if the
        // answer is in right subarray
        if (product < n) {
            left = mid + 1;
        }
        else {
            right = mid;
        }
    }
    return left;
}
 
// Driver Code
    let arr = [ 1, 10, 3, 10, 2 ];
    let n = 3, k = 1;
     
    // Function Call
    document.write(minDays(arr, n, k));
 
</script>


Output: 

3

 

Time Complexity: O(N*logN)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads