Max Contiguous Sequences
Coding Problem Keys
Max Contiguous Sequences
Problem Statement
Given an array of N integers, determine all the contiguous subsequences of positive numbers. Then write a function to find the sum of elements in each sub-sequence and output the maximum sum values.
Boundary Condition(S)
1 <= R <= N <= 100
1 <= ID of each item <= 1000
Input Specification
input1: An integer N denoting the length of the array
input2: An integer array of length N (-10000 <= A[i] <= 10000)
Output Specification
Return the maximum sum of contiguous sub-sequence of numbers.
Example Input/Output 1
Input
input1: 5
input2: {1, 2, 4, -2, 3}
Output
output: 7
Explanation
Contiguous sub-sequence of positive numbers in the given array are {1, 2, 4} and {3}.
Their respective sums are 7 and 3. Hence, the maximum value between them is 7.
Hence the output is 7.
Solution
Programming Language: Python 3 Language
def FindContigiousSeq(input1,input2):
arr=[int(i) for i in input2]
res=min(arr)
sm=0
f=0
for i in arr:
if(i<0):
res=max(res,sm)
sm=0
else:
f=1
sm+=i
if(f==1):
return res
return min(arr)
n=input()
arr=input().split()
print(FindContigiousSeq(n,arr))
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments