Problem:
Write a java program for the brute-force method of solving the maximum-subarray
problem.
Solution:
package com.basics;
import java.util.Scanner;
public class MaxSumOfSubArray {
static int[] array;
int arrayLimit;
public static void main(String[] args) {
MaxSumOfSubArray maxSumOfSubArray = new MaxSumOfSubArray();
maxSumOfSubArray.initializeTheArrays();
maxSumOfSubArray.findMaximumSumOfSubArray();
}
public void initializeTheArrays(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the array limit");
arrayLimit = scanner.nextInt();
array = new int[arrayLimit];
System.out.println("enter the array elements");
for (int i = 0; i < array.length; i++) {
System.out.println("enter the element");
array[i] = scanner.nextInt();
}
scanner.close();
}
public void findMaximumSumOfSubArray(){
int answer = Integer.MIN_VALUE;
for(int sub_array_size = 1; sub_array_size <= arrayLimit; ++sub_array_size ){
for (int startIndex = 0; startIndex < array.length; ++startIndex) {
if(startIndex + sub_array_size > arrayLimit){
break;
}
int sum = 0;
for(int i = startIndex; i < (startIndex + sub_array_size); i++){
sum = sum + array[i];
}
answer = Math.max(answer, sum);
}
}
System.out.println("the max sub array sum is "+ answer);
}
}
Output:
enter the array limit
8
enter the array elements
enter the element
-2
enter the element
-3
enter the element
4
enter the element
-1
enter the element
-2
enter the element
1
enter the element
5
enter the element
-3
the max sub array sum is 7
Write a java program for the brute-force method of solving the maximum-subarray
problem.
Solution:
package com.basics;
import java.util.Scanner;
public class MaxSumOfSubArray {
static int[] array;
int arrayLimit;
public static void main(String[] args) {
MaxSumOfSubArray maxSumOfSubArray = new MaxSumOfSubArray();
maxSumOfSubArray.initializeTheArrays();
maxSumOfSubArray.findMaximumSumOfSubArray();
}
public void initializeTheArrays(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the array limit");
arrayLimit = scanner.nextInt();
array = new int[arrayLimit];
System.out.println("enter the array elements");
for (int i = 0; i < array.length; i++) {
System.out.println("enter the element");
array[i] = scanner.nextInt();
}
scanner.close();
}
public void findMaximumSumOfSubArray(){
int answer = Integer.MIN_VALUE;
for(int sub_array_size = 1; sub_array_size <= arrayLimit; ++sub_array_size ){
for (int startIndex = 0; startIndex < array.length; ++startIndex) {
if(startIndex + sub_array_size > arrayLimit){
break;
}
int sum = 0;
for(int i = startIndex; i < (startIndex + sub_array_size); i++){
sum = sum + array[i];
}
answer = Math.max(answer, sum);
}
}
System.out.println("the max sub array sum is "+ answer);
}
}
enter the array limit
8
enter the array elements
enter the element
-2
enter the element
-3
enter the element
4
enter the element
-1
enter the element
-2
enter the element
1
enter the element
5
enter the element
-3
the max sub array sum is 7
No comments:
Post a Comment