Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array in C++


Suppose we have two arrays A and B with n numbers, we have to rearrange the elements of B in itself in a way such that the sequence formed by (A[i] + B[i]) % n after rearranging it is lexicographically smallest. Finally we will return the lexicographically smallest possible sequence.

So, if the input is like A = {1, 2, 3, 2}, B = {4, 3, 2, 2}, then the output will be [0, 0, 1, 2]

To solve this, we will follow these steps −

  • n := size of a

  • Define one map my_map

  • Define one set my_set

  • for initialize i := 0, when i < n, update (increase i by 1), do −

    • (increase my_map[b[i]] by 1)

    • insert b[i] into my_set

  • Define an array sequence

  • for initialize i := 0, when i < n, update (increase i by 1), do −

    • if a[i] is same as 0, then −

      • it := elements of my_set which are not smaller than 0, initially point to first element

      • value := it

      • insert value mod n at the end of sequence

      • (decrease my_map[value] by 1)

      • if my_map[value] is zero, then −

        • delete value from my_set

  • Otherwise

    • x := n - a[i]

    • it := elements of my_set which are not smaller than x, initially point to first element

    • if it is not in my_set, then −

      • it := elements of my_set which are not smaller than 0, initially point to first element

    • value := it

    • insert (a[i] + value) mod n at the end of sequence

    • (decrease my_map[value] by 1)

    • if my_map[value] is zero, then −

      • delete value from my_set

  • return sequence

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v) {
   cout << "[";
   for (int i = 0; i < v.size(); i++) {
      cout << v[i] << ", ";
   }
   cout << "]" <; endl;
}
vector<int> solve(vector<int>&a, vector<int> &b) {
   int n = a.size();
   unordered_map<int, int> my_map;
   set<int> my_set;
   for (int i = 0; i < n; i++) {
      my_map[b[i]]++;
      my_set.insert(b[i]);
   }
   vector<int> sequence;
   for (int i = 0; i < n; i++) {
      if (a[i] == 0) {
         auto it = my_set.lower_bound(0);
         int value = *it;
         sequence.push_back(value % n);
         my_map[value]--;
         if (!my_map[value])
            my_set.erase(value);
         }
         else {
            int x = n - a[i];
            auto it = my_set.lower_bound(x);
            if (it == my_set.end())
               it = my_set.lower_bound(0);
            int value = *it;
            sequence.push_back((a[i] + value) % n);
            my_map[value]--;
            if (!my_map[value])
               my_set.erase(value);
         }
      }
   return sequence;
}
int main() {
   vector<int> a = {1, 2, 3, 2};
   vector<int> b = {4, 3, 2, 2};
   vector<int> res = solve(a, b);
   print_vector(res);
}

Input

{1, 2, 3, 2}, {4, 3, 2, 2}

Output

[0, 0, 1, 2, ]

Updated on: 20-Aug-2020

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements