What is the issue with my code? It only passes 6 of my testcases and I am sure I am making some mistake but I am not able to figure it out. Also if someone could help me understand the space and time complexity of this code I would be more than grateful. Thank You.
def arrayManipulation(n, queries):
arr = [0]*(n+2)
for q in queries:
print(q)
a = q[0]
b = q[1]
k = q[-1]
arr[a] += k
arr[b+1] -= k
print(arr)
max_sum = temp = 0
for i in arr:
print(i)
temp += i
print("TEMP:",temp)
max_sum = max(max_sum,temp)
print("MAXSUM:",max_sum)
return max_sum
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nm = input().split()
n = int(nm[0])
m = int(nm[1])
queries = []
for _ in range(m):
queries.append(list(map(int, input().rstrip().split())))
result = arrayManipulation(n, queries)
fptr.write(str(result) + '\n')
fptr.close()
The problem is within following part of your code:
max_sum and temp are both integer type which can only handle number up to 2147483647 before overflow. But the question specify that k is within range of 0 to 10^9, when those number adds up it will easily overflow. Try to use long instead of int.