-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path6th_question_July.cpp
88 lines (68 loc) · 2 KB
/
6th_question_July.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#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 appears, 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;
}