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++

`// CPP program to find permutation of an array that ` `// has smaller values from another array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print required permutation ` `void` `anyPermutation(` `int` `A[], ` `int` `B[], ` `int` `n) ` `{ ` ` ` `// Storing elements and indexes ` ` ` `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 }; ` ` ` ` ` `// Filling the answer array ` ` ` `vector<` `int` `> remain; ` ` ` `while` `(i < n && j < n) { ` ` ` ` ` `// pair element of A and B ` ` ` `if` `(Ap[i].first > Bp[j].first) { ` ` ` `ans[Bp[j].second] = Ap[i].first; ` ` ` `i++; ` ` ` `j++; ` ` ` `} ` ` ` `else` `{ ` ` ` `remain.push_back(i); ` ` ` `i++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Fill the remaining elements of answer ` ` ` `j = 0; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `if` `(ans[i] == 0) { ` ` ` `ans[i] = Ap[remain[j]].first; ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// Output required permutation ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `cout << ans[i] << ` `" "` `; ` `} ` ` ` `// Driver program ` `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; ` `} ` ` ` `// This code is written by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find permutation of ` `# an array that has smaller values from ` `# another array ` ` ` `# Function to print required permutation ` `def` `anyPermutation(A, B, n): ` ` ` ` ` `# Storing elements and indexes ` ` ` `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 ` ` ` ` ` `# Filling the answer array ` ` ` `remain ` `=` `[] ` ` ` `while` `i < n ` `and` `j < n: ` ` ` ` ` `# pair element of A and B ` ` ` `if` `Ap[i][` `0` `] > Bp[j][` `0` `]: ` ` ` `ans[Bp[j][` `1` `]] ` `=` `Ap[i][` `0` `] ` ` ` `i ` `+` `=` `1` ` ` `j ` `+` `=` `1` ` ` ` ` `else` `: ` ` ` `remain.append(i) ` ` ` `i ` `+` `=` `1` ` ` ` ` `# Fill the remaining elements ` ` ` `# of answer ` ` ` `j ` `=` `0` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` `if` `ans[i] ` `=` `=` `0` `: ` ` ` `ans[i] ` `=` `Ap[remain[j]][` `0` `] ` ` ` `j ` `+` `=` `1` ` ` ` ` `# Output required permutation ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` `print` `(ans[i], end ` `=` `" "` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `A ` `=` `[ ` `12` `, ` `24` `, ` `8` `, ` `32` `] ` ` ` `B ` `=` `[ ` `13` `, ` `25` `, ` `32` `, ` `11` `] ` ` ` `n ` `=` `len` `(A) ` ` ` `anyPermutation(A, B, n) ` ` ` `# This code is contributed ` `# by Rituraj Jain ` |

*chevron_right*

*filter_none*

**Output:**

24 32 8 12

**Time Complexity:** O(N*log(N)), where N is the length of array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Delete array elements which are smaller than next or become smaller
- Minimum number of given operations required to convert a permutation into an identity permutation
- Maximize a number considering permutations with values smaller than limit
- Check if permutaion of one string can break permutation of another
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Sort first k values in ascending order and remaining n-k values in descending order
- Count of strings in the first array which are smaller than every string in the second array
- Find the nearest smaller numbers on left side in an array
- Find next Smaller of next Greater in an array
- Count of smaller or equal elements in sorted array
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Minimizing array sum by subtracting larger elements from smaller ones
- Construct array having X subsequences with maximum difference smaller than d
- First strictly smaller element in a sorted array in Java
- Find closest smaller value for every element in array
- Count of smaller elements on right side of each element in an Array using Merge sort
- Largest element smaller than current element on left for every element in Array
- Count of array elements which is smaller than both its adjacent elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.