# Rearrange two given arrays such that sum of same indexed elements lies within given range

Given two arrays arr1[] and arr2[] consisting of N positive integers and an even integer K, the task is to check if the sum of the same indexed elements in the two arrays lie in the range [K/2, K] after rearranging the given arrays or not. If it is possible to obtain such an arrangement, then print “Yes”. Otherwise, print “No”.

Examples:

Input: arr1[] = {1, 4, 3, 5}, arr2[] = {0, 2, 1, 1}, K = 6
Output: Yes
Explanation: Rearranging arr1[] to {1, 4, 3, 5} and arr2[] to {2, 0, 1, 1} ensures that the sum of same indexed elements lie in the range [3, 6]. Therefore, print “Yes”.

Input: arr1[] = {2, 0}, arr2[] = {3, 4}, K = 2
Output: No
Explanation: No such arrangement is possible

Naive Approach: The simplest approach to generate all possible permutations of the given arrays and check if any possible arrangements satisfy the given conditions or not. If found to be true, then print “Yes”. Otherwise, print “No”

Time Complexity: O((N!)2)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to check if there exists any``// arrangements of the arrays such that``// sum of element lie in the range [K/2, K]``void` `checkArrangement(``int` `A1[], ``int` `A2[],``                      ``int` `n, ``int` `k)``{``    ``// Sort the array arr1[] in``    ``// increasing order``    ``sort(A1, A1 + n);` `    ``// Sort the array arr2[] in``    ``// decreasing order``    ``sort(A2, A2 + n, greater<``int``>());` `    ``int` `flag = 0;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// If condition is not satisfied``        ``// break the loop``        ``if` `((A1[i] + A2[i] > k)``            ``|| (A1[i] + A2[i] < k / 2)) {` `            ``flag = 1;``            ``break``;``        ``}``    ``}` `    ``// Print the result``    ``if` `(flag == 1)``        ``cout << ``"No"``;``    ``else``        ``cout << ``"Yes"``;``}` `// Driver Code``int` `main()``{``    ``int` `arr1[] = { 1, 3, 4, 5 };``    ``int` `arr2[] = { 2, 0, 1, 1 };` `    ``int` `K = 6;` `    ``int` `N = ``sizeof``(arr1)` `            ``/ ``sizeof``(arr1[0]);` `    ``checkArrangement(arr1, arr2, N, K);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.util.*;` `class` `GFG{` `// Function to check if there exists any``// arrangements of the arrays such that``// sum of element lie in the range [K/2, K]``static` `void` `checkArrangement(Integer[] A1, ``                             ``Integer[] A2,``                             ``int` `n, ``int` `k)``{``    ` `    ``// Sort the array arr1[] in``    ``// increasing order``    ``Arrays.sort(A1);` `    ``// Sort the array arr2[] in``    ``// decreasing order``    ``Arrays.sort(A2, Collections.reverseOrder());` `    ``int` `flag = ``0``;` `    ``// Traverse the array``    ``for``(``int` `i = ``0``; i < n; i++) ``    ``{``        ` `        ``// If condition is not satisfied``        ``// break the loop``        ``if` `((A1[i] + A2[i] > k) || ``            ``(A1[i] + A2[i] < k / ``2``)) ``        ``{``            ``flag = ``1``;``            ``break``;``        ``}``    ``}` `    ``// Print the result``    ``if` `(flag == ``1``)``        ``System.out.println(``"No"``);``    ``else``        ``System.out.println(``"Yes"``);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``Integer[] arr1 = { ``1``, ``3``, ``4``, ``5` `};``    ``Integer[] arr2 = { ``2``, ``0``, ``1``, ``1` `};` `    ``int` `K = ``6``;` `    ``int` `N = arr1.length;` `    ``checkArrangement(arr1, arr2, N, K);``}``}` `// This code is contributed by akhilsaini`

## Python3

 `# Python3 program for the above approach` `# Function to check if there exists any``# arrangements of the arrays such that``# sum of element lie in the range [K/2, K]``def` `checkArrangement(A1, A2, n, k):``    ` `    ``# Sort the array arr1[] in``    ``# increasing order``    ``A1 ``=` `sorted``(A1)` `    ``# Sort the array arr2[] in``    ``# decreasing order``    ``A2 ``=` `sorted``(A2)` `    ``A2 ``=` `A2[::``-``1``]` `    ``flag ``=` `0` `    ``# Traverse the array``    ``for` `i ``in` `range``(n):` `        ``# If condition is not satisfied``        ``# break the loop``        ``if` `((A1[i] ``+` `A2[i] > k) ``or``            ``(A1[i] ``+` `A2[i] < k ``/``/` `2``)):``            ``flag ``=` `1``            ``break` `    ``# Print the result``    ``if` `(flag ``=``=` `1``):``        ``print``(``"No"``)``    ``else``:``        ``print``(``"Yes"``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``arr1 ``=` `[ ``1``, ``3``, ``4``, ``5` `]``    ``arr2 ``=` `[ ``2``, ``0``, ``1``, ``1` `]` `    ``K ``=` `6` `    ``N ``=` `len``(arr1)` `    ``checkArrangement(arr1, arr2, N, K)` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections;` `class` `GFG{` `// Function to check if there exists any``// arrangements of the arrays such that``// sum of element lie in the range [K/2, K]``static` `void` `checkArrangement(``int``[] A1, ``int``[] A2,``                             ``int` `n, ``int` `k)``{``    ` `    ``// Sort the array arr1[] in``    ``// increasing order``    ``Array.Sort(A1);` `    ``// Sort the array arr2[] in``    ``// decreasing order``    ``Array.Sort(A2);``    ``Array.Reverse(A2);` `    ``int` `flag = 0;` `    ``// Traverse the array``    ``for``(``int` `i = 0; i < n; i++) ``    ``{``        ` `        ``// If condition is not satisfied``        ``// break the loop``        ``if` `((A1[i] + A2[i] > k) ||``            ``(A1[i] + A2[i] < k / 2))``        ``{``            ``flag = 1;``            ``break``;``        ``}``    ``}` `    ``// Print the result``    ``if` `(flag == 1)``        ``Console.WriteLine(``"No"``);``    ``else``        ``Console.WriteLine(``"Yes"``);``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int``[] arr1 = { 1, 3, 4, 5 };``    ``int``[] arr2 = { 2, 0, 1, 1 };` `    ``int` `K = 6;` `    ``int` `N = arr1.Length;` `    ``checkArrangement(arr1, arr2, N, K);``}``}` `// This code is contributed by akhilsaini`

## Javascript

 ``

Output:
`Yes`

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

Previous
Next