Pairs with specific difference | Practice | GeeksforGeeks

Given an array of integers, arr[] and a number, K.You can pair two numbers of the array if the difference between them is strictly less than K. The task is to find the maximum possible sum of such disjoint pairs (i.e., each element of the array can be used at most once). The Sum of P pairs is the sum of all 2P elements of pairs.
 
Example 1:
Input  : 
arr[] = {3, 5, 10, 15, 17, 12, 9}
K = 4
Output : 
62
Explanation :
Then disjoint pairs with difference less
than K are, (3, 5), (10, 12), (15, 17)
max sum which we can get is 
3 + 5 + 10 + 12 + 15 + 17 = 62
Note that an alternate way to form 
disjoint pairs is,(3, 5), (9, 12), (15, 17)
but this pairing produces less sum.
 
Example 2:
Input  : 
arr[] = {5, 15, 10, 300}
K = 12
Output : 
25

 
Your Task:  
You don't need to read, input, or print anything. Your task is to complete the function maxSumPairWithDifferenceLessThanK() which takes the array arr[], its size N, and an integer K as inputs and returns the maximum possible sum of disjoint pairs.
 
Expected Time Complexity: O(N. log(N))
Expected Auxiliary Space: O(N)
 
Constraints:
1 ≤ N ≤ 105
0 ≤ K ≤ 105
1 ≤ arr[i] ≤ 104
0
2021aspire041 day ago
class Solution{   int maxSumPairWithDifferenceLessThanK(int arr[], int N, int K) {      Arrays.sort(arr);      int i=N-1;      int sum=0;      while(i>=1)      {          if((arr[i]-arr[i-1])<K)          {              sum=sum+arr[i]+arr[i-1];              i=i-2;          }          else          i--;      }      return sum;   }};
0
milindprajapatmst192 weeks ago
class Solution{
public:
    int maxSumPairWithDifferenceLessThanK(int arr[], int n, int k) {
        sort(arr, arr + n, [](int& x, int & y) { return x > y; });
        int _sum = 0, index = 1;
        while (index < n) {
            if (arr[index - 1] - arr[index] < k) {
                _sum += arr[index - 1] + arr[index];
                index++;
            }
            index++;
        }
        return _sum;
    }
};
0
hsnagb201 month ago
Solution with Space Complexity=O(1) and Time Complexity=O(N log N)
class Solution{   public:   int maxSumPairWithDifferenceLessThanK(int arr[], int n, int k){       sort(arr,arr+n);       int sum=0;       for(int i=n-1;i>0;i--){           if(abs(arr[i]-arr[i-1])<k){               sum=sum+arr[i]+arr[i-1];               i--;           }       }       return sum;   }};
+1
bhargav_412 months ago
Java Solution 
 
class Solution { 
    public static void swap(int[] arr,int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    public static void reverse(int[] arr){
        int i=0,j=arr.length-1;
        while(i<j){
            swap(arr,i,j);
            i++;
            j--;
        }
    }
    public static int maxSumPairWithDifferenceLessThanK(int arr[], int N, int K) 
    {
       Arrays.sort(arr);
        reverse(arr);
       int sum = 0;
       int i=0;
       while(i<N){
           if(i==N-1) break;
           int pairDifference = arr[i]-arr[i+1];
           if(pairDifference<K){
               sum += arr[i] + arr[i+1];
               i+=2;
           }else i++;
       }
       return  sum;
    }
    
}
0
uttarandas5012 months ago
int maxSumPairWithDifferenceLessThanK(int arr[], int N, int K)
    {
        // Your code goes here   
        sort(arr, arr+N, greater<int>());
        int i, sum=0;
        
        for(i=0; i<N-1; i++){
            if(arr[i]-arr[i+1] < K){sum += (arr[i]+arr[i+1]); i++;}
        }
        return sum;
    }
0
ritikkhandelwal083 months ago
Java Solution!!!
Execution time: 0.1 / 2.5
 
class Solution { 
    public static int maxSumPairWithDifferenceLessThanK(int arr[], int N, int K){
        Arrays.sort(arr);
        int sum=0;
        for(int i=N-1; i>0;){
            if(arr[i]-arr[i-1] < K){
                sum+=arr[i]+arr[i-1];
                i=i-2;
            }
            else{
                i--;
            }
        }
        return sum;
    }
}
+1
decostarsharma1133 months ago
WORKING SOLUTION WITH O(1) SPACE COMPLEXITY 
class Solution {    public static int maxSumPairWithDifferenceLessThanK(int arr[], int N, int K)    {       // Your code goes here        Arrays.sort(arr);       int i = N - 1 ;       int sum = 0 ;       while( i >= 1)       {           if( arr[i] - arr[i-1] < K )            {               sum = sum + arr[i] + arr[i-1];               i = i - 2 ;           }else           {               i--;           }       }       return sum ;          }   }
0
ilihaspatel443 months ago
int maxSumPairWithDifferenceLessThanK(int arr[], int N, int k)   {              sort(arr,arr+N);       int sum=0;              for(int j=N-1;j>0;){           if(arr[j]-arr[j-1]<k ){               sum+=arr[j-1]+arr[j];               // cout<<arr[j]<<" "<<arr[j-1]<<"|| ";               j=j-2;           }           else{               j--;           }       }       return sum;                               }
0
swarnavo094 months ago
C++ Code
int maxSumPairWithDifferenceLessThanK(int arr[], int N, int K)   {       sort(arr, arr+N);       int sum = 0;       int i = N-1;       while(i>0){           if((arr[i]-arr[i-1])<K)           {               sum += arr[i] + arr[i-1];               i = i-2;           }           else               i--;       }       return sum;       // Your code goes here      }
0
ankitkushawaha10004 months ago
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.