**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.

var1var2Initialise 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.

SolutionFirst 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 example

Permu :

`1 2 3 4 5 6`

Count :

`x x x x 2 x`

Permu :

`1 2 4 5 3 6`

The 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).

CodeHow 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 ?

I didn't understand, why do we care about cur_max?

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.IDK if it's working, but can you prove this solution?

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.

Yup got u. This might work. Write a code and pass it to me

some cases I wrote:

https://ideone.com/72yjBz

are data structures like ordered set permitted to use in interviews?

No, but i guess its better to say something instead of being blank !

No I guess, but you can implement the ordered set with binary indexed tree and use it

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).

I think Kefrov's solution is pretty neat.

However, if you can't use

`PBDS`

in an interview, you can use`std::next`

for 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## 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.

https://codeforces.com/edu/course/2/lesson/4/3/practice/contest/274545/problem/B

It’s a standard segment tree problem of generating the permutation using inversion array.