Given two arrays A[] and B[] of size N, the task is to find the maximum possible sum of abs(A[i] – B[i]) by rearranging the array elements.
Examples:
Input: A[] = {1, 2, 3, 4, 5}, B[] = {1, 2, 3, 4, 5}
Output: 12
Explanation:
One of the possible rearrangements of A[] is {5, 4, 3, 2, 1}.
One of the possible rearrangements of B[] is {1, 2, 3, 4, 4}.
Therefore, the sum of all possible values of abs(A[i] – B[i]) = { abs(5 – 1) + abs(4 – 2) + abs(3 – 3) + abs(2 – 4) + abs(1 – 5) } = 12Input: A[] = {1, 2, 2, 4, 5}, B[] = {5, 5, 5, 6, 6}
Output: 13
Explanation:
One of the possible rearrangements of A[] is {5, 4, 2, 2, 1}.
One of the possible rearrangements of B[] is {5, 5, 5, 6, 6}.
Therefore, the sum of all possible values of abs(A[i] – B[i]) = { abs(5 – 5) + abs(4 – 5) + abs(2 – 5) + abs(2 – 6) + abs(1 – 6) } = 13
Approach: Follow the steps below to solve the problem:
- Sort the array A[] in ascending order.
- Sort the array B[] in descending order.
- Traverse both the arrays and print the sum of all possible value of abs(A[i] – B[i]).
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 find the maximum possible sum // by rearranging the given array elements int MaxRearrngeSum(int A[], int B[], int N) { // Sort the array A[] // in ascending order sort(A, A + N); // Sort the array B[] // in descending order sort(B, B + N, greater<int>()); // Stores maximum possible sum // by rearranging array elements int maxSum = 0; // Traverse both the arrays for (int i = 0; i < N; i++) { // Update maxSum maxSum += abs(A[i] - B[i]); } return maxSum; } // Driver Code int main() { int A[] = { 1, 2, 2, 4, 5 }; int B[] = { 5, 5, 5, 6, 6 }; int N = sizeof(A) / sizeof(A[0]); cout<< MaxRearrngeSum(A, B, N); return 0; } |
13
Time Complexity: O(N * Log N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Rearrange array to make Bitwise XOR of similar indexed elements of two arrays is same
- Maximize product of same-indexed elements of same size subsequences
- Rearrange array such that sum of same indexed elements is atmost K
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Reverse a subarray to maximize sum of even-indexed elements of given array
- Rearrange array such that all even-indexed elements in the Array is even
- Rearrange an array to make similar indexed elements different from that of another array
- Modify given array to make sum of odd and even indexed elements same
- Minimize the Sum of all the subarrays made up of the products of same-indexed elements
- Rearrange an array such that every odd indexed element is greater than it previous
- Maximize count of corresponding same elements in given Arrays by Rotation
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Maximize array sum by concatenating corresponding elements of given two arrays
- Minimum sum of two elements from two arrays such that indexes are not same
- Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array
- Rearrange the array to maximize the number of primes in prefix sum of the array
- Rearrange given binary strings to maximize their Bitwise XOR value
- Rearrange characters in a string such that no two adjacent are same
- Rearrange numbers in an array such that no two adjacent numbers are same
- Maximum sum of even indexed elements obtained by right shift on an even sized subarray
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

