Java: Compute the maximum value of the sum of the passing integers
Java Basic: Exercise-240 with Solution
As shown in Figure 1, arrange integers (0 to 99) as narrow hilltops. When reading such data from top to bottom, following the next rule represents a huge amount of data.
Write a Java program that computes the maximum value of the sum of the passing integers.
Input:
A series of integers separated by commas are given in diamonds. No spaces are included in each line. The input example corresponds to Figure 1. The number of lines of data is less than 100 lines.
Output: The maximum value of the sum of integers passing according to the rule on one line.
Visual Presentation:
Sample Solution:
Java Code:
// Importing required Java classes
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// Main class named "Main"
public class Main {
// Main method, the entry point of the program
public static void main(String[] args) {
// Prompting the user to input numbers (Ctrl+C to exit)
System.out.println("Input the numbers (Ctrl+C to exit):");
// Creating a Scanner object to read input from the console
Scanner sc = new Scanner(System.in);
// Creating a List to store input lines as strings
List<String> l = new ArrayList<>(); // Specify the type of List as String
// Reading input until the user exits (Ctrl+C)
while(sc.hasNext()) {
l.add(sc.next());
}
// Getting the number of input lines
int n = l.size();
// Creating a 2D array 'a' to store parsed integers from input lines
int[][] a = new int[n][];
// Parsing input lines and populating the 2D array 'a'
for(int i = 0; i < n; i++) {
String[] s = l.get(i).split(",");
int k = s.length;
a[i] = new int[k];
for(int j = 0; j < k; j++) {
a[i][j] = Integer.parseInt(s[j]);
}
}
// Initializing an array 'sd' with the first element of the first row of 'a'
int[] sd = {a[0][0]};
// Dynamic programming approach to find the maximum sum
for(int i = 1; i < n; i++) {
int[] tmp = new int[a[i].length];
for(int j = 0; j < tmp.length; j++) {
if(i <= n / 2) {
if(j == 0) tmp[j] = sd[j] + a[i][j];
else if(j == tmp.length - 1) tmp[j] = sd[j - 1] + a[i][j];
else tmp[j] = Math.max(sd[j - 1] + a[i][j], sd[j] + a[i][j]);
}
else {
tmp[j] = Math.max(sd[j] + a[i][j], sd[j + 1] + a[i][j]);
}
}
sd = tmp;
}
// Prompting the user with the result
System.out.println("Maximum value of the sum of integers passing according to the rule on one line.");
// Printing the final result
System.out.println(sd[0]);
}
}
Sample Output:
Input the numbers (ctrl+c to exit): 8 4,9 9,2,1 3,8,5,5 5,6,3,7,6 3,8,5,5 9,2,1 4,9 8 Maximum value of the sum of integers passing according to the rule on one line. 64
Flowchart:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters.
Next: Write a Java program to find the number of combinations that satisfy p + q + r + s = n where n is a given number <= 4000 and p, q, r, s in the range of 0 to 1000.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/basic/java-basic-exercise-240.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics