
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 −
#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, ]
- Related Articles
- Find the smallest and second smallest elements in an array in C++
- JavaScript Program to Find Lexicographically smallest rotated sequence
- How many numbers can be formed by arranging the digits of the number 211 ?
- Find maximum points which can be obtained by deleting elements from array in C++
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Find maximum points which can be obtained by deleting elements from array in Python
- Arranging lexicographically and removing whitespaces in JavaScript
- Maximum sum of smallest and second smallest in an array in C++
- Find the lexicographically smallest string which satisfies the given condition in Python
- C++ code to check array can be formed from Equal Not-Equal sequence or not
- Maximum sum of smallest and second smallest in an array in C++ Program
- Lexicographically Smallest Equivalent String in C++
- C program to find the second largest and smallest numbers in an array
- Find elements which are present in first array and not in second in C++
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
