Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Maximum subset sum having difference between its maximum and minimum in range [L, R]
Next article icon

Maximum subset sum having difference between its maximum and minimum in range [L, R]

Last Updated : 15 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] of N positive integers and a range [L, R], the task is to find the maximum subset-sum such that the difference between the maximum and minimum elements of the subset lies in the given range.

Examples:

Input: arr[] = {6, 5, 0, 9, 1}, L = 0, R = 3
Output: 15
Explanation: The subset {6, 9} of the given array has the difference between maximum and minimum elements as 3 which lies in the given range [0, 3] and the sum of the elements as 15 which is the maximum possible.

Input: arr[] = {5, 10, 15, 20, 25}, L = 1, R = 2
Output: 0
Explanation: There exist no valid subsets.

Approach: The given problem can be solved with the help of a Binary Search after sorting the given array. Below are the steps to follow:

  • Sort the given array in non-decreasing order.
  • Create a prefix sum array sum[] using the algorithm discussed here.
  • Traverse the array using a variable i for all values of i in the range [0, N).
  • For each i, find the index j of the largest integer such that arr[j] <= arr[i] + R. This can be done using the upper_bound function.
  • Maintain a variable ans, which stores the maximum subset-sum. Initially, ans = 0.
  • If the subarray arr[i, j] forms a valid subset, update the value of ans to the max of ans and sum of the subarray arr[i, j].
  • The value stored in ans is the required answer.

Below is the implementation of the above approach:

