Today I had CodeNation coding round (on-campus):

**question1** : Give an array $$$a$$$ of size $$$n$$$ where $$$1<=n<=1e5$$$ and $$$1<=a_i<=2e5$$$, we need to find out for each $$$i$$$ number of other $$$j$$$ such that either $$$a_imoda_j=0$$$ or $$$a_jmoda_i=0$$$.

Example : 4 2 1 3

output : 2 2 3 1

We can solve it in nlogn by using concept of harmonic series.

**question2** : given tree of size $$$n$$$, $$$1<=n<=1e5$$$ indexed from 0,rooted at node 0 and each node having distinct value from 0 till n-1. Find mex of each subtree.

Example : n=3 , edges : (0,1),(0,2) and values : v[0]=0, v[1]=1, v[2]=2. Output : mex[0]=3,mex[1]=0,mex[2]=0.

Was able to solve partially in $$$O(n*n)$$$. can someone tell how to solve in better complexity ?

**question3** : Given an array $$$a$$$ of size $$$n$$$, beauty from $$$l$$$ to $$$r$$$ is defined as $$$a_l+(-1)*a_{l+1}+a_{l+2}+(-1)*a_{l+3}....$$$repeating till r. $$$1<=n<=1e4$$$. There are $$$Q$$$ queries, $$$1<=Q<=1e5$$$. Either 1 i x, which means update a[i]=x, or 2 L R, which means maximum beauty among all subarrays in index range L to R. Note that $$$-1e9<=x,a[i]<=1e9$$$.

Example :n=2 a: 4 -2 7, two queries 1 2 1e9 and 2 1 3. answer for query is 7.

How to solve 2nd and 3rd one ?