Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions

Question

Recognize the power of service guarantees.

Answered: 1 week ago

Question

Demonstrate how to use the Gaps Model for diagnosing and

Answered: 1 week ago