Check if mapping is possible to make sum of first array larger than second array
Given a positive integer K and two arrays A[] and B[] of size N and M respectively, each containing elements in the range [1, K]. The task is to map all numbers from 1 to K with a function f, such that f(1)<f(2)<f(3)<…<f(K), and find whether the sum of f(A[i]) for 0≤i<N is greater than the sum of f(B[i]) for 0≤i<M.
Examples:
Input: A[] = {2, 2, 2}, B[] = {1, 1, 3}, K=3
Output: YES
Explanation: One possible way is to assign the following values to the function f as follows:
f(1)=1
f(2)=2
f(3)=3
Sum of f(A[i]) for every i = 2+2+2 = 6 and sum of f(B[i]) for every i = 1+1+3 = 5.
Input: A[] = {8, 2, 8, 6, 9, 10}, B[] = {1, 2, 3, 4}, K=10
Output: YES
Approach: Follow the steps below to solve the problem:
- If the value of N>M, the print “YES” as the answer, since an assignment can always be made such that the sum of A is greater than B.
- Otherwise, sort the arrays A[] and B[] in descending order.
- Iterate in the range [0, N-1], using the variable i and if for any index, A[i]>B[i], print “YES” as the answer since assigning a large enough value to A[i] will make the sum of A[] is greater than B[].
- Otherwise, print “NO” as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int isPossible( int A[], int B[], int K, int N, int M)
{
if (N > M)
return 1;
sort(A, A + N, greater< int >());
sort(B, B + M, greater< int >());
for ( int i = 0; i < N; i++)
if (A[i] > B[i])
return 1;
return 0;
}
int main()
{
int A[] = { 2, 2, 2 };
int B[] = { 1, 1, 3 };
int K = 3;
int N = sizeof (A) / sizeof (A[0]);
int M = sizeof (B) / sizeof (B[0]);
if (isPossible(A, B, K, N, M))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
class GFG {
public static void reverse( int array[])
{
int n = array.length;
for ( int i = 0 ; i < n / 2 ; i++) {
int temp = array[i];
array[i] = array[n - i - 1 ];
array[n - i - 1 ] = temp;
}
}
public static int isPossible( int A[], int B[], int K,
int N, int M)
{
if (N > M)
return 1 ;
Arrays.sort(A);
reverse(A);
Arrays.sort(B);
reverse(B);
for ( int i = 0 ; i < N; i++)
if (A[i] > B[i])
return 1 ;
return 0 ;
}
public static void main(String[] args)
{
int A[] = { 2 , 2 , 2 };
int B[] = { 1 , 1 , 3 };
int K = 3 ;
int N = A.length;
int M = B.length;
if (isPossible(A, B, K, N, M) == 1 )
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def isPossible(A, B, K, N, M):
if (N > M):
return 1
A.sort(reverse = True )
B.sort(reverse = True )
for i in range (N):
if (A[i] > B[i]):
return 1
return 0
if __name__ = = '__main__' :
A = [ 2 , 2 , 2 ]
B = [ 1 , 1 , 3 ]
K = 3
N = len (A)
M = len (B)
if (isPossible(A, B, K, N, M)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int isPossible( int []A, int []B, int K,
int N, int M)
{
if (N > M)
return 1;
Array.Sort(A);
Array.Reverse(A);
Array.Sort(B);
Array.Reverse(B);
for ( int i = 0; i < N; i++)
if (A[i] > B[i])
return 1;
return 0;
}
public static void Main()
{
int []A = { 2, 2, 2 };
int []B = { 1, 1, 3 };
int K = 3;
int N = A.Length;
int M = B.Length;
if (isPossible(A, B, K, N, M) == 1)
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
Javascript
<script>
function isPossible(A, B, K, N, M) {
if (N > M)
return 1;
A.sort( function (a,b) { return b - a })
B.sort( function (a,b) { return b - a })
for (let i = 0; i < N; i++)
if (A[i] > B[i])
return 1;
return 0;
}
var A = [2, 2, 2];
var B = [1, 1, 3];
var K = 3;
var N = A.length;
var M = B.length;
if (isPossible(A, B, K, N, M))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Last Updated :
06 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...