Java to find an array of consecutive n elements to make it and the largest

Source: Internet
Author: User

Given an array, the number of successive elements in the group is the largest value. If all elements are positive, it is obvious that the entire array is required. If the value of all elements is negative, the maximum value is 0.

This is seen on the programming Zhuji, whose time complexity is reduced from O (N3) to O (n).

Java code

package cn.lifx.test;





public class Maxsum


{


public static void Main (string[] args)


  {


int[] arr = new int[]{31,-41, 59, 26,-53, 58, 97,-93,-23, 84};





maxsum ms = new Maxsum ();





Ms. Max (arr);


Ms. MAX2 (arr);


Ms. MAX3 (arr);





int max = Ms. MAX4 (arr, 0, arr.length-1);


System.out.println ("Max sum is" + max);





Ms. MAX5 (arr);


  }





//Method 1: Time Complexity of O (n*n*n)


public void Max (int[] arr)


  {


int max = 0;


int sum = 0;


int left =-1;


int right =-1;





for (int i=0; i<arr.length; i++)


  {


for (int j=i; j<arr.length; j + +)


   {


sum = 0;





for (int k=i; k<=j; k++)


   {


sum = sum + arr[k];


   }





if (Sum > Max)


   {


left = i;


right = J;


max = sum;


   }


   }


  }





if (right > 0)


  {


System.out.println ("+ Left +" ("+ Arr[left] +") to element "+ Right +" ("+ arr[right] + "), Max sum is" + max ";


  }


Else


  {


System.out.println ("Max sum is 0.");


  }


  }





//Method 2: Time complexity of O (n*n)


public void Max2 (int[] arr)


  {


int max = 0;


int sum = 0;


int left =-1;


int right =-1;





for (int i=0; i<arr.length; i++)


  {


sum = 0;


for (int j=i; j<arr.length; j + +)


   {


sum = sum + arr[j];


if (Sum > Max)


   {


left = i;


right = J;


max = sum;


   }


   }


  }





if (right > 0)


  {


System.out.println ("+ Left +" ("+ Arr[left] +") to element "+ Right +" ("+ arr[right] + "), Max sum is" + max ";


  }


Else


  {


System.out.println ("Max sum is 0.");


  }


  }





//Method 3: Time complexity of O (n*n)


public void Max3 (int[] arr)


  {


int max = 0;


int sum = 0;


int left =-1;


int right =-1;





int[] temp = new int[arr.length+1];





temp[0] = 0;


for (int i=0; i<arr.length; i++)


  {


temp[i+1] = Temp[i] + arr[i];


  }





for (int i=0; i<arr.length; i++)


  {


for (int j=i; j<temp.length; j + +)


   {


sum = temp[j]-temp[i];


if (Sum > Max)


   {


left = i;


right = j-1;


max = sum;


   }


   }


  }





if (right > 0)


  {


System.out.println ("Max" from element "+ Left +" ("+ Arr[left] +") to element "+ Right +" ("+ arr[right] +"), Max Sum is "+ max);


  }


Else


  {


System.out.println ("Max sum is 0.");


  }


  }





//Method 4: Time Complexity of O (N*LOGN)


public int Max4 (int[] arr, int left, int right)


  {


int sum = 0;


int max = 0;


int max1 = 0;


int max2 = 0;


int middle = 0;





if (left > right)


  {


return 0;


  }


else if (left = right)


  {


return (Arr[left] > 0? arr[left]: 0);


  }





middle = (left + right)/2;





for (int i=middle; i>=left; i--)


  {


sum = sum + arr[i];


if (Sum > Max1)


   {


max1 = sum;


   }


  }





sum=0;


for (int i=middle+1; i<=right; i++)


  {


sum = sum + arr[i];


if (Sum > Max2)


   {


max2 = sum;


   }


  }





max = MAX1+MAX2;


int temp1 = Max4 (arr, left, middle);


int temp2 = Max4 (arr, middle+1, right);





if (Temp1 > Max)


  {


max = Temp1;


  }





if (Temp2 > Max)


  {


max = Temp2;


  }





return Max;


  }





//Method 5: Time Complexity of O (n)


public void Max5 (int[] arr)


{


int max1 = 0;


int max2 = 0;


int left =-1;


int right =-1;


int temp = 0;





for (int i=0; i<arr.length; i++)


  {


temp = (max1+arr[i]);


if (temp > 0)


   {


max1 = temp;


   }


Else


   {


left = i+1;


max1 = 0;


   }





if (Max1 > Max2)


   {


right = i;


max2 = max1;


   }


  }





if (right > 0)


  {


System.out.println ("+ Left +" ("+ Arr[left] +") to element "+ Right +" ("+ arr[right] + "), Max sum is" + max2);


  }


Else


  {


System.out.println ("Max sum is 0.");


  }


  }


}

The output is:

Java code

Max is from element 2(59) to element 6(97), max sum is 187
Max is from element 2(59) to element 6(97), max sum is 187
Max is from element 2(59) to element 6(97), max sum is 187
Max sum is 187
Max is from element 2(59) to element 6(97), max sum is 187

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.