given n and read the next n lines as integers and convert n integers as array. calculate the sum of its prefixes at each index and return the number of positive sum of prefixes.
For Example :
n = 4, next n lines 1, 3, 0, -5
read inputs as array = [-9,3,0,1,-1]
converts into _____ format Like
[3, 1, 0, -9, -1]
Then calculate sum of prefixes like
[3, 4, 4, -5,-6]
Now count the positive numbers above the list is 3.
so output is 3
a = []
for i in range(int(input('Enter n: '))):
a.append(int(input()))
res1 = [i for i in a if i > -1]
res2 = [i for i in a if i < 0]
a = sorted(res1, reverse=True) + sorted(res2)
print(f'Converted Array: {a}')
result = []
result.append(a[0])
for i in range(1, len(a)):
result.append(result[-1] + a[i])
print(f'Prefix sums {result}')
print(f'{len([i for i in result if i >= 0])} positive numbers')
Comments
Leave a comment