process Q queries of type l, r. For each query determine the number of unique integers appearing in the range from l to r in the sequence described

Viewed 17

enter image description here

process Q queries of type l, r. For each query determine the number of unique integers appearing in the range from l to r in the sequence described

1 Answers

answer for the question is as follows in python

    def solve(q,query):
    max_len=0
    for i in query:
        if max(i)>max_len:
            max_len=max(i)
    l=[]
    c=1
    output=[]
    while len(l)!=max_len:
        t=int(2*int(c**(1/2)))
        for k in range(t):
            l.append(c)
            if len(l)==max_len:
                break
        c=c+1
    for i in query:
        cut_l=l[i[0]-1:i[1]]
        num=len(set(cut_l))
        output.append(num)
    return output
    
q=3
query = [[1,10],[2,8],[1,15]]
out_=solve(q,query)
print(' '.join(map(str,out_)))
Related