Rearrange array such that sum of same indexed elements is atmost K
Given two arrays A[] and B[] consisting of N integers each and an integer K, the task is to rearrange the array B[] such that sum of Ai + Bi is atmost K. If no such arrangement is possible, print -1.
Examples:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input: A[] = {1, 2, 3, 4, 2}, B[] = {1, 2, 3, 1, 1}, K = 5
Output: 1 3 1 1 2Input: A[] = {1, 2, 3, 4, 5}, B[] = {2, 3, 4, 5, 6}, K = 6
Output: -1
Approach: The most optimal rearrangement of the array B[] to satisfy the given conditions is to sort the array in descending order.
Proof:
- Since the sum of every pair of ith indexed elements can be atmost K. Therefore, mn + num ≤ X and mx + num1 ≤ X, where mn and mx are the minimum elements in the array A[] and B[] respectively.
- Therefore, by induction, it can be proved that mn and mx can be paired.
- Therefore, sorting the array in descending order is the most optimal rearrangement
Follow the steps below to solve the problem:
- Sort the array B[] in descending order.
- Traverse the array and check if Ai + Bi is less than or equal to K for every ith index.
- If the condition fails for any pair, print -1.
- Otherwise, print the array B[]
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to rearrange array such// that sum of similar indexed elements// does not exceed Kvoid rearrangeArray(int A[], int B[], int N, int K){ // Sort the array B[] // in descending order sort(B, B + N, greater<int>()); bool flag = true; for (int i = 0; i < N; i++) { // If condition fails if (A[i] + B[i] > K) { flag = false; break; } } if (!flag) { cout << "-1" << endl; } else { // Print the array for (int i = 0; i < N; i++) { cout << B[i] << " "; } }}// Driver Codeint main(){ // Given arrays int A[] = { 1, 2, 3, 4, 2 }; int B[] = { 1, 2, 3, 1, 1 }; int N = sizeof(A) / sizeof(A[0]); int K = 5; rearrangeArray(A, B, N, K); return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{// Reverse arraystatic int[] reverse(int a[]){ int i, n = a.length, t; for(i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } return a;} // Function to rearrange array such// that sum of similar indexed elements// does not exceed Kstatic void rearrangeArray(int A[], int B[], int N, int K){ // Sort the array B[] // in descending order Arrays.sort(B); B = reverse(B); boolean flag = true; for(int i = 0; i < N; i++) { // If condition fails if (A[i] + B[i] > K) { flag = false; break; } } if (!flag) { System.out.print("-1" + "\n"); } else { // Print the array for(int i = 0; i < N; i++) { System.out.print(B[i] + " "); } }}// Driver Codepublic static void main(String[] args){ // Given arrays int A[] = { 1, 2, 3, 4, 2 }; int B[] = { 1, 2, 3, 1, 1 }; int N = A.length; int K = 5; rearrangeArray(A, B, N, K);}}// This code is contributed by Amit Katiyar |
Python3
# Python3 program for the above approach# Function to rearrange array such# that sum of similar indexed elements# does not exceed Kdef rearrangeArray(A, B, N, K): # Sort the array B[] # in descending order B.sort(reverse = True) flag = True for i in range(N): # If condition fails if (A[i] + B[i] > K): flag = False break if (flag == False): print("-1") else: # Print the array for i in range(N): print(B[i], end = " ")# Driver Codeif __name__ == '__main__': # Given arrays A = [ 1, 2, 3, 4, 2 ] B = [ 1, 2, 3, 1, 1 ] N = len(A) K = 5; rearrangeArray(A, B, N, K)# This code is contributed by SURENDRA_GANGWAR |
C#
// C# program for the// above approachusing System;class GFG{// Reverse arraystatic int[] reverse(int []a){ int i, n = a.Length, t; for(i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } return a;} // Function to rearrange array such// that sum of similar indexed elements// does not exceed Kstatic void rearrangeArray(int []A, int []B, int N, int K){ // Sort the array []B // in descending order Array.Sort(B); B = reverse(B); bool flag = true; for(int i = 0; i < N; i++) { // If condition fails if (A[i] + B[i] > K) { flag = false; break; } } if (!flag) { Console.Write("-1" + "\n"); } else { // Print the array for(int i = 0; i < N; i++) { Console.Write(B[i] + " "); } }}// Driver Codepublic static void Main(String[] args){ // Given arrays int []A = {1, 2, 3, 4, 2}; int []B = {1, 2, 3, 1, 1}; int N = A.Length; int K = 5; rearrangeArray(A, B, N, K);}}// This code is contributed by Rajput-Ji |
Javascript
<script>// JavaScript program for the above approach// Reverse arrayfunction reverse(a){ let i, n = a.length, t; for(i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } return a;} // Function to rearrange array such// that sum of similar indexed elements// does not exceed Kfunction rearrangeArray(A, B, N, K){ // Sort the array B[] // in descending order B.sort(); B = reverse(B); let flag = true; for(let i = 0; i < N; i++) { // If condition fails if (A[i] + B[i] > K) { flag = false; break; } } if (!flag) { document.write("-1" + "<br/>"); } else { // Prlet the array for(let i = 0; i < N; i++) { document.write(B[i] + " "); } }}// Driver Code// Given arrayslet A = [ 1, 2, 3, 4, 2 ];let B = [ 1, 2, 3, 1, 1 ];let N = A.length;let K = 5;rearrangeArray(A, B, N, K);// This code is contribute by target_2</script> |
3 2 1 1 1
Time Complexity: O(N)
Auxiliary Space Complexity: O(1)

