Using DSU || Simple Beginner Friendly Solution with Dry Run
Intuitionprerequisite : DSU. If you don't know what this is then I'll recommend u to study that first. You can follow this to study dsu: DSU by StriverNow that
# Intuition
**prerequisite** : DSU. If you don't know what this is then I'll recommend u to study that first. You can follow this to study dsu: [DSU by Striver](https://takeuforward.org/data-structure/disjoint-set-union-by-rank-union-by-size-path-compression-g-46/)

Now that you know what dsu is lets understand why dsu can be used here to efficiently track the longest prefix length.
**The intuition behind using a DSU in this problem is to efficiently track and manage consecutive uploaded videos as a single connected component.**
The longest prefix of uploaded videos starting from 1 can be determined by checking the size of the connected component that starts from video 1.
Here each time we add a video, we can (i):Connect to its next video (if uploaded) and (ii): Connect to its previous video (if uploaded).
Note that here we wont be doing union by size or union by rank, instead we will connect the larger video number to the smaller video number (here video number does not means size) and add the size of ultimate parent of larger video number to ultimate parent of smaller video number so that for each chunk , the size[start] stores the size of the component.
By merging these groups, DSU ensures that all consecutive uploaded videos are treated as one connected component.
# Approach
Inorder to check whether the next and previous videos have been uploaded we will maintain an array. This will also help us to check whether '1' has been uploaded previously at any particular moment.

# Upload function:
- Connect the video with the next video(if the next video is visited) by taking a union between them.
- Connect the video with the prev video(if the prev one is visited or already added) by taking a union between them.
- Now mark the current video as visited or available.
- Then check if the video '1' is already available if it is the size[1] is the length of the component starting from '1', update the variable which stores the maximum length prefix.
> Dry run: Suppose we already have videos 1 and 3 and now we want to upload 2
![longest_uploaded_prefix_dry_run.jpg](https://assets.leetcode.com/users/images/be665801-9a4b-4d1e-bbe7-ede6928dbb7e_1736713688.624287.jpeg)


# longest function:
just return the variable that we have used to maintain the maximum prefix length.

# Code
```cpp []
class DisjointSet{
    public:
    vector<int>parent;
    vector<int>size;
    DisjointSet(int n){
        parent.resize(n);
        size.resize(n);
        for(int i=0;i<n;i++){
            parent[i]=i;
            size[i]=1;
        }
    }
    int findUPar(int node){
        if(parent[node]==node){
            return node;
        }
        return parent[node]=findUPar(parent[node]);
    }
    void un(int u,int v){
        //u mein smaller bhejo
        int uu=findUPar(u);
        int uv=findUPar(v);
        if(uu==uv){
            return;
        }
        parent[uv]=uu;
        size[uu]+=size[uv];
        
    }
};

class LUPrefix {
public:
    DisjointSet* ds=NULL;
    vector<int>v; 
    int maxi=0;
    int n;
    LUPrefix(int n) {
        ds=new DisjointSet(n+1);
        v.resize(n+1,0);
        this->n=n;
    }
    
    void upload(int video) {
        if(video<n){
            if(v[video+1]==1){
                ds->un(video,video+1);
            }
        }
        if(video>1){
            if(v[video-1]==1){
                ds->un(video-1,video);
            }
        }
        v[video]=1;
        if(v[1]==1){
            maxi=max(maxi,ds->size[1]);
        }
    }
    
    int longest() {
        return maxi;
        
    }
};

/**
 * Your LUPrefix object will be instantiated and called as such:
 * LUPrefix* obj = new LUPrefix(n);
 * obj->upload(video);
 * int param_2 = obj->longest();
 */
```
['Union Find', 'C++']