To push an element in queue, you have to shift all the existing elements to the right by 1 position, hence O(N) time.
You are using q2.push() which will take O(N) and a loop of O(M) is already there outside. Overall time complexity is O(N*M), then why did it not give TLE? Please explain me because the same approach in Python is giving TLE.
# cook your code heren = input()s = map(int, raw_input().split())nd = 0 #nesting depthmnd = 0 #maximum nesting depthmnp = 0 #maximum nesting depth positionms = 0 #middle symbolsmms = 0 #maximum middle symbolspp = -1 #previous position for maximum symbolcp = -1 #current position for maximum symbolmmp = 0 #middle symbol starting positionfor i in range(n):if s[i]==1:nd += 1if nd>mnd:mnd = ndmnp = i+1else:nd -= 1if nd==0:pp = cpcp = ims = cp - ppif ms>mms:mms = msmmp = pp + 2print mnd, mnp, mms, mmp