Reduce sum of same-indexed elements of two arrays to less than K by rearranging the second array
Given two arrays arr1[] and arr2[], both of size N and an integer X, the task is to check if the sum of same-indexed elements of both the arrays at corresponding indices can be made at most K after rearranging the second array. If it is possible then print “Yes” else print “No”.
Examples:
Input: arr1[] = {1, 2, 3}, arr2[] = {1, 1, 2}, X = 4
Output: Yes
Explanation:
Rearranging the array B[] as {1, 2, 1}. Now the sum of corresponding indices are:
A[0] + B[0] = 1 + 1 = 2 ? 4
A[1] + B[1] = 2 + 2 = 4 ? 4
A[2] + B[2] = 3 + 1 = 4 ? 4
Input: arr1[] = {1, 2, 3, 4}, arr2[] = {1, 2, 3, 4}, X = 4
Output: No
Explanation: There is no way that the array B[] can be rearranged such that the condition A[i] + B[i] <= X is satisfied.
Naive Approach: The simplest approach is to generate all possible permutations of the array B[] and if any permutation satisfies the given condition, then print Yes. Otherwise, print No.
Time Complexity: O(N!)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Sorting. 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 rearrange( int A[], int B[],
int N, int X)
{
bool flag = true ;
sort(A, A + N);
sort(B, B + N, greater< int >());
for ( int i = 0; i < N; i++) {
if (A[i] + B[i] > X) {
flag = false ;
break ;
}
}
if (flag)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int A[] = { 1, 2, 3 };
int B[] = { 1, 1, 2 };
int X = 4;
int N = sizeof (A) / sizeof (A[0]);
rearrange(A, B, N, X);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void rearrange( int A[], int B[],
int N, int X)
{
boolean flag = true ;
Arrays.sort(A);
Arrays.sort(B);
for ( int i = 0 ; i < N; i++)
{
if (A[i] + B[N - 1 - i] > X)
{
flag = false ;
break ;
}
}
if (flag == true )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main (String[] args)
{
int A[] = { 1 , 2 , 3 };
int B[] = { 1 , 1 , 2 };
int X = 4 ;
int N = A.length;
rearrange(A, B, N, X);
}
}
|
Python3
def rearrange(A, B, N, X):
flag = True
A = sorted (A)
B = sorted (B)[:: - 1 ]
for i in range (N):
if (A[i] + B[i] > X):
flag = False
break
if (flag):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
A = [ 1 , 2 , 3 ]
B = [ 1 , 1 , 2 ]
X = 4
N = len (A)
rearrange(A, B, N, X)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void rearrange( int [] A, int [] B,
int N, int X)
{
bool flag = true ;
Array.Sort(A);
Array.Sort(B);
for ( int i = 0; i < N; i++)
{
if (A[i] + B[N - 1 - i] > X)
{
flag = false ;
break ;
}
}
if (flag == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main()
{
int []A = { 1, 2, 3 };
int []B = { 1, 1, 2 };
int X = 4;
int N = A.Length;
rearrange(A, B, N, X);
}
}
|
Javascript
<script>
function rearrange(A, B, N, X)
{
let flag = true ;
A.sort();
B.sort();
for (let i = 0; i < N; i++)
{
if (A[i] + B[N - 1 - i] > X)
{
flag = false ;
break ;
}
}
if (flag == true )
document.write( "Yes" );
else
document.write( "No" );
}
let A = [ 1, 2, 3 ];
let B = [ 1, 1, 2 ];
let X = 4;
let N = A.length;
rearrange(A, B, N, X);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Last Updated :
04 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...