-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arrays-Array Manipulation.py
56 lines (41 loc) · 1.21 KB
/
Arrays-Array Manipulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#Problem Link : https://www.hackerrank.com/challenges/crush/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays
#Ans:
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'arrayManipulation' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts following parameters:
# 1. INTEGER n
# 2. 2D_INTEGER_ARRAY queries
#
def arrayManipulation(n, queries):
arr = [0]*(n+1)
max_value = 0
total_sum = 0
for query in queries:
l = query[0]
h = query[1]
val = query[2]
arr[l-1] = arr[l-1] + val
arr[h] = arr[h]-val
for value in arr:
total_sum = total_sum + value
max_value = max(max_value, total_sum)
return max_value
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
m = int(first_multiple_input[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()