Given two arrays **A** and** B** of **N** integers. Reorder the elements of B in itself in such a way that the sequence formed by **(A[i] + B[i]) % N** after re-ordering is the smallest lexicographically. The task is to print the **lexicographically** *smallest* sequence possible.

**Note**: The array elements are in range [0, n).

**Examples:**

Input:a[] = {0, 1, 2, 1}, b[] = {3, 2, 1, 1}

Output:1 0 0 2

Reorder B to {1, 3, 2, 1} to get the smallest sequence possible.

Input:a[] = {2, 0, 0}, b[] = {1, 0, 2}

Output:0 0 2

**Approach:** The problem can be solved *greedily*. Initially keep a count of all the numbers of array B using hashing, and store them in the set in C++, so that lower_bound() [* To check for an element* ] and erase() [ *To erase an element* ] operations can be done in logarithmic time.

For every element in the array, check for a number equal or greater than** n-a[i]** using *lower_bound* function. If there are no such elements then take the smallest element in the set. Decrease the value by 1 in the hash table for the number used, if the hash table’s value is 0, then erase the element from the set also.

However there is an exception if the array element is 0, then check for 0 at the first then for N, if both of them are not there, then take the smallest element.

Below is the implementation of the above approach:

`// C++ implementation of the ` `// above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to get the smallest ` `// sequence possible ` `void` `solve(` `int` `a[], ` `int` `b[], ` `int` `n) ` `{ ` ` ` ` ` `// Hash-table to count the ` ` ` `// number of occurrences of b[i] ` ` ` `unordered_map<` `int` `, ` `int` `> mpp; ` ` ` ` ` `// Store the element in sorted order ` ` ` `// for using binary search ` ` ` `set<` `int` `> st; ` ` ` ` ` `// Iterate in the B array ` ` ` `// and count the occurrences and ` ` ` `// store in the set ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `mpp[b[i]]++; ` ` ` `st.insert(b[i]); ` ` ` `} ` ` ` ` ` `vector<` `int` `> sequence; ` ` ` ` ` `// Iterate for N elements ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// If the element is 0 ` ` ` `if` `(a[i] == 0) { ` ` ` ` ` `// Find the nearest number to 0 ` ` ` `auto` `it = st.lower_bound(0); ` ` ` `int` `el = *it; ` ` ` `sequence.push_back(el % n); ` ` ` ` ` `// Decrease the count ` ` ` `mpp[el]--; ` ` ` ` ` `// Erase if no more are there ` ` ` `if` `(!mpp[el]) ` ` ` `st.erase(el); ` ` ` `} ` ` ` ` ` `// If the element is other than 0 ` ` ` `else` `{ ` ` ` ` ` `// Find the difference ` ` ` `int` `x = n - a[i]; ` ` ` ` ` `// Find the nearest number which can give us ` ` ` `// 0 on modulo ` ` ` `auto` `it = st.lower_bound(x); ` ` ` ` ` `// If no such number occurs then ` ` ` `// find the number closest to 0 ` ` ` `if` `(it == st.end()) ` ` ` `it = st.lower_bound(0); ` ` ` ` ` `// Get the number ` ` ` `int` `el = *it; ` ` ` ` ` `// store the number ` ` ` `sequence.push_back((a[i] + el) % n); ` ` ` ` ` `// Decrease the count ` ` ` `mpp[el]--; ` ` ` ` ` `// If no more appers, then erase it from set ` ` ` `if` `(!mpp[el]) ` ` ` `st.erase(el); ` ` ` `} ` ` ` `} ` ` ` ` ` `for` `(` `auto` `it : sequence) ` ` ` `cout << it << ` `" "` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 0, 1, 2, 1 }; ` ` ` `int` `b[] = { 3, 2, 1, 1 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `solve(a, b, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 0 0 2

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:

- Largest number not greater than N which can become prime after rearranging its digits
- Find the smallest and second smallest elements in an array
- Count of distinct XORs formed by rearranging two Binary strings
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
- Count of elements which are second smallest among three consecutive elements
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by removing at most one character
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Order of indices which is lexicographically smallest and sum of elements is <= X
- Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1
- Smallest number by rearranging digits of a given number
- Find the lexicographically smallest string which satisfies the given condition
- Lexicographically smallest rotated sequence | Set 2
- Maximum sum of smallest and second smallest in an array
- Check whether second string can be formed from characters of first string
- Count of times second string can be formed from the characters of first string
- Cost of rearranging the array such that no element exceeds the sum of its adjacent elements
- Lexicographically smallest string which differs from given strings at exactly K indices
- Find elements which are present in first array and not in second
- Print the lexicographically smallest array by swapping elements whose sum is odd

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.