Can anyone help me with the following code:
In the code I am traversing the array and storing the all the max length of subarrays that are in strictly increasing sequence into a list called lis. Then I am finding the max of sum of length of 2 consecutive subarrays in increasing sequence.
For example:
- if input array = [1, 2, 2, 1] then lis array = [2, 1, 1] as the subarrays with increasing sequence are [1,2], [2] and [1] and max of sum of 2 consecutive elements is 3.
- If input array = [1, 2, 4, 3, 9, 7, 16, 12, 5], then lis = [3, 2, 2, 1, 1] as the subarrays with increasing sequence are [1, 2, 4], [3, 9], [7, 16]. [12] and [5]. Then taking a max of sum of 2 consecutive elements which is 5 in this case.
But I am getting wrong answer for it although it passing the sample test cases. Please help me in understanding the incorrectness in logic/code.
tc = int(input())
for i in range(tc):
n = int(input())
arr = list(map(int, input().split()))
lis = []
count = 0
for j in range(n):
if j == 0:
count += 1
elif arr[j - 1] < arr[j]:
count += 1
else:
lis.append(count)
count = 1
lis.append(count)
max_sum = lis[0]
for j in range(len(lis) - 1):
if lis[j] + lis[j + 1] > max_sum:
max_sum = lis[j] + lis[j + 1]
print(max_sum)