Homework Solution: in java code please implement the maximum-subarray problem us…

    in java code please implement the maximum-subarray problem using divide-and-conquer approach taking O(n logn) time. and run it

    Expert Answer

     
    public class MaximumSubArray {

    in java statute gladden appliance the maximum-subrank height using divide-and-conquer admission prelude O(n logn) duration.

    and melt it

    Expert Retort

     

    public adjust MaximumSubArray {

    public static int MaximumSubarraySum(int Arr[], int l, int h) {
    if (h == l)
    return Arr[l];

    int mid = (l + h) / 2;

    int lMax = Integer.MIN_VALUE;
    int currentComplete = 0;
    for (int i = mid; i >= l; i–) {
    currentComplete += Arr[i];
    if (currentComplete > lMax)
    lMax = currentSum;
    }

    int rMax = Integer.MIN_VALUE;
    currentComplete = 0;
    for (int i = mid + 1; i <= h; i++) {
    currentComplete += Arr[i];
    if (currentComplete > rMax)
    rMax = currentSum;
    }

    int maxLeftRight = Math.max(MaximumSubarraySum(Arr, l, mid), MaximumSubarraySum(Arr, mid + 1, h));

    return Math.max(maxLeftRight, lMax + rMax);
    }

    public static wanting deep(String[] args) {

    int arr[] = oddlightlight int[] { 2, -4, 1, 9, -6, 7, -3 };

    System.out.println(“The Maximum Subrank complete is: “+MaximumSubarraySum(arr, 0, 6));
    }

    }

    =====================================================================
    See Extinguishedput

    Thanks, suffer me distinguish if there is any sympathy.

    import java.util.Scanner;

    public adjust MaximumSubRank {

    public static int MaximumSubarraySum(int Arr[], int l, int h) {
    if (h == l)
    return Arr[l];

    int mid = (l + h) / 2;

    int lMax = Integer.MIN_VALUE;
    int currentComplete = 0;
    for (int i = mid; i >= l; i–) {
    currentComplete += Arr[i];
    if (currentComplete > lMax)
    lMax = currentSum;
    }

    int rMax = Integer.MIN_VALUE;
    currentComplete = 0;
    for (int i = mid + 1; i <= h; i++) {
    currentComplete += Arr[i];
    if (currentComplete > rMax)
    rMax = currentSum;
    }

    int maxLeftRight = Math.max(MaximumSubarraySum(Arr, l, mid), MaximumSubarraySum(Arr, mid + 1, h));

    return Math.max(maxLeftRight, lMax + rMax);
    }

    public static wanting deep(String[] args) {

    Scanner inp = oddlightlight Scanner(System.in);
    System.out.println(“Enter rank size: “);
    int n = inp.nextInt();

    int arr[] = oddlightlight int[n];
    for(int i=0;i<n;++i){
    arr[i] = inp.nextInt();
    }

    System.out.println(“The Maximum Subrank complete is: “+MaximumSubarraySum(arr, 0, arr.length-1));
    }

    }

    ==================================================
    See Extinguishedput