Given an array A[] of size N, return length of the longest subarray of non- negative integers.
Note: Subarray here means a continuous part of the array.
 
Example 1:
Input : 
N = 9
A[] = {2, 3, 4, -1, -2, 1, 5, 6, 3}
Output : 
4
Explanation :
The subarray [ 1, 5, 6, 3] has longest length 4 and
contains no negative integers
 
Example 2:
Input : 
N = 10
A[] = {1, 0, 0, 1, -1, -1, 0, 0, 1, 0}
Output : 
4
Explanation :
Subarrays [1, 0, 0, 1] and [0, 0, 1, 0] have
equal lengths but sum of first one is greater
so that will be the output.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function longestSubarray() which takes the array A[] and its size N as inputs and returns the length of the longest subarray of non-negative integers.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 10^{5}
-10^{5 }<= A[i] <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Amortized analysis

Starter code: #User function Template for python3

def longestSubarry( A, N):
    
    
    

Solutions:
def longestSubarry(A, N):
	A.append(-1)
	curr = 0
	ans = 0
	for x in A:
		if x <= 0:
			curr = 0
		else:
			curr += 1
		ans = max(curr, ans)
	return ans


def longestSubarry(A, N):
	(cnt, c) = (0, 0)
	for i in A:
		if i >= 0:
			c += 1
		else:
			cnt = max(cnt, c)
			c = 0
	return max(cnt, c)


def longestSubarry(A, N):
	(ans, cur) = (0, 0)
	for i in A:
		if i >= 0:
			cur += 1
		else:
			cur = 0
		ans = max(ans, cur)
	return ans


def longestSubarry(A, N):
	mx = 0
	ln = 0
	for i in range(N):
		if A[i] < 0:
			mx = max(ln, mx)
			ln = 0
		else:
			ln += 1
	return max(mx, ln)


def longestSubarry(A, N):
	maxi = 0
	c = 0
	for i in range(N):
		if A[i] >= 0:
			c = c + 1
			maxi = max(c, maxi)
		else:
			c = 0
	return maxi


from sys import maxsize

def longestSubarry(a, n):
	mx = -maxsize - 1
	cur = 0
	ind = 0
	end = 0
	t = 0
	for i in range(n):
		if a[i] < 0:
			if mx < cur:
				mx = cur
			t = i + 1
			end = i - 1
			ind = i + 1
			cur = 0
		else:
			cur = i - ind + 1
			if mx < cur:
				mx = cur
				ind = t
				end = i
	return mx


def longestSubarry(A, N):
	a = []
	a1 = []
	for i in A:
		if i >= 0:
			a1.append(i)
		else:
			a.append(a1)
			a1 = []
	a.append(a1)
	c = 0
	for i in a:
		d = len(i)
		if c < d:
			c = d
	return c


def longestSubarry(A, N):
	i = 0
	j = N - 1
	flag = False
	ans = 0
	temp = 0
	while i <= j:
		if flag == False:
			if A[i] >= 0:
				temp += 1
			else:
				temp = 0
				flag = True
			i += 1
		else:
			if A[j] >= 0:
				temp += 1
			else:
				temp = 0
				flag = False
			j -= 1
		ans = max(ans, temp)
	return ans


def longestSubarry(A, N):
	b = 0
	c = 0
	for i in range(len(A)):
		if A[i] >= 0:
			c = c + 1
		else:
			b = max(b, c)
			c = 0
	return max(b, c)


def longestSubarry(A, N):
	(temp1, temp2, counter) = (0, 0, 0)
	globalcounter = 0
	for i in range(N):
		temp1 = temp2 + A[i]
		if temp1 >= temp2:
			temp2 = temp1
			counter = counter + 1
		else:
			temp2 = 0
			temp1 = 0
			if counter > globalcounter:
				globalcounter = counter
			counter = 0
	if counter > globalcounter:
		globalcounter = counter
	return globalcounter


def longestSubarry(a, n):
	m = 0
	m1 = 0
	for i in range(n):
		if a[i] > 0:
			m1 += 1
		if a[i] < 0:
			m = max(m1, m)
			m1 = 0
	return max(m1, m)


def longestSubarry(A, N):
	count = 0
	maxi = 0
	for i in range(len(A)):
		if A[i] >= 0:
			count = count + 1
			maxi = max(maxi, count)
		elif A[i] < 0:
			count = 0
	return maxi


def longestSubarry(A, N):
	m = 0
	b = []
	for i in range(len(A)):
		if A[i] >= 0:
			m += 1
			b.append(m)
		elif A[i] < 0:
			b.append(m)
			m = 0
	return max(b)


def longestSubarry(A, N):
	i = 0
	j = N - 1
	c = 0
	maxc = 0
	while i <= j:
		if A[i] >= 0:
			c += 1
			i += 1
		elif A[i] < 0:
			maxc = max(maxc, c)
			c = 0
			i += 1
	maxc = max(maxc, c)
	return maxc


def longestSubarry(A, N):
	count = 0
	maxi = 0
	for item in A:
		if item >= 0:
			count += 1
			maxi = max(maxi, count)
		else:
			count = 0
	return maxi


def longestSubarry(A, N):
	max = 0
	count = 0
	for i in range(N):
		if A[i] >= 0:
			count += 1
		else:
			count = 0
		if count >= max:
			max = count
	return max


def longestSubarry(A, N):
	i = 0
	j = 0
	count = 0
	max_ = 0
	while j < N:
		if A[j] >= 0:
			count += 1
		else:
			count = 0
			i = j
		j += 1
		max_ = max(count, max_)
	return max_


def longestSubarry(A, N):
	l = 0
	m = 0
	curr = 0
	for r in range(N):
		if A[r] >= 0:
			curr += 1
			m = max(m, curr)
		else:
			curr = 0
	return m


def longestSubarry(A, N):
	ma = 0
	i = 0
	while i < N:
		cur = 0
		while i < N and A[i] >= 0:
			cur += 1
			i += 1
		ma = max(ma, cur)
		i += 1
	return ma


def longestSubarry(A, N):
	u = []
	c = 0
	for i in range(len(A)):
		if A[i] >= 0:
			c += 1
		elif A[i] < 0:
			u.append(c)
			c = 0
	u.append(c)
	return max(u)


def longestSubarry(arr, N):
	count = 0
	maximum = 0
	for i in range(N):
		if arr[i] >= 0:
			count += 1
		else:
			maximum = max(maximum, count)
			count = 0
	if count > 0:
		maximum = max(maximum, count)
	return maximum


def longestSubarry(A, N):
	(sum1, sum2, max1) = (0, A[0], 0)
	if A[0] > 0:
		curr = 1
	else:
		curr = 0
	for i in range(N - 1):
		sum1 += A[i]
		sum2 += A[i + 1]
		if sum2 > sum1:
			curr += 1
			max1 = max(curr, max1)
		else:
			max1 = max(curr, max1)
			curr = 0
	return max1


def longestSubarry(A, N):
	max_len = 0
	curr_len = 0
	for n in A:
		if n > 0:
			curr_len += 1
		else:
			curr_len = 0
		if curr_len > max_len:
			max_len = curr_len
	return max_len


def longestSubarry(A, N):
	maxL = 0
	j = 0
	for i in range(N):
		if A[i] < 0 or i == N - 1:
			l = i - j
			if i == N - 1 and (not A[i] < 0):
				l += 1
			if maxL < l:
				maxL = l
			j = i + 1
	return maxL


def longestSubarry(A, N):
	l = 0
	ans = 0
	for i in range(N):
		if A[i] >= 0:
			l += 1
			ans = max(ans, l)
		else:
			l = 0
	return ans


def longestSubarry(A, N):
	(i, j) = (0, 0)
	mx = float('-inf')
	while i < N and j < N:
		if A[j] >= 0:
			j += 1
			continue
		mx = max(mx, j - i)
		j += 1
		i = j
	mx = max(mx, j - i)
	return mx


def longestSubarry(A, N):
	Maxlen = 0
	curlen = 0
	for i in range(N):
		if A[i] < 0:
			curlen = 0
		else:
			curlen += 1
			Maxlen = max(Maxlen, curlen)
	return Maxlen


def longestSubarry(A, N):
	count = 0
	res = 0
	for i in range(N):
		if A[i] < 0:
			count = 0
			continue
		count += 1
		res = max(res, count)
	return res


def longestSubarry(A, N):
	l = 0
	res = 0
	cur = 0
	for i in range(N):
		if A[i] >= 0:
			cur += 1
			res = max(res, cur)
		else:
			l = i
			l += 1
			cur = 0
	res = max(res, cur)
	return res


def longestSubarry(A, N):
	c = 0
	e = []
	for i in A:
		if i > 0:
			c += 1
		else:
			c = 0
		e.append(c)
	return max(e)


def longestSubarry(A, N):
	maxSize = 0
	i = 0
	size = 0
	while i < N:
		if A[i] < 0:
			if maxSize < size:
				maxSize = size
			size = 0
		else:
			size += 1
		i += 1
	if maxSize < size:
		maxSize = size
	return maxSize


def longestSubarry(A, N):
	count = 0
	maxx = 0
	for i in A:
		if i >= 0:
			count += 1
		maxx = max(maxx, count)
		if i < 0:
			count = 0
	return maxx


def longestSubarry(a, n):
	intialval = 0
	max1 = 0
	res = [0]
	for i in range(0, n):
		if a[i] >= 0:
			intialval += 1
		else:
			res.append(intialval)
			intialval = 0
	res.append(intialval)
	return max(res)


def longestSubarry(A, N):
	subarr = 0
	counter = 0
	for i in A:
		if i < 0:
			counter = 0
			continue
		else:
			counter += 1
		subarr = max(subarr, counter)
	return subarr


def longestSubarry(arr, n):
	count = 0
	curr_count = 0
	i = 0
	while n > i:
		if arr[i] > 0:
			curr_count += 1
			count = max(curr_count, count)
		else:
			curr_count = 0
		i += 1
		count = max(curr_count, count)
	return count


def longestSubarry(A, N):
	d = 0
	c = 0
	for i in A:
		if i >= 0:
			c += 1
		else:
			if c > d:
				d = c
			c = 0
	if c > d:
		d = c
	return d


def longestSubarry(A, N):
	m = 0
	l = []
	for i in A:
		if i >= 0:
			l.append(i)
		else:
			l = []
		m = max(m, len(l))
	return m


def longestSubarry(A, N):
	(i, j) = (0, 0)
	maxLen = 0
	while j < len(A):
		if A[j] >= 0:
			maxLen = max(maxLen, j - i + 1)
			j += 1
		else:
			j += 1
			i = j
	return maxLen


def longestSubarry(A, N):
	i = 0
	j = 0
	count = 0
	while j < N:
		if A[j] >= 0:
			j += 1
			temp = j - i
			if temp > count:
				count = temp
			continue
		if A[j] < 0:
			i = j
			i += 1
			j += 1
	return count


def longestSubarry(A, N):
	currlen = 0
	maxlen = 0
	for i in range(N):
		if A[i] >= 0:
			currlen += 1
			maxlen = max(maxlen, currlen)
		else:
			currlen = 0
	return maxlen


def longestSubarry(e, N):
	i = 0
	j = 0
	mx = 0
	(x, y) = (0, 0)
	while i < len(e) and j < len(e):
		if e[j] >= 0:
			j = j + 1
			sol = j - i + 1
			if sol > mx:
				mx = sol
				(x, y) = (j, i)
		else:
			j = j + 1
			i = j
	return x - y


def longestSubarry(arr, N):
	p = cnt = maxi = 0
	while p < len(arr):
		if arr[p] >= 0:
			cnt += 1
			p += 1
		elif arr[p] < 0:
			maxi = max(cnt, maxi)
			cnt = 0
			p += 1
	maxi = max(cnt, maxi)
	return maxi


def longestSubarry(A, N):
	ptr1 = 0
	ptr2 = 0
	size = 0
	mx = 0
	for i in range(N):
		if A[i] < 0:
			ptr2 = ptr1
			size = 0
		else:
			ptr1 += 1
			size = ptr1 - ptr2
			mx = max(mx, size)
	return mx


def longestSubarry(arr, N):
	ans = 0
	i = 0
	c = 0
	while i < len(arr):
		if arr[i] >= 0:
			c += 1
		else:
			ans = max(ans, c)
			c = 0
		i += 1
	ans = max(ans, c)
	return ans


def longestSubarry(A, N):
	maxx = -float('inf')
	count = 0
	for i in range(N):
		if A[i] >= 0:
			count += 1
		else:
			if maxx < count:
				maxx = count
			count = 0
	if maxx < count:
		maxx = count
	return maxx


def longestSubarry(arr, N):
	count = 0
	res = 0
	for num in arr:
		if num >= 0:
			count += 1
		else:
			count = 0
		res = max(res, count)
	return res


def longestSubarry(A, N):
	res = 0
	temp = 0
	for i in A:
		if i >= 0:
			temp += 1
			res = max(temp, res)
		else:
			temp = 0
	return res


