Question: Please help me to transform the code below from python to JAVA. For this assignment, First, I have to generate n random numbers between -100

Please help me to transform the code below from python to JAVA. For this assignment, First, I have to generate n random numbers between -100 to 100 and save them in array a. Then, I have to write 2 functios to find the Maximum Subsequence Sum ( means find the sequence of numbers that when you add them together, you get the largest value). One has to have the running time O(n) and the other one O(logN). The last step is to Print the generated array and the outputs of both of the functions. This code works perfectly on python, I just need to translate it to JAVA. Thanks!

//PART A

import random

def MSS(a, n): maxsum = 0 sum = 0 for i in range(0, n): sum += a[i] if sum < 0: sum = 0 elif maxsum < sum: maxsum = sum return maxsum

a = list() n = int(input("Please enter a positive integer: ")) for i in range(0, n): a.append(random.randint(-100, 101)) print(a) b = MSS(a, n) print('Maximum Sum of Subarrays:', b)

//PART B:

import random

def maxCrossSum(a, low, mid, hi): #elements on left of mid. temp = 0 leftsum = -1000

for i in range(mid, low - 1, -1): temp = temp + a[i]

if (temp > leftsum): leftsum = temp

#elements on right of mid temp = 0 rightsum = -1000 for i in range(mid + 1, hi + 1): temp = temp + a[i]

if (temp > rightsum): rightsum = temp

#Return sum of elements on left and right of mid return leftsum + rightsum

def maxSubListSum(a, low, hi):

#only one element if (low == hi): return a[low]

mid = (low + hi) // 2

#Maximum subarray sum in left half #Maximum subarray sum in right half #Maximum subarray sum such that the subarray crosses the midpoint return max(maxSubListSum(a, low, mid), maxSubListSum(a, mid + 1, hi), maxCrossSum(a, low, mid, hi))

a = list() n = int(input("Please enter a positive integer: ")) for i in range(0, n): a.append(random.randint(-100, 101)) print(a) b = maxSubListSum(a, 0, n-1) print('Maximum Sum of Subarrays:', b)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!