2
int[] par, size, count, bits;4
// par: parent array, tells about whose it the parent of ith element5
// size: it tells the size of component6
// count: it tells the count of islands (1111 etc) of size i;7
// count[3] = 4: ie -> there are 4 islands of size 39
public int find(int u) {10
if (u == par[u]) return u;11
par[u] = find(par[u]);15
public void union(int u, int v) {16
// union is performed over parents of elements not nodes itself17
int p1 = find(u), p2 = find(v);20
// decrease the count of islands of size p1, p230
// adjust the count of islands of new size ie: size of p134
public int findLatestStep(int[] arr, int m) {37
size = new int[n + 1];38
count = new int[n + 1];39
bits = new int[n + 2];41
for (int i = 0; i < n; i++) {47
for (int i = 0; i < n; i++) {51
// increase the count of islands of size 154
if (bits[idx - 1] > 0) {57
if (bits[idx + 1] > 0) {61
// check if island of size m exists64
// as it is 1 based indexing