def longestSubarry(A, N):
	s = ans = l = 0
	while s < N:
		if A[s] >= 0:
			l += 1
			s += 1
			ans = max(ans, l)
		else:
			l = 0
			s += 1
	return ans


def longestSubarry(A, N):
	maxLen = 0
	leng = 0
	(l, r) = (0, 0)
	while r < N:
		if A[r] < 0:
			leng = 0
			l = r
		else:
			leng += 1
		if maxLen < leng:
			maxLen = leng
		r += 1
	return maxLen


def longestSubarry(A, N):
	l = []
	count = maxi = 0
	for i in A:
		if i >= 0:
			count += 1
			maxi = max(maxi, count)
		else:
			count = 0
	return maxi


def longestSubarry(A, N):
	result = -1
	streak = 0
	for i in range(N):
		if A[i] < 0:
			result = max(result, streak)
			streak = 0
		else:
			streak += 1
	return max(result, streak)

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		a = [int(x) for x in input().strip().split()]
		print(longestSubarry(a, n))
		T -= 1
if __name__ == '__main__':
	main()


def longestSubarry(arr, N):
	l = 0
	ans = 0
	cnt = 0
	while l < len(arr):
		if arr[l] >= 0:
			cnt += 1
			l += 1
		else:
			ans = max(ans, cnt)
			cnt = 0
			l += 1
	ans = max(ans, cnt)
	return ans

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		a = [int(x) for x in input().strip().split()]
		print(longestSubarry(a, n))
		T -= 1
if __name__ == '__main__':
	main()


def longestSubarry(arr, N):
	j = 0
	maxi = 0
	count = 0
	for i in range(len(arr)):
		if arr[i] < 0:
			j = i + 1
			maxi = max(maxi, count)
			count = 0
		else:
			count += 1
	maxi = max(maxi, count)
	return maxi

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		a = [int(x) for x in input().strip().split()]
		print(longestSubarry(a, n))
		T -= 1
if __name__ == '__main__':
	main()


def longestSubarry(A, N):
	maxC = 0
	curr = 0
	for i in A:
		if i > 0:
			curr += 1
		elif i < 0 and curr > 0:
			maxC = curr if curr > maxC else maxC
			curr = 0
	if curr > maxC:
		maxC = curr
	return maxC

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		a = [int(x) for x in input().strip().split()]
		print(longestSubarry(a, n))
		T -= 1
if __name__ == '__main__':
	main()


def longestSubarry(a, N):
	c = 0
	maxi = 0
	for i in range(N):
		if a[i] >= 0:
			c += 1
		elif a[i] < 0:
			c = 0
		maxi = max(maxi, c)
	return maxi

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		a = [int(x) for x in input().strip().split()]
		print(longestSubarry(a, n))
		T -= 1
if __name__ == '__main__':
	main()


def first(A, i):
	for j in range(i, len(A)):
		if A[j] > 0:
			return j
	return -1

def longestSubarry(A, N):
	p1 = first(A, 0)
	l = -1
	for i in range(p1, len(A)):
		if A[i] < 0 or (i == len(A) - 1 and A[i] > 0):
			if i == len(A) - 1 and A[i] > 0:
				i += 1
			if i - p1 > l:
				l = i - p1
			p1 = first(A, i)
			if p1 == -1:
				break
			i = p1
	return l


def longestSubarry(arr, n):
	s = 0
	e = 0
	count = 0
	max_count = 0
	for i in range(n):
		if arr[i] > 0:
			count += 1
		else:
			count = 0
		max_count = max(count, max_count)
	return max_count


def longestSubarry(arr, N):
	l = 0
	r = 0
	lnn = 0
	while l < len(arr) and r < len(arr):
		if arr[r] >= 0:
			r += 1
		elif arr[r] < 0:
			a = r - l
			lnn = max(lnn, a)
			l = r + 1
			r += 1
	lnn = max(lnn, r - l)
	return lnn


def longestSubarry(A, N):
	maxSoFar = 0
	maxEndingHere = 0
	for i in range(0, N):
		num = A[i]
		if num < 0:
			maxSoFar = max(maxEndingHere, maxSoFar)
			maxEndingHere = 0
		else:
			maxEndingHere += 1
	maxSoFar = max(maxEndingHere, maxSoFar)
	return maxSoFar


def longestSubarry(a, n):
	maxans = -1000000
	i = 0
	while i < n:
		if a[i] >= 0:
			count = 0
			while i < n and a[i] >= 0:
				count += 1
				maxans = max(maxans, count)
				i += 1
		i += 1
	if maxans == -1000000:
		return 0
	return maxans


def longestSubarry(arr, N):
	(i, j) = (0, 0)
	c = 0
	maxi = 0
	while j <= N - 1:
		if arr[j] >= 0:
			c += 1
			j += 1
		else:
			maxi = max(c, maxi)
			c = 0
			j += 1
	maxi = max(c, maxi)
	return maxi


def longestSubarry(A, N):
	maxi_res = 0
	maxi_curr = 0
	for i in range(len(A)):
		if A[i] > 0:
			maxi_curr += 1
		else:
			maxi_curr = 0
		maxi_res = max(maxi_res, maxi_curr)
	return maxi_res


def longestSubarry(A, N):
	count = 0
	ans = 0
	for x in A:
		if x >= 0:
			count += 1
		else:
			ans = max(ans, count)
			count = 0
	return max(ans, count)


def longestSubarry(A, N):
	count = 0
	right = 0
	max_arr = 0
	while right < N:
		if A[right] < 0:
			max_arr = max(max_arr, count)
			count = 0
			right += 1
		else:
			count += 1
			right += 1
			if right == N:
				max_arr = max(max_arr, count)
	return max_arr


def longestSubarry(arr, N):
	slow = 0
	fast = 0
	maxer = 0
	while fast < len(arr):
		if arr[fast] < 0:
			maxer = max(maxer, fast - slow)
			slow = fast + 1
		maxer = max(maxer, fast - slow + 1)
		fast += 1
	return maxer

def solOne(arr, N):
	slow = 0
	fast = 0
	l = []
	maxer = 0
	while fast < len(arr):
		l.append(arr[fast])
		if l and l[-1] < 0:
			while l and l[-1] < 0:
				del l[0]
				slow += 1
		maxer = max(len(l), maxer)
		fast += 1
	return maxer


def longestSubarry(A, N):
	count = 0
	max_count = 0
	for i in range(N):
		if A[i] > 0:
			count += 1
		if A[i] < 0 or i == N - 1:
			max_count = max(max_count, count)
			count = 0
	return max_count


def longestSubarry(A, N):
	ls_sub = 0
	max_sub = 0
	for i in range(0, N):
		if A[i] >= 0:
			ls_sub += 1
			max_sub = max(max_sub, ls_sub)
		if A[i] < 0:
			ls_sub = 0
	return max_sub


def longestSubarry(A, N):
	(curr_length, max_length) = (0, 0)
	for idx in range(N):
		if A[idx] >= 0:
			curr_length += 1
			max_length = max(max_length, curr_length)
		else:
			curr_length = 0
	return max_length


def longestSubarry(a, n):
	max_current_window = 0
	res = 0
	for i in range(n):
		if a[i] >= 0:
			max_current_window += 1
			res = max(max_current_window, res)
		else:
			max_current_window = 0
	return res


def longestSubarry(A, N):
	max_len = 0
	i = 0
	j = 0
	while j < N:
		if A[j] > -1:
			max_len = max(max_len, j - i + 1)
		else:
			i = j + 1
		j += 1
	return max_len


def longestSubarry(A, N):
	m = -1
	for i in range(N):
		if A[i] >= 0:
			break
	c = 0
	for i in range(i, N):
		if A[i] >= 0:
			c += 1
		else:
			m = max(c, m)
			c = 0
	m = max(c, m)
	return m


def longestSubarry(A, N):
	cur_sum = 0
	max_sum = 0
	for i in range(N):
		if A[i] > 0:
			cur_sum = cur_sum + 1
			max_sum = max(max_sum, cur_sum)
		else:
			cur_sum = 0
	return max_sum


def longestSubarry(A, N):
	(i, j) = (0, 0)
	ans = 0
	while j < N:
		while j < N and A[j] >= 0:
			j += 1
		ans = max(ans, j - i)
		while j < N and A[j] < 0:
			j += 1
		i = j
	return ans


def longestSubarry(a, n):
	max_ = 0
	current = 0
	for i in a:
		if i < 0:
			current = 0
		else:
			current += 1
			max_ = max(current, max_)
	return max_


def longestSubarry(A, N):
	c = 0
	l = []
	for i in A:
		if i >= 0:
			c += 1
			l.append(c)
		elif i < 0:
			c = 0
	if len(l) > 0:
		return max(l)
	else:
		return 0