Array Manipulation in Python - HackerRank Solution
Solution for hackerRank problem Array Manipulation in Python
Problem :
Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array.
Example,
n = 10
queries = [ [1, 5, 3], [4, 8, 7], [6, 9, 1] ]
Queries are interpreted as follows:
Add the values of k between the indices a and b inclusive:
The largest value is 10 after all operations are performed.
Function Description
Complete the function arrayManipulation in the editor below.
arrayManipulation has the following parameters:
- int n - the number of elements in the array
- int queries[q][3] - a two dimensional array of queries where each queries[i] contains three integers, a, b, and k.
Returns
- int - the maximum value in the resultant array
Input Format
The first line contains two space-separated integers n and m, the size of the array and the number of operations.
Each of the next m lines contains three space-separated integers a, b and k, the left index, right index and summand.
Constraints
- 3 <= n <= 10^7
- 1 <= m <= 2 * 10^5
- 1 <= a <=b <= n
- 0 <= k <=10^9
Sample Input
5 3
1 2 100
2 5 100
3 4 100
Sample Output
200
Explanation
After the first update the list is 100 100 0 0 0.
After the second update list is 100 200 100 100 100.
After the third update list is 100 200 200 200 100.
The maximum value is 200.
Code :
#!/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):
l = [0 for i in range(n+1)]
max_val=0
sum_val=0
for i in range(len(queries)):
start=queries[i][0]
end=queries[i][1]
value=queries[i][2]
l[start-1]+=value
l[end]-=value
for i in range(len(l)-1):
sum_val=sum_val+l[i]
if(sum_val>max_val):
max_val=sum_val
return max_val
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()
Comments
Post a Comment