ZZX has a sequence of boxes numbered 1,2,...,n1,2,...,n. Each box can contain at most one ball. You are given the initial configuration of the balls. For 1≤i≤n1≤i≤n, if the ii-th box is empty then a[i]=0a[i]=0, otherwise the i-th box contains exactly…