Ignoring problems

Report a typo

Sometimes we don't have to solve all subproblems to get our solution. Let's consider an example.

Mark is a physicist, and he needs to measure some radius using a meter scale during an experiment. Accidentally, he has measured the first few objects using a centimeter scale. Now, the measured data looks like this: radius={200,450,370,240,2,6,3,7,3,4,2,6}radius = \{200, 450, 370, 240, 2, 6, 3, 7, 3, 4, 2, 6\}. Mark has to divide the first few data points (that are greater than 100) by 100. He has written this pseudocode using the divide and conquer paradigm to automate this job:

function update_radius(radius, start, end):
    if start == end - 1 then:
        radius[start] = radius[start] / 100
        return radius[start:end]
    if start < end then:
        mid = int((start + end) div 2)
        if radius[mid] < 100 then:
            left = update_radius(radius, start, mid)
            right = radius[mid:end]
        else:
            left = update_radius(radius, start, mid)
            right = update_radius(radius, mid, end)
        return combine(left, right)

Here int()int() function returns the largest integer less than the given number, i.e. for example, int(4.5)int(4.5) will return 4. The radius[start:end]radius[start:end] returns a subarray of the radiusradius array from the index startstart up to the index endend but excluding the index endend. The combine()combine() function combines the elements of two arrays into a single array.

If you examine Mark's pseudocode, you will notice that he hasn't used any recursion for the rightright subproblem if the first element of the right subarray is less than 100. Often, this kind of observation of data will improve the performance of our programs.

Now suppose one of Mark's students has some measurement data for the same experiment in the following array: {2,5,340,230,5,3,3,450}\{2, 5, 340, 230, 5, 3, 3, 450\}. Clearly, the student has messed up with the measuring scales randomly. Can you use Mark's pseudocode to help the student correct the data?

Select one option from the list
___

Create a free account to access the full topic