CPP
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;  // Function to find Maximum Subset Sum such // that the difference between maximum and // minimum elements lies in the range [L, R] int maxSubsetSum(vector<int> arr, int L, int R) {     int N = arr.size();      // Sort the given array arr[] in     // non-decreasing order     sort(arr.begin(), arr.end());      // Stores the sum of elements till     // current index of the array arr[]     int sum[N + 1] = {};      // Stores the Maximum subset sum     int ans = 0;      // Create prefix sum array     for (int i = 1; i <= N; i++) {         sum[i] = sum[i - 1] + arr[i - 1];     }      // Iterate over all indices of array     for (int i = 0; i < N; i++) {          // Maximum possible value of         // subset considering arr[i]         // as the minimum element         int val = arr[i] + R;          // Calculate the position of the         // largest element <= val         auto ptr = upper_bound(             arr.begin(), arr.end(), val);         ptr--;         int j = ptr - arr.begin();          // If the difference between the         // maximum and the minimum lies in         // the range, update answer         if ((arr[j] - arr[i] <= R)             && (arr[j] - arr[i] >= L)) {             ans = max(ans, sum[j + 1] - sum[i]);         }     }      // Return Answer     return ans; }  // Driver Code int main() {     vector<int> arr{ 6, 5, 0, 9, 1 };     int L = 0;     int R = 3;     cout << maxSubsetSum(arr, L, R);      return 0; } 
Java
// Java program for the above approach import java.util.*;  class GFG{    static int upperBound(int[] a, int low, int high, int element){     while(low < high){       int middle = low + (high - low)/2;       if(a[middle] > element)         high = middle;       else          low = middle + 1;     }     return low;   }    // Function to find Maximum Subset Sum such   // that the difference between maximum and   // minimum elements lies in the range [L, R]   static int maxSubsetSum(int[] arr, int L, int R)   {     int N = arr.length;      // Sort the given array arr[] in     // non-decreasing order     Arrays.sort(arr);      // Stores the sum of elements till     // current index of the array arr[]     int []sum  = new int[N + 1];      // Stores the Maximum subset sum     int ans = 0;      // Create prefix sum array     for (int i = 1; i <= N; i++) {       sum[i] = sum[i - 1] + arr[i - 1];     }      // Iterate over all indices of array     for (int i = 0; i < N; i++) {        // Maximum possible value of       // subset considering arr[i]       // as the minimum element       int val = arr[i] + R;        // Calculate the position of the       // largest element <= val       int ptr = upperBound(arr, 0, N,val);       ptr--;       int j = ptr;        // If the difference between the       // maximum and the minimum lies in       // the range, update answer       if ((arr[j] - arr[i] <= R)           && (arr[j] - arr[i] >= L)) {         ans = Math.max(ans, sum[j + 1] - sum[i]);       }     }      // Return Answer     return ans;   }    // Driver Code   public static void main(String[] args)   {     int[] arr = { 6, 5, 0, 9, 1 };     int L = 0;     int R = 3;     System.out.print(maxSubsetSum(arr, L, R));    } }  // This code is contributed by umadevi9616  
Python3
# Python Program to implement # the above approach from functools import cmp_to_key  def cmp_sort(a, b):     return a - b   def InsertionIndex(nums, target, left):     low = 0     high = len(nums)      while (low < high):         mid = low + ((high - low) // 2)          if (nums[mid] > target or (left and target == nums[mid])):             high = mid         else:             low = mid + 1      return low  def upper_bound(nums, target):     targetRange = [-1, -1]      leftIdx = InsertionIndex(nums, target, True)      if (leftIdx == len(nums) or nums[leftIdx] != target):         return targetRange      targetRange[0] = leftIdx     targetRange[1] = InsertionIndex(nums, target, False) - 1      return targetRange  def maxSubsetSum(arr, L, R):     N = len(arr)      # Sort the given array arr[] in     # non-decreasing order     arr.sort(key = cmp_to_key(cmp_sort))      # Stores the sum of elements till     # current index of the array arr[]     sum = [0 for i in range(N+1)]      # Stores the Maximum subset sum     ans = 0      # Create prefix sum array     for i in range(1,N+1):         sum[i] = sum[i - 1] + arr[i - 1]     # Iterate over all indices of array     for i in range(N):          # Maximum possible value of         # subset considering arr[i]         # as the minimum element         val = arr[i] + R          # Calculate the position of the         # largest element <= val         ptr = upper_bound(arr, val)         j = ptr[1]          # If the difference between the         # maximum and the minimum lies in         # the range, update answer         if ((arr[j] - arr[i] <= R) and (arr[j] - arr[i] >= L)):             ans = max(ans, sum[j + 1] - sum[i])      # Return Answer     return ans  # Driver Code arr = [6, 5, 0, 9, 1] L = 0 R = 3 print(maxSubsetSum(arr, L, R))  # This code is contributed by shinjanpatra 
C#
// C# program for the above approach using System;  class GFG {   static int upperBound(int[] a, int low, int high,                         int element)   {     while (low < high) {       int middle = low + (high - low) / 2;       if (a[middle] > element)         high = middle;       else         low = middle + 1;     }     return low;   }    // Function to find Maximum Subset Sum such   // that the difference between maximum and   // minimum elements lies in the range [L, R]   static int maxSubsetSum(int[] arr, int L, int R)   {     int N = arr.Length;      // Sort the given array arr[] in     // non-decreasing order     Array.Sort(arr);      // Stores the sum of elements till     // current index of the array arr[]     int[] sum = new int[N + 1];      // Stores the Maximum subset sum     int ans = 0;      // Create prefix sum array     for (int i = 1; i <= N; i++) {       sum[i] = sum[i - 1] + arr[i - 1];     }      // Iterate over all indices of array     for (int i = 0; i < N; i++) {       // Maximum possible value of       // subset considering arr[i]       // as the minimum element       int val = arr[i] + R;        // Calculate the position of the       // largest element <= val       int ptr = upperBound(arr, 0, N, val);       ptr--;       int j = ptr;        // If the difference between the       // maximum and the minimum lies in       // the range, update answer       if ((arr[j] - arr[i] <= R)           && (arr[j] - arr[i] >= L)) {         ans = Math.Max(ans, sum[j + 1] - sum[i]);       }     }      // Return Answer     return ans;   }    // Driver Code   static void Main(string[] args)   {     int[] arr = { 6, 5, 0, 9, 1 };     int L = 0;     int R = 3;     Console.WriteLine(maxSubsetSum(arr, L, R));   } }  // This code is contributed by phasing17 
JavaScript
<script>         // JavaScript Program to implement         // the above approach         function InsertionIndex(nums, target, left)         {             let low = 0,                 high = nums.length              while (low < high) {                 const mid = low + Math.floor((high - low) / 2)                  if (nums[mid] > target || (left && target === nums[mid]))                     high = mid                 else                     low = mid + 1             }              return low         }         function upper_bound(nums, target) {             const targetRange = [-1, -1]              const leftIdx = InsertionIndex(nums, target, true)              if (leftIdx === nums.length || nums[leftIdx] != target)                 return targetRange              targetRange[0] = leftIdx             targetRange[1] = InsertionIndex(nums, target, false) - 1              return targetRange          }         function maxSubsetSum(arr, L, R) {             let N = arr.length;              // Sort the given array arr[] in             // non-decreasing order             arr.sort(function (a, b) { return a - b })              // Stores the sum of elements till             // current index of the array arr[]             let sum = new Array(N + 1).fill(0);              // Stores the Maximum subset sum             let ans = 0;              // Create prefix sum array             for (let i = 1; i <= N; i++) {                 sum[i] = sum[i - 1] + arr[i - 1];             }              // Iterate over all indices of array             for (let i = 0; i < N; i++) {                  // Maximum possible value of                 // subset considering arr[i]                 // as the minimum element                 let val = arr[i] + R;                  // Calculate the position of the                 // largest element <= val                 let ptr = upper_bound(                     arr, val);                 let j = ptr[1]                                  // If the difference between the                 // maximum and the minimum lies in                 // the range, update answer                 if ((arr[j] - arr[i] <= R)                     && (arr[j] - arr[i] >= L)) {                     ans = Math.max(ans, sum[j + 1] - sum[i]);                 }             }              // Return Answer             return ans;         }          // Driver Code         let arr = [6, 5, 0, 9, 1];         let L = 0;         let R = 3;         document.write(maxSubsetSum(arr, L, R));      // This code is contributed by Potta Lokesh     </script> 

 
 


