0% found this document useful (0 votes)
7 views

2020508_HW2

Uploaded by

Harsh Aggarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

2020508_HW2

Uploaded by

Harsh Aggarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Q1).

Description: Since we have to find the maximum distance that the monkey can cover only by
jumping, we can find the sum of elements of all the subarrays one by one in which the monkey
is covering the distance only by jumping and then we can find our required distance. If there are
n trees and if we find the required result for n - 1 trees then we can use this result and combine
it with the output of nth tree and find our required result.

Recurrence to subproblem: We are recursively starting from ith to nth tree, where i is growing
from 1 to n. And we are combining the result to get a favorable output.

Subproblem: Here the subproblem is given below:

max_jump = min. value of Integer datatype


temp = 0
for ( n <= i < len(jump[ ])):
if ( temp != dist[i + 1]):
temp = 0
temp += dist[i] + jump[i]
max_jump = max(max_jump, temp)
return max_jump

Pseudo-Code:

findValue(dist[ ], jump[ ], n):


max_jump = min. value of Integer datatype
temp = 0
for ( n <= i < len(jump[ ])):
if ( temp != dist[i + 1]):
temp = 0
temp += dist[i] + jump[i]
max_jump = max(max_jump, temp)
return max_jump

maxJump(dist[ ], jump[ ], n):


value = min. value of Integer datatype
if (n >= 0):
maxJump(dist[ ], jump[ ], n - 1)
value = max(findValue(dist[ ], jump[ ], n), value)
return value

Time Complexity:
The time complexity of the subproblem is O(n) and through recursion the subproblem is running
n times so the overall time complexity is O(n2).

Q2).

Description: Since we have to find the maximum distance that the monkey can cover only by
jumping, we can find the sum of elements of all the subarrays one by one in which the monkey
is covering the distance only by jumping and then we can find our required distance. If there are
n trees and if we find the required result for n - 1 trees then we can use this result and combine
it with the output of nth tree and find our required result. In this question we can replace all the
trees values used by Mr. Monkey with -1 because by this another monkey cannot use these
trees for jumping and we can find our favorable output by calculating the value for both the
monkeys and then adding them.

Recurrence to subproblem: We are recursively starting from ith to nth tree, where i is growing
from 1 to n. And we are combining the result to get a favorable output.

Subproblem: Here the subproblem is given below:

max_jump = min. value of Integer datatype


temp = 0
for ( n <= i < len(jump[ ])):
if ( temp != dist[i + 1]):
temp = 0
temp += dist[i] + jump[i]
max_jump = max(max_jump, temp)
return max_jump

Pseudo-Code:

findValue(dist[ ], jump[ ], n):


max_jump = min. value of Integer datatype
temp = 0
tempArr = copy of dist[ ]
for ( n <= i < len(jump[ ])):
if ( temp != dist[i + 1] or dist[i] == -1):
temp = 0
tempArray[ ] = copy of dist[ ]
temp += dist[i] + jump[i]
tempArray[i] = -1
max_jump = max(max_jump, temp)
return max_jump, tempArray
maxJump(dist[ ], jump[ ], n):
value = min. value of Integer datatype
if (n >= 0):
maxJump(dist[ ], jump[ ], n - 1)
Value, tempArray = max(findValue(dist[ ], jump[ ], n), value)
if (n == len(jump[ ]) - 1:
dist[ ] = tempArray[ ]
return value, tempArray

findTotalJump():
dist[ ] and jump[ ] are already given
value1, dist[ ] = maxJump(dist[ ], jump[ ], n)
value2, dist[ ] = maxJump(dist[ ], jump[ ], n)
return value1 + value2

Time Complexity:
The time complexity of the subproblem is O(n) and through recursion the subproblem is running
n times so the overall time complexity for one monkey is O(n2). Now as we are finding the
results for two monkeys by adding their results so the time complexity of whole algorithm is
O(n2).

You might also like