Given an array, arr[] of size N and an integer K, the task is to partition the array into the minimum number of subsets such that the maximum pair sum in each subset is less than or equal to K.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}, K = 5
Output: 3
Explanation:
Subset having maximum pair sum less than or equal to K(= 5) are: {{2, 3}, {1, 4}, {5}}.
Therefore, the required output is 3.Input: arr[] = {2, 6, 8, 10, 20, 25}, K = 26
Output: 3
Explanation:
Subset having maximum pair sum less than or equal to K(=26) are: {{2, 6, 8, 10}, {20}, {25}}.
Therefore, the required output is 3.
Approach: The problem can be solved using two pointer technique. The idea is to partition the array such that the maximum pair sum of each subset is minimized. Follow the steps below to solve the problem:
- Sort the given array.
- Initialize a variable say, res to store the minimum number of subsets that satisfy the given condition.
- Initialize two variables, say start and end to store the start and end index of the sorted array respectively.
- Traverse the sorted array and check if arr[start] + arr[end] ? K or not. If found to be true, then increment the value of start by 1.
- Otherwise, decrement the value of end by 1 and increment the res by 1.
- Finally, print the value of res.
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to get the minimum// count of subsets that satisfy// the given conditionint cntMinSub(int arr[],              int N, int K){    // Store the minimum count    // of subsets that satisfy    // the given condition    int res = 0;Â
    // Stores start index    // of the sorted array.    int start = 0;Â
    // Stores end index    // of the sorted array    int end = N - 1;Â
    // Sort the given array    sort(arr, arr + N);Â
    // Traverse the array    while (end - start > 1) {        if (arr[start] + arr[end]            <= K) {            start++;        }        else {            res++;            end--;        }    }Â
    // If only two elements    // of sorted array left    if (end - start == 1) {        if (arr[start] + arr[end]            <= K) {            res++;            start++;            end--;        }        else {            res++;            end--;        }    }Â
    // If only one elements    // left in the array    if (start == end) {        res++;    }Â
    return res;}Â
// Driver Codeint main(){Â Â Â Â int arr[] = { 2, 6, 8, 10, 20, 25 };Â Â Â Â int N = sizeof(arr) / sizeof(arr[0]);Â Â Â Â int K = 26;Â Â Â Â cout << cntMinSub(arr, N, K);} |
Java
// Java program to implement// the above approachimport java.util.*;class GFG{Â
// Function to get the minimum// count of subsets that satisfy// the given conditionstatic int cntMinSub(int arr[],                     int N, int K){  // Store the minimum count  // of subsets that satisfy  // the given condition  int res = 0;Â
  // Stores start index  // of the sorted array.  int start = 0;Â
  // Stores end index  // of the sorted array  int end = N - 1;Â
  // Sort the given array  Arrays.sort(arr);Â
  // Traverse the array  while (end - start > 1)   {    if (arr[start] +         arr[end] <= K)     {      start++;    }    else    {      res++;      end--;    }  }Â
  // If only two elements  // of sorted array left  if (end - start == 1)   {    if (arr[start] +         arr[end] <= K)     {      res++;      start++;      end--;    }    else    {      res++;      end--;    }  }Â
  // If only one elements  // left in the array  if (start == end)   {    res++;  }Â
  return res;}Â
// Driver Codepublic static void main(String[] args){Â Â int arr[] = {2, 6, 8, 10, 20, 25};Â Â int N = arr.length;Â Â int K = 26;Â Â System.out.print(cntMinSub(arr, N, K));}}Â
// This code is contributed by shikhasingrajput |
Python3
# Python3 program to implement# the above approachÂ
# Function to get the minimum# count of subsets that satisfy# the given conditiondef cntMinSub(arr, N, K):         # Store the minimum count    # of subsets that satisfy    # the given condition    res = 0Â
    # Stores start index    # of the sorted array.    start = 0Â
    # Stores end index    # of the sorted array    end = N - 1Â
    # Sort the given array    arr = sorted(arr)Â
    # Traverse the array    while (end - start > 1):        if (arr[start] + arr[end] <= K):            start += 1        else:            res += 1            end -= 1Â
    # If only two elements    # of sorted array left    if (end - start == 1):        if (arr[start] + arr[end] <= K):            res += 1            start += 1            end -= 1        else:            res += 1            end -= 1                 # If only one elements    # left in the array    if (start == end):        res += 1Â
    return resÂ
# Driver Codeif __name__ == '__main__':Â
    arr = [ 2, 6, 8, 10, 20, 25 ]    N = len(arr)    K = 26         print(cntMinSub(arr, N, K))Â
# This code is contributed by mohit kumar 29 |
C#
// C# program to implement// the above approachusing System;class GFG{Â
// Function to get the minimum// count of subsets that satisfy// the given conditionstatic int cntMinSub(int []arr,                     int N, int K){  // Store the minimum count  // of subsets that satisfy  // the given condition  int res = 0;Â
  // Stores start index  // of the sorted array.  int start = 0;Â
  // Stores end index  // of the sorted array  int end = N - 1;Â
  // Sort the given array  Array.Sort(arr);Â
  // Traverse the array  while (end - start > 1)   {    if (arr[start] +         arr[end] <= K)     {      start++;    }    else    {      res++;      end--;    }  }Â
  // If only two elements  // of sorted array left  if (end - start == 1)   {    if (arr[start] +         arr[end] <= K)     {      res++;      start++;      end--;    }    else    {      res++;      end--;    }  }Â
  // If only one elements  // left in the array  if (start == end)   {    res++;  }Â
  return res;}Â
// Driver Codepublic static void Main(String[] args){Â Â int []arr = {2, 6, 8, 10, 20, 25};Â Â int N = arr.Length;Â Â int K = 26;Â Â Console.Write(cntMinSub(arr, N, K));}}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>Â
// Javascript program to implement// the above approachÂ
// Function to get the minimum// count of subsets that satisfy// the given conditionfunction cntMinSub(arr, N, K){         // Store the minimum count    // of subsets that satisfy    // the given condition    var res = 0;Â
    // Stores start index    // of the sorted array.    var start = 0;Â
    // Stores end index    // of the sorted array    var end = N - 1;Â
    // Sort the given array    arr.sort();Â
    // Traverse the array    while (end - start > 1)    {        if (arr[start] + arr[end] <= K)         {            start++;        }         else        {            res++;            end--;        }    }Â
    // If only two elements    // of sorted array left    if (end - start == 1)    {        if (arr[start] + arr[end] <= K)        {            res++;            start++;            end--;        }         else        {            res++;            end--;        }    }Â
    // If only one elements    // left in the array    if (start == end)     {        res++;    }Â
    return res;}Â
// Driver Codevar arr = [ 2, 6, 8, 10, 20, 25 ];var N = arr.length;var K = 26;Â
document.write(cntMinSub(arr, N, K));Â
// This code is contributed by gauravrajput1Â
</script> |
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
