VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-number-of-unique-elements-in-an-array-after-each-query/

⇱ Find Number of Unique Elements in an Array After each Query - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find Number of Unique Elements in an Array After each Query

Last Updated : 23 Jul, 2025

Given 2d array A[][1] of size N and array Q[][2] of size M representing M queries of type {a, b}. The task for this problem is in each query move all elements from A[a] to A[b] and print the number of unique elements in A[b].

Constraints:

  • 1 <= N, Q <= 105
  • 1 <= A[i] <= 109
  • 1 <= a, b <= N

Examples:

Input: A[][1] = {{1}, {1}, {1}, {2}, {2}, {3}}, Q[][2] = {{1, 2}, {6, 4}, {5, 1}, {3, 6}, {4, 6}}
Output: 1 2 1 1 3
Explanation:

  • For First query {1, 2} move all elements from A[1] to A[2]. A becomes {{}, {1, 1}, {1}, {2}, {2}, {3}}. Number of unique elements in A[2] is 1.
  • For Second query {6, 4} move all elements from A[6] to A[4]. A becomes {{}, {1, 1}, {1}, {2, 3}, {2}, {}}. Number of unique elements in A[4] is 2.
  • For Third query {5, 1} move all elements from A[5] to A[1]. A becomes {{2}, {1, 1}, {1}, {2, 3}, {}, {}}. Number of unique elements in A[1] is 1.
  • For Fourth query {3, 6} move all elements from A[3] to A[6]. A becomes {{2}, {1, 1}, {}, {2, 3}, {}, {1}}. Number of unique elements in A[6] is 1.
  • For Fifth query {4, 6} move all elements from A{4] to A[6]. A becomes {{2}, {1, 1}, {}, {}, {}, {1, 2, 3}}. Number of unique elements in A[6] is 3.

Input: A[][1] = {{2}, {4}, {2}, {4}, {2}}, Q[][2] = {{3, 1}, {2, 5}, {3, 2}}
Output: 1 2 0
Explanation:

  • For First query {3, 1} move all elements from A[3] to A[1]. A becomes {{2, 2}, {4}, {}, {4}, {2}}. Number of unique elements in A[1] is 1.
  • For Second query {2, 5} move all elements from A[2] to A[5]. A becomes {{2, 2}, {}, {}, {4}, {2, 4}}. Number of unique elements in A[5] is 2.
  • For Third query {3, 2} move all elements from A[3] to A[2]. A becomes {{2, 2}, {}, {}, {4}, {2, 4}}. Number of unique elements in A[2] is 0.

Efficient Approach: To solve the problem follow the below idea.

This problem is an application of the “union-by-size / small-to-large heuristic”. This algorithm depends upon fact that swapping index of sets take place in constant time, and merging a smaller set of size M into a larger set of size N takes O(MlogN). When merging sets, always move from a smaller set to a larger set. So whenever we need to move some elements from larger to smaller set we just move smaller size set elements to larger size set elements and swap their indexes.

Below are the steps for the above approach:

  • Declaring vector of sets V[N].
  • Create an HashMap for dealing with the indexes.
  • Insert all N arrays from A[][1] to N sets and initialize index map ind.
  • Iterate for M queries and for each query
  • Declare two variables x(which stores set which is to be moved) and y(which will take all incoming elements from other set).
  • whenever we find elements in the set [x] is less than set[y] we insert elements in y set and if it is greater than set[y] then we insert all element in set [x] and swap their indexes.
  • At the end of each query print the size of set V[ind[y]].

Below is the implementation of the above approach:


Output
1 2 1 1 3 

Time Complexity: O(Nlog2N)
Auxiliary Space: O(N)

Comment