Array Manipulation Hackerrank solution in Python | Hack With Infy previous year questions | Infosys

Опубликовано: 18 Апрель 2020
на канале: Engineers Revolution
2,005
18

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in your array.

For example, the length of your array of zeros . Your list of queries is as follows:

a b k
1 5 3
4 8 7
6 9 1
Add the values of between the indices and inclusive:

index- 1 2 3 4 5 6 7 8 9 10
[0,0,0, 0, 0,0,0,0,0, 0]
[3,3,3, 3, 3,0,0,0,0, 0]
[3,3,3,10,10,7,7,7,0, 0]
[3,3,3,10,10,8,8,8,1, 0]
The largest value is after all operations are performed.

Function Description

Complete the function arrayManipulation in the editor below. It must return an integer, the maximum value in the resulting array.

arrayManipulation has the following parameters:

n - the number of elements in your array
queries - a two dimensional array of queries where each queries[i] contains three integers, a, b, and k.
Input Format

The first line contains two space-separated integers and , the size of the array and the number of operations.
Each of the next lines contains three space-separated integers , and , the left index, right index and summand.

Constraints

Output Format

Return the integer maximum value in the finished array.
Sample Input
5 3
1 2 100
2 5 100
3 4 100
Sample Output
200
Explanation
After the first update list will be 100 100 0 0 0.
After the second update list will be 100 200 100 100 100.
After the third update list will be 100 200 200 200 100.
The required answer will be .
Array Manipulation - Hacker Rank Solution
You are given a list of size n, initialized with zeroes. You have to perform m queries on the list and output the maximum of final values of all the n elements in the list. For every query, you are given three integers a, b and k and you have to add value k to all the elements ranging from index a to b(both inclusive).
Sub-Optimal Brute Force:
Given each update , for each index in the range from [, ], add the value to each number in the range.
The final step is to go through the whole array and find the maximum value and print that maximum value.
The complexity of this solution is O() which is too high to pass in time.
Optimal:
Given a range[, ] and a value we need to add to all the numbers whose indices are in the range from [, ].
We can do an O() update by adding to index and add to index .
Doing this kind of update, the number in the array will be prefix sum of array from index 1 to i because we are adding to the value at index and subtracting from the value at index and taking prefix sum will give us the actual value for each index after operations .
So, we can do all updates in O(m) time. Now we have to check the largest number in the original array. i.e. the index i such that prefix sum attains the maximum value.
We can calculate all prefix sums as well as maximum prefix sum in O(n) time which will execute in time.
Optimal:
This can be further optimized to run in O(m logm) time because we have to check the value of prefix sum at only indices. i.e. and values of all the updates.
We have, in total queries and each query has a range [, ] which needs to be updated. So, in total we have indices.
For each query, we can insert both and in an array and sort the array.
Now, we have to just take the prefix sum of the array and find the maximum element which will be our answer.
Check the setter's code for better understanding.
Note: If you thought of solving it using segment tree with lazy propogation, it won't pass here as can be as high as .


Смотрите видео Array Manipulation Hackerrank solution in Python | Hack With Infy previous year questions | Infosys онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Engineers Revolution 18 Апрель 2020, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 2,005 раз и оно понравилось 18 людям.