Can someone please tell me an approach and possibly the optimal code for this problem.
Given an array A, you need to split the array into non empty subarrays such that each subarray is a permutation (each number from 1 to the n appears exactly once where n is the size of the subarray). For eg: A=2,1,3,6,3,1,2,5,4,2,1. Then the subarrays are {2,1,3},{6,3,1,2,5,4},{2,1}. You need to return these subarrays. It is guaranteed that answer always exists and the given array can always be split in such a way that every subarray is a permutation.








Auto comment: topic has been updated by NewCoder100 (previous revision, new revision, compare).
Why not ask AI? I think it's perfectly fine to use AI for learning knowledge, as long as you don't use it to cheat.
I did but it was not able to give a correct solution :(
Assume ans always exist
We scan A from left to right and keep a current set S of distinct values in the ongoing segment, plus minimum and maximum value . Let sz = size of set , At each element x:
If x == sz + 1, insert x and continue (we extended the permutation to 1..sz+1).
Otherwise, close the current segment (save it), clear S, and start a new segment at the current index, inserting x into the fresh S.
When the scan ends, the final S must be a permutation (problem guarantees a solution), so close it too.
i think this should be okk. there can be various other solution
This logic fails. For example {1,2,3,1,2,4}
hmm .. thanks
I have updated the question where it states that answer always exists.. so your given array is invalid
{1,2,3,1,2,4} -> {1,2}, {3,1,2,4} What are the constraints for this problem?
I find a DP solution but complexity O ( n^2 )
I also came up with O(N^2) dp solution. It works close to O(N) on random data, but it won't work for array like {1,2....100'000}
Auto comment: topic has been updated by NewCoder100 (previous revision, new revision, compare).
In $$$\mathcal{O}(n)$$$, use an unordered set $$$S$$$. We'll iterate from $$$i=0$$$ to $$$i=n-1$$$ while building maximum size permutation greedily.
You'll need to maintain $$$i$$$ the starting index for the current permutation subarray and $$$j \geq i$$$ the current processed element in $$$a$$$. For each index $$$a[j]$$$ :
either $$$a[j]$$$ was not already inserted in $$$S$$$. Then insert it.
or it was already inserted, and your permutation must be between indices $$$i$$$ and $$$j-1$$$. Iterate over values from $$$1$$$ to $$$j-i$$$ until it cannot be found in S. For each, remove it from S, increase $$$i$$$ by $$$1$$$ and update the current subarray. Then add the subarray to the answer.
giving wrong answer for [2 , 1 , 3 , 4]
maybe you can use the fact that each value
1needs to be partitioned separately?In that case, you already know a lot like how many groups there will be, etc. Then just need to do frequency analysis of all values and assign them into right groups.
Just notice that the number of valid interval will not exceed 2n.Use segtree or whatever you like to find all the 2n predecessor is efficient enough for n <= 10^6 .This algorithm can also be used to determine whether a solution exists.
I dont have any proof and neither I can disprove this but it seems that this algorithm is O(n log n).
The Algorithm is simple. We will maintain a dp such that dp[i] means the prefix till i can be written as some subarrays of permutation.
To calculate dp[r] we maintain the last and second last index of each number from 1 to n in the array as we go from left to right. Initially they are both set to -1 for every number.
Then for some r fixed, we will fix l to be r. Then we will find the last index of 1 just before or equal to r. Then we will update our value of l. If at any point the maximum in second last indices will be greater than or equal to l then we end iteration.
we also maintain a segment tree to find maximum element in any range. So we will find maximum in (l, r) if the array is unique and the maximum is same as r-l+1. then that means we found one valid permutation, otherwise we will set l to be r-maximum+1. if l becomes less than or equal to the maximum in second last index then we will just stop and move next.
If valid permutation then we will check if dp[l-1] is true. If yes then we will set dp[r] to be true and move forward. Otherwise we will change the value of l to the last index of maximum+1 and continue this approach. Sorry to have a messy approach but approach is actually simple.(if you know segment tree) I would be glad if it could be proven to be amortized O(n log n) or even proven to have like O(n^2 log n) worst case time complexity.
https://www.luogu.com.cn/problem/P9970 just translate the tutorial
[problem:1870E]has a prove
Thanks a lot