Output: 
15

 


 

Time Complexity: O(N*log N)
Auxiliary Space: O(N) 


 


Next Article
Maximum subset sum having difference between its maximum and minimum in range [L, R]

S

Shivam.Pradhan
Improve
Article Tags :
  • Searching
  • Sorting
  • Mathematical
  • DSA
  • Arrays
  • subset
  • Binary Search
Practice Tags :
  • Arrays
  • Binary Search
  • Mathematical
  • Searching
  • Sorting
  • subset

Similar Reads

    Split array into subarrays such that sum of difference between their maximums and minimums is maximum
    Given an array arr[] consisting of N integers, the task is to split the array into subarrays such that the sum of the difference between the maximum and minimum elements for all the subarrays is maximum. Examples : Input: arr[] = {8, 1, 7, 9, 2}Output: 14Explanation:Consider splitting the given arra
    6 min read
    Minimize difference between maximum and minimum Subarray sum by splitting Array into 4 parts
    Given an array arr[] of size N, the task is to find the minimum difference between the maximum and the minimum subarray sum when the given array is divided into 4 non-empty subarrays. Examples: Input: N = 5, arr[] = {3, 2, 4, 1, 2}Output: 2Explanation: Divide the array into four parts as {3}, {2}, {
    14 min read
    Split array into K Subarrays to minimize sum of difference between min and max
    Given a sorted array arr[] of size N and integer K, the task is to split the array into K non-empty subarrays such that the sum of the difference between the maximum element and the minimum element of each subarray is minimized. Note: Every element of the array must be included in one subarray and e
    6 min read
    Count of subsequences with a sum in range [L, R] and difference between max and min element at least X
    Given an array arr[] consisting of N positive integers and 3 integers L, R, and X, the task is to find the number of subsequences of size atleast 2 with a sum in the range [L, R], and the difference between the maximum and minimum element is at least X. (N≤15) Examples: Input: arr[] = {1 2 3}, L = 5
    9 min read
    Find sum of difference of maximum and minimum over all possible subsets of size K
    Given an array arr[] of N integers and an integer K, the task is to find the sum of the difference between the maximum and minimum elements over all possible subsets of size K. Examples: Input: arr[] = {1, 1, 3, 4}, K = 2Output: 11Explanation:There are 6 subsets of the given array of size K(= 2). Th
    12 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences