Disjoint set (Union-Find) | Practice | GeeksforGeeks

Given an array A[] that stores all number from 1 to N (both inclusive and sorted) and K queries.
The task is to do the following operations on array elements :
1. UNION X Z : Perform union of X and Z i.e. parent of Z will become the parent of X.
2. FIND X: Find the parent of X and print it.
Note: Initially all are the parent of themselves.
Input:
N = 5, K = 4
queries[] = {{find 4},
             {find 1},
             {unionSet 3 1},
             {find 3}}
Output:
4 1 1
Explanation:
1. The parent of 4 is 4. Hence the output is 4.
2. The parent of 1 is 1. Hence the output is 1.
3. After performing unionSet 3 1, parent of 3 becomes 1,
   since, parent of 1 is currently 1 itself.
4. The parent of 3 is now 1. Hence, the output is 1.


Your Task:  
You don't need to read input or print anything. Your task is to complete the functions- find() which takes an array A[] and an integer X as an input parameter and return the parent of X and the function unionSet() which takes an array A[] and two integers X and Z and performs the union of X and Z.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N, K <= 100
0
badgujarsachin831 month ago
int find(int A[],int X)
{
       //add code here
       if(X==A[X]){
           return X;
       }
       return A[X]=find(A,A[X]);
}
void unionSet(int A[],int X,int Z)
{
	//add code here.
	X=find(A,X);
	Z=find(A,Z);
	A[X]=Z;
}
+2
aloksinghbais023 months ago
C++ solution having time complexity as O(4*alpha) (mathematically proven) and auxiliary space complexity as O(1) is as follows :-
Execution Time :- 0.0 / 1.4 sec
 
int find(int A[],int X){   if(A[X] == X) return (X);   return  A[X] = find(A,A[X]);}void unionSet(int A[],int X,int Z){   X = find(A,X);   Z = find(A,Z);   A[X] = Z;}
+2
sangamchoudhary75 months ago
int find(int A[],int X){
    if(X == A[X]){
        return X;
    }
    return A[X] = find(A,A[X]);
}
void unionSet(int A[],int X,int Z){
    A[find(A,X)] = find(A,Z);
}
0
shivam4215 months ago
int find(int A[],int X) 
{ 
  if(X==A[X]) return X; 
  return A[X]=find(A,A[X]); 
} 
void union (int A[],int X,int Z ) 
{ 
      X=find(A,X); 
      Z=find(A,Z); 
     if(X==Z) return ; 
     A[X]=Z; 
}
 
+2
shreysomu22117 months ago
int find(int A[],int X){      //add code here      if (A[X]==X)          return X;          else           return find(A,A[X]);      }void unionSet(int A[],int X,int Z){//add code here.int XRep=find(A,X);int ZRep = find(A,Z);A[XRep] = ZRep;}
0
Stangly chang9 months ago
Stangly chang
int find(int A[],int X){       //add code here       if(A[X]==X)  return X;       else return find(A,A[X]);}void unionSet(int A[],int X,int Z){//add code here.int xRep = find(A,X);int zRep = find(A,Z);A[xRep] = zRep;}
0
Ananya Ananth9 months ago
Ananya Ananth
python3 solutiondef find(A, X):    for i,a in enumerate(A):        if type(a) is list:            for x in a:                if x==X:                    return i+1        if X == a:            return i+1# function shouldn't return or print anythingdef unionSet(A, X, Z):    if X==Z :        pass    else:        i=find(A,Z)-1        j=find(A,X)-1        if i!=j:            if type(A[i]) is not list:                A[i]=[A[i]]            try:                A[i]=A[i]+A[j]            except:                A[i].append(A[j])            try:                A[j].remove(A[j])                A[j]=0            except:                A[j]=0
0
Vikash Maddi9 months ago
Vikash Maddi
 simple code - 0.0 /1.4 int find(int A[],int x){     while(A[x] != x){         int k = A[x];         A[x] = A[A[x]];         x = k;     }     return A[x];}void unionSet(int A[],int x,int z){    x=find(A,x);z=find(A,z);if(x==z) return;A[x]=z;}
+2
coderPlusTester9 months ago
coderPlusTester
Piece of advice, CPP Programmers. The Driver code is assuming 1-index array and not 0-index.
0
Prakhar Shukla10 months ago
Prakhar Shukla
In python. The driver code enters the array with 0 based indexing, so I think we have to convert it into 1 based indexing first before using recursion for  find method! Make sure you compare x with arr[x-1] andnot arr[x]..Peace!
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.