Permutation of an array that has smaller values from another array
Given two arrays A and B of equal size. The task is to print any permutation of array A such that the number of indices i for which A[i] > B[i] is maximized.
Examples:
Input: A = [12, 24, 8, 32],
B = [13, 25, 32, 11]
Output: 24 32 8 12
Input: A = [2, 7, 11, 15],
B = [1, 10, 4, 11]
Output: 2 11 7 15
If the smallest element in A beats the smallest element in B, we should pair them. Otherwise, it is useless for our score, as it can’t beat any other element of B.
With above strategy we make two vector of pairs, Ap for A and Bp for B with their element and respective index. Then sort both vectors and simulate them. Whenever we found any element in vector Ap such that Ap[i].first > Bp[j].first for some (i, j) we pair them i:e we update our answer array to ans[Bp[j].second] = Ap[i].first. However if Ap[i].first < Bp[j].first for some (i, j) then we store them in vector remain and finally pair them with any one.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void anyPermutation( int A[], int B[], int n)
{
vector<pair< int , int > > Ap, Bp;
for ( int i = 0; i < n; i++)
Ap.push_back(make_pair(A[i], i));
for ( int i = 0; i < n; i++)
Bp.push_back(make_pair(B[i], i));
sort(Ap.begin(), Ap.end());
sort(Bp.begin(), Bp.end());
int i = 0, j = 0, ans[n] = { 0 };
vector< int > remain;
while (i < n && j < n) {
if (Ap[i].first > Bp[j].first) {
ans[Bp[j].second] = Ap[i].first;
i++;
j++;
}
else {
remain.push_back(i);
i++;
}
}
j = 0;
for ( int i = 0; i < n; ++i)
if (ans[i] == 0) {
ans[i] = Ap[remain[j]].first;
j++;
}
for ( int i = 0; i < n; ++i)
cout << ans[i] << " " ;
}
int main()
{
int A[] = { 12, 24, 8, 32 };
int B[] = { 13, 25, 32, 11 };
int n = sizeof (A) / sizeof (A[0]);
anyPermutation(A, B, n);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void anyPermutation( int A[], int B[], int n)
{
ArrayList< int []> Ap = new ArrayList<>();
ArrayList< int []> Bp = new ArrayList<>();
for ( int i = 0 ; i < n; i++)
Ap.add( new int [] { A[i], i });
for ( int i = 0 ; i < n; i++)
Bp.add( new int [] { B[i], i });
Collections.sort(Ap, (x, y) -> {
if (x[ 0 ] != y[ 0 ])
return x[ 0 ] - y[ 0 ];
return y[ 1 ] - y[ 1 ];
});
Collections.sort(Bp, (x, y) -> {
if (x[ 0 ] != y[ 0 ])
return x[ 0 ] - y[ 0 ];
return y[ 1 ] - y[ 1 ];
});
int i = 0 , j = 0 ;
int ans[] = new int [n];
ArrayList<Integer> remain = new ArrayList<>();
while (i < n && j < n)
{
if (Ap.get(i)[ 0 ] > Bp.get(j)[ 0 ])
{
ans[Bp.get(j)[ 1 ]] = Ap.get(i)[ 0 ];
i++;
j++;
}
else
{
remain.add(i);
i++;
}
}
j = 0 ;
for (i = 0 ; i < n; ++i)
if (ans[i] == 0 )
{
ans[i] = Ap.get(remain.get(j))[ 0 ];
j++;
}
for (i = 0 ; i < n; ++i)
System.out.print(ans[i] + " " );
}
public static void main(String[] args)
{
int A[] = { 12 , 24 , 8 , 32 };
int B[] = { 13 , 25 , 32 , 11 };
int n = A.length;
anyPermutation(A, B, n);
}
}
|
Python3
def anyPermutation(A, B, n):
Ap, Bp = [], []
for i in range ( 0 , n):
Ap.append([A[i], i])
for i in range ( 0 , n):
Bp.append([B[i], i])
Ap.sort()
Bp.sort()
i, j = 0 , 0 ,
ans = [ 0 ] * n
remain = []
while i < n and j < n:
if Ap[i][ 0 ] > Bp[j][ 0 ]:
ans[Bp[j][ 1 ]] = Ap[i][ 0 ]
i + = 1
j + = 1
else :
remain.append(i)
i + = 1
j = 0
for i in range ( 0 , n):
if ans[i] = = 0 :
ans[i] = Ap[remain[j]][ 0 ]
j + = 1
for i in range ( 0 , n):
print (ans[i], end = " " )
if __name__ = = "__main__" :
A = [ 12 , 24 , 8 , 32 ]
B = [ 13 , 25 , 32 , 11 ]
n = len (A)
anyPermutation(A, B, n)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static void anyPermutation( int [] A, int [] B, int n)
{
List< int []> Ap = new List< int []>();
List< int []> Bp = new List< int []>();
for ( int i = 0; i < n; i++)
{
Ap.Add( new int []{A[i], i});
}
for ( int i = 0; i < n; i++)
{
Bp.Add( new int []{B[i], i});
}
Ap.Sort((x,y)=> (x[0] != y[0]) ? x[0] - y[0] : y[1] - y[1]);
Bp.Sort((x,y)=> (x[0] != y[0]) ? x[0] - y[0] : y[1] - y[1]);
var ii = 0;
var j = 0;
int [] ans = new int [n];
var remain = new List< int >();
while (ii < n && j < n)
{
if (Ap[ii][0] > Bp[j][0])
{
ans[Bp[j][1]] = Ap[ii][0];
ii++;
j++;
}
else
{
remain.Add(ii);
ii++;
}
}
j = 0;
for ( var i = 0; i < n; ++i)
{
if (ans[i] == 0)
{
ans[i] = Ap[remain[j]][0];
j++;
}
}
for ( var i = 0; i < n; ++i)
{
Console.Write(ans[i].ToString() + " " );
}
}
public static void Main(String[] args)
{
int [] A = {12, 24, 8, 32};
int [] B = {13, 25, 32, 11};
var n = A.Length;
GFG.anyPermutation(A, B, n);
}
}
|
Javascript
<script>
function anyPermutation(A, B, n){
let Ap = [], Bp = []
for (let i=0;i<n;i++){
Ap.push([A[i], i])
}
for (let i=0;i<n;i++){
Bp.push([B[i], i])
}
Ap.sort()
Bp.sort()
let i = 0
let j = 0
let ans = new Array(n).fill(0)
let remain = []
while (i < n && j < n){
if (Ap[i][0] > Bp[j][0]){
ans[Bp[j][1]] = Ap[i][0]
i += 1
j += 1
}
else {
remain.push(i)
i += 1
}
}
j = 0
for (let i=0;i<n;i++){
if (ans[i] == 0){
ans[i] = Ap[remain[j]][0]
j += 1
}
}
for (let i=0;i<n;i++){
document.write(ans[i], " " )
}
}
let A = [ 12, 24, 8, 32 ]
let B = [ 13, 25, 32, 11 ]
let n = A.length
anyPermutation(A, B, n)
</script>
|
Time Complexity: O(N*log(N)), where N is the length of array.
Last Updated :
18 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...