C++ Permutation of an Array that has Smaller Values from Another Array

Two arrays, A and B, are given to us in this tutorial. For example, we need to output any permutation of A so that the indices for which A[ I ] > B[ I ] are maximized, for example

Input: A = [12, 22, 41, 13],
B = [1, 20, 10, 12]
Output: 12, 22, 41, 13

Input: A = [2, 5, 9, 7],
B = [1, 12, 4, 54]
Output: 2 7 5 9

Multiple answers can be present in that case we are simply going to print any one of the answers.

In this problem, we need to maximize the indices where A[ i ] > B[ i ], so we are going to solve this problem greedily.

Approach to Find the Solution

In this approach, we will first sort both arrays now; we check greedily for each index of array B such that A[ i ] is more significant than it and then place that element in a vector.


#include <bits/stdc++.h>
using namespace std;
int main(){
    int A[] = { 2, 5, 9, 7 };
    int B[] = { 1, 12, 4, 54 };
    int n = sizeof(A) / sizeof(int); // size of our arrays
    vector<pair<int, int> > A_pair, B_pair;
    /***********************We are linking element to its position***********/
    for (int i = 0; i < n; i++)
        A_pair.push_back({A[i], i});
    for (int i = 0; i < n; i++)
        B_pair.push_back({B[i], i});
    /*****Sorting our pair vectors********************/
    sort(A_pair.begin(), A_pair.end());
    sort(B_pair.begin(), B_pair.end());
    int i = 0, j = 0, ans[n];
    memset(ans, -1, sizeof(ans)); // initializing all the elements with value -1
    vector<int> remaining; // this will store our elements which have lesser value than elemnt present in B.
    while (i < n && j < n) {
        // as our array is sorted then if we find any element in
        //current index which has less value than B of current index then
        // automatically it will have less value than other elements of B
        // that's why we push such indices in remaining otherwise we push element in ans
        if (A_pair[i].first > B_pair[j].first) {
            ans[B_pair[j].second] = A_pair[i].first;
        else {
    j = 0;
    for (int i = 0; i < n; ++i){
        // now if any index of answer is unchanged so that means
        //we need to fill that position with the remaining elements
        if (ans[i] == -1){
            ans[i] = A_pair[remaining[j]].first;
    for (int i = 0; i < n; i++) // printing our answer
        cout << ans[i] << " ";
    return 0;


2 7 5 9

Explanation of the Above Code

In this approach, we first link all the elements to their indices to still have their old index in it when we sort it. We sort both of the vectors of pairs now we greedily search for our answers as we move through both the arrays if we get an index of A_pair which has more excellent value than of B_pair, so we store that in our an array(and in the position of B_pair) else as we have sorted both the vectors, so we know that we won’t be able to use this value of A_pair, so we push that elements index in our remaining vector now we fill the array by the help of remaining vector, and then we print the answer.


In this tutorial, we solve a problem to find the Permutation of an array with smaller values from another array. We also learned the C++ program for this problem and the complete approach we solved. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.

Updated on: 25-Nov-2021


Kickstart Your Career

Get certified by completing the course

Get Started