
Asked in ACKO
Maximum Sum of Disjoint Pairs with Specific Difference
Given an array of integers and a number K
, your task is to form pairs of elements from the array such that the absolute difference between them is strictly less than K
, and the sum of these disjoint pairs is maximized.
Explanation:
The sum of a pair is the sum of its two elements. You need to find the maximum possible sum of all such disjoint pairs that can be formed.
Input:
T
N
ARR[]
K
Output:
Maximum sum of disjoint pairs for each test case in a new line.
Example:
Input:
ARR[] = {3, 5, 10, 15, 17, 12, 9}, K = 4
Output:
62
Explanation:
Disjoint pairs with differences less than K
are (3, 5)
, (10, 12)
, and (15, 17)
. Their maximum sum is 3 + 5 + 10 + 12 + 15 + 17 = 62
.
Constraints:
1 <= T <= 100
1 <= N <= 3000
1 <= ARR[i] <= 1000
1 <= K < 1000
- Time limit: 1 sec
Note:
Multiple ways to form disjoint pairs are possible, but the sum should be maximized.

AnswerBot
4mo
Find maximum sum of disjoint pairs with specific difference in an array.
Sort the array in non-decreasing order.
Iterate through the array and form pairs with absolute difference less than K.
Keep track ...read more
Help your peers!
Add answer anonymously...
Top Software Developer Intern Interview Questions Asked at ACKO
Q. Given the root of a binary tree, traverse the tree using inorder, preorder, and ...read more
Q. How would you design an application like Instagram?
Q. Rat in a Maze Problem Statement Given a maze of size N * N with a rat placed at ...read more
Interview Questions Asked to Software Developer Intern at Other Companies
Top Skill-Based Questions for ACKO Software Developer Intern
C++ Interview Questions and Answers
300 Questions
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Operating Systems Interview Questions and Answers
250 Questions
System Design Interview Questions and Answers
250 Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

