Rearrange two given arrays such that sum of same indexed elements lies within given range
Last Updated :
08 Feb, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void checkArrangement( int A1[], int A2[],
int n, int k)
{
sort(A1, A1 + n);
sort(A2, A2 + n, greater< int >());
int flag = 0;
for ( int i = 0; i < n; i++) {
if ((A1[i] + A2[i] > k)
|| (A1[i] + A2[i] < k / 2)) {
flag = 1;
break ;
}
}
if (flag == 1)
cout << "No" ;
else
cout << "Yes" ;
}
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
import java.io.*;
import java.util.*;
class GFG{
static void checkArrangement(Integer[] A1,
Integer[] A2,
int n, int k)
{
Arrays.sort(A1);
Arrays.sort(A2, Collections.reverseOrder());
int flag = 0 ;
for ( int i = 0 ; i < n; i++)
{
if ((A1[i] + A2[i] > k) ||
(A1[i] + A2[i] < k / 2 ))
{
flag = 1 ;
break ;
}
}
if (flag == 1 )
System.out.println( "No" );
else
System.out.println( "Yes" );
}
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);
}
}
|
Python3
def checkArrangement(A1, A2, n, k):
A1 = sorted (A1)
A2 = sorted (A2)
A2 = A2[:: - 1 ]
flag = 0
for i in range (n):
if ((A1[i] + A2[i] > k) or
(A1[i] + A2[i] < k / / 2 )):
flag = 1
break
if (flag = = 1 ):
print ( "No" )
else :
print ( "Yes" )
if __name__ = = '__main__' :
arr1 = [ 1 , 3 , 4 , 5 ]
arr2 = [ 2 , 0 , 1 , 1 ]
K = 6
N = len (arr1)
checkArrangement(arr1, arr2, N, K)
|
C#
using System;
using System.Collections;
class GFG{
static void checkArrangement( int [] A1, int [] A2,
int n, int k)
{
Array.Sort(A1);
Array.Sort(A2);
Array.Reverse(A2);
int flag = 0;
for ( int i = 0; i < n; i++)
{
if ((A1[i] + A2[i] > k) ||
(A1[i] + A2[i] < k / 2))
{
flag = 1;
break ;
}
}
if (flag == 1)
Console.WriteLine( "No" );
else
Console.WriteLine( "Yes" );
}
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);
}
}
|
Javascript
<script>
function checkArrangement( A1, A2, n, k)
{
A1.sort();
A2.sort();
A2.reverse();
let flag = 0;
for (let i = 0; i < n; i++)
{
if ((A1[i] + A2[i] > k) ||
(A1[i] + A2[i] < k / 2))
{
flag = 1;
break ;
}
}
if (flag == 1)
document.write( "No" );
else
document.write( "Yes" );
}
let arr1 = [ 1, 3, 4, 5 ];
let arr2 = [ 2, 0, 1, 1 ];
let K = 6;
let N = arr1.length;
checkArrangement(arr1, arr2, N, K);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...