### Sahilamin219's blog

By Sahilamin219, history, 11 days ago,

count array : for a given array it stores, for each ith element number of element greater than it's value from the left.

For a given number N , there exits an permutation array (1,N) such that it gives you same count array. You are given an count array and you have to find original array.

Example : count array => 0 0 0 1 2 0

then original array would be => 1 2 5 4 3 6

Required Solution was Nlog(N).

Hope the question is clear, if there's any doubt plz comment.

• -7

 » 11 days ago, # | ← Rev. 4 →   0 var1// Variant 1 #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, tmp; cin >> n; vector a(n); vector abc(n); for (int i = 0; i < n; i++) { cin >> a[i]; abc[i] = (i + 1); } for (int i = n - 1; i >= 0; i--) { tmp = a[i]; a[i] = abc[abc.size() - 1 - tmp]; abc.erase(abc.begin() + abc.size() - 1 - tmp); } for (int i = 0; i < n; i++) cout << a[i] << ' '; }  var2// Variant 2 #include #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, tmp; cin >> n; vector a(n); unordered_set abc; for (int i = 0; i < n; i++) { cin >> a[i]; abc.insert(n - i); } for (int i = n - 1; i >= 0; i--) { auto itr = abc.begin(); for (int j = 0; j < a[i]; j++) ++itr; a[i] = *(itr); abc.erase(a[i]); } for (int i = 0; i < n; i++) cout << a[i] << ' '; } 
 » 11 days ago, # | ← Rev. 2 →   +13 Initialise an ordered_set containing integers from 1 to N. You'll be constructing the original array in reverse, because we can make this observation: Consider the element with index N, we know that all the other numbers of the permutation are to the left of it, we can now deduce which number it is thanks to count_array[N]. The log(N) part of the complexity comes from searching in the ordered_set using find_by_order() and removing every element we find so far. Solution#include using namespace std; // Including ordered_set #include #include using namespace __gnu_pbds; #define ordered_set tree,rb_tree_tag,tree_order_statistics_node_update> int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int N; cin >> N; int count_arr[N]; for (int i = 0; i < N; i++) cin >> count_arr[i]; int oset_size = N; ordered_set remaining; for (int i = 1; i <= N; i++) remaining.insert(i); int original_arr[N]; int order; for (int i = N - 1; i >= 0; i--) { order = oset_size - count_arr[i] - 1; original_arr[i] = *remaining.find_by_order(order); remaining.erase(original_arr[i]); oset_size--; } for (int i = 0; i < N; i++) cout << original_arr[i] << " "; return 0; } 
 » 11 days ago, # |   0 First of all, only the order of the elements does matter, the values themselves don't, so this array 1 2 3 4 is equivalent to this 1 3 4 6.if cnt[i] = x means that we can shift the subarray (i, i-x) by 1. For examplePermu : 1 2 3 4 5 6Count : x x x x 2 xPermu :1 2 4 5 3 6The subarray $[1,i-1]$ still has the same sorted order (only order does matter).So how to do this shifting efficiently? Because the subarray is still sorted, and let k be $count[i]$, then $Permu[i] =$ the $k_{th}$ largest value in the current subarray which can be found using ordered_set (PBDS). Codevoid solve() { int n; cin >> n; vector a(n + 1), p(n + 1); orderd_set s; for (int i = 1; i <= n; i++) { cin >> a[i]; s.insert(i); } for (int i = n; i >= 1; i--) { int k = s.size() - a[i] - 1; if (k < 0) return cout << "No solution\n", void(); auto it = s.find_by_order(k); // Kth largest p[i] = *it; s.erase(it); } for (int i = 1; i <= n; i++) cout << p[i] << " "; } 
•  » » 11 days ago, # ^ |   0 How about doing something like this : finding first zero from right, the element at this index(suppose ith) is bound to be current_max available. Then we can decrease array[i+1,n] by 1. And now current_max available will be decreased by 1 and also replace ith element by INF. Range update of (i,n) can be done by lazy seg trees and rightmost 0 can also be found using seg tree. TC : O(nlogn) Is there a better way to do update of (i,n) by -1 ?
•  » » » 11 days ago, # ^ |   0 I didn't understand, why do we care about cur_max?
•  » » » » 11 days ago, # ^ |   0 because cur_max will be filled at rightmost 0 in every iteration.(i meant in permutaion array)arr: 0 0 0 1 2 0 0 0 0 1 2 6 0 0 5 0 1 6 0 0 5 4 0 6 0 0 5 4 3 6 0 2 5 4 3 6 1 2 5 4 3 6 by current_max i simply meant unused max.
•  » » » » » 11 days ago, # ^ |   0 IDK if it's working, but can you prove this solution?
•  » » » » » » 11 days ago, # ^ |   +1 suppose currently in count array there r multiple 0s. Then max element available must be placed at rightmost pos, because if we place at any other place, condition for 0s after that index can't be satisfied. Second, once we place the element at ith(rightmost 0), we have placed one greater element to all pos in its right, so decrease (i+1,n) by 1 in count array. If u think it's wrong, a counter case will be highly welcome.
•  » » » » » » » 11 days ago, # ^ |   0 Yup got u. This might work. Write a code and pass it to mesome cases I wrote: 3 0 1 1 3 1 2 4 0 1 0 2 3 1 4 2 4 0 1 2 3 4 3 2 1 
•  » » » » » » » » 11 days ago, # ^ |   0
•  » » 11 days ago, # ^ |   +10 are data structures like ordered set permitted to use in interviews?
•  » » » 11 days ago, # ^ |   +5 No, but i guess its better to say something instead of being blank !
•  » » » 11 days ago, # ^ |   +5 No I guess, but you can implement the ordered set with binary indexed tree and use it
 » 11 days ago, # |   0 It's just an observation/constructive problem. Start with a sorted array from 1 to N, and update the array from right to left, whenever count[i] is not 0, take the count[i]th number on the left from the index i and put it at index i and put the current number at i in i — 1 (to maintain the sorted behavior).
 » 10 days ago, # | ← Rev. 2 →   0 I think Kefrov's solution is pretty neat.However, if you can't usePBDSin an interview, you can usestd::nextfor this specific problem as std::set is sorted by default. Not sure about something similar in other languages.*(std::next(set.begin(), index)is equivalent to*(__gnu_pbds::ordered_set.find_by_order(order)) Code#include #include #include //. using i64 = long long; void solve() { int n; std::cin >> n; std::vector count(n); for(int& i: count) std::cin >> i; std::set st; for(int i = 1; i <= n; i++) st.insert(i); std::vector permutation(n); int sz = n; for(int i = n - 1; i >= 0; i--) { int pos = sz - count[i] - 1; int ele = *(std::next(st.begin(), pos)); permutation[i] = ele; st.erase(ele); --sz; } for(int i: permutation) std::cout << i << ' '; std::cout << '\n'; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0);std::cout.tie(0); #ifndef ONLINE_JUDGE freopen("input", "r", stdin); freopen("output", "w", stdout); #endif solve(); return 0; } // g++ -std=c++20 .cpp 
• »
»
10 days ago, # ^ |
0

# Thanks so much for applying with us

Hi gitgudmonsta,

Following up on your recent interview with Google, we have decided not to proceed with your application at this time. Although this role didn't work out, we may contact you if we come across another opening that we think could interest you and that matches your skills and experience.

Also, if you applied for any other roles with us recently, look for an update on them soon.

 » 10 days ago, # |   0
 » 10 days ago, # |   0 It’s a standard segment tree problem of generating the permutation using inversion array.