123k views
3 votes
A data analyst recently joined HackerRank as an intern. Example Consider n=4 , data =[1,-4,-5,2], k=2 and updates =[[2,4],[1,2]] 1

"

User Grimxn
by
8.4k points

1 Answer

3 votes

The code below is used in iterating through the updates and applying them to the original data.

python

def apply_updates(data, updates):

for update in updates:

l, r = update

data[l-1:r] = [-x for x in data[l-1:r]] # Negate the subarray from index l to r

# Example usage:

initial_data = [1, 2, 3, 4]

updates = [[2, 4]]

apply_updates(initial_data, updates)

print("Final Data:", initial_data)

So, the above example is one that uses the given initial data [1, 2, 3, 4] and updates it based on the given update [2, 4]. The apply_updates function is one that takes the initial data and updates as input, and it modifies the data in-place according to the specified updates.

Language Python 3

Environment

Auto

6. Data Updates

A data analyst recently joined HackerRank as an intern.

As an initial task, data for n days is provided to the intern. Then, k updates are performed on the data, where each update is of the form [l, r]. This indicates that the subarray of data starting at index /and ending at index r is negated. For example, if data = [1, 2, 3, 4] and the updates are [2,4] then the data becomes data = [1, -2, -3, -4].

Given the initial data and k updates, find the final data after all updates.

Note: 1-based indexing is used.

Example

1 >#!/bin/python3-

10

# Complete the 'getFinalData' function below.

11

#

12

13

#

14

15

# The function is expected to return an INTEGER_ARRAY.

# The function accepts following parameters:

16

#

1. INTEGER_ARRAY data

17

#

2. 2D INTEGER_ARRAY updates

18

#

19

20

21

def getFinalData (data, updates): # Write your code here

22

23 > if

name

main__':

56

Consider n = 4, data = [1, -4, -5, 2], k = 2 and updates = [[2,4], [1, 2]].

7

8

9

1. After the first update, the data becomes data = [1, 4, 5, -2].

2. After the second update, the data becomes data = [-1,-4, 5, -2].

The final data is [-1, -4, 5, -2].

A data analyst recently joined HackerRank as an intern. Example Consider n=4 , data-example-1
User Jacob Amos
by
7.4k points