Knowee
Questions
Features
Study Tools

Implement a Java program to Find the subarray of size 3 with the greatest sum.Input FormatThe input consists of a single line where the user is prompted to enter 30 integers, separated by spaces.ConstraintsThe array contains exactly 30 integers.Output FormatThe output consists of two lines: The first line contains the subarray with the greatest sum separated by spaces. The second line contains the sum of the subarray.Sample Input 010 20 30 40 50 60 70 80 90 100 11 22 33 44 55 66 77 88 99 9 8 7 6 5 4 3 2 1 99 88Sample Output 0Subarray with the Greatest Sum: 80 90 100Sum of the Subarray: 270Contest ends in an hourSubmissions: 0Max Score: 10Difficulty: MediumRate This Challenge: More Java 151import java.io.*;2import java.util.*;3​4public class Solution {5​6    public static void main(String[] args) {7        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */8   }9}

Question

Implement a Java program to Find the subarray of size 3 with the greatest sum.Input FormatThe input consists of a single line where the user is prompted to enter 30 integers, separated by spaces.ConstraintsThe array contains exactly 30 integers.Output FormatThe output consists of two lines: The first line contains the subarray with the greatest sum separated by spaces. The second line contains the sum of the subarray.Sample Input 010 20 30 40 50 60 70 80 90 100 11 22 33 44 55 66 77 88 99 9 8 7 6 5 4 3 2 1 99 88Sample Output 0Subarray with the Greatest Sum: 80 90 100Sum of the Subarray: 270Contest ends in an hourSubmissions: 0Max Score: 10Difficulty: MediumRate This Challenge: More Java 151import java.io.;2import java.util.;3​4public class Solution {5​6    public static void main(String[] args) {7        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */8   }9}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Java program that finds the subarray of size 3 with the greatest sum:

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] array = new int[30];
        for (int i = 0; i < 30; i++) {
            array[i] = scanner.nextInt();
        }
        scanner.close();

        int maxSum = Integer.MIN_VALUE;
        int[] maxSubarray = new int[3];
        for (int i = 0; i < array.length - 2; i++) {
            int currentSum = array[i] + array[i + 1] + array[i + 2];
            if (currentSum > maxSum) {
                maxSum = currentSum;
                maxSubarray[0] = array[i];
                maxSubarray[1] = array[i + 1];
                maxSubarray[2] = array[i + 2];
            }
        }

        System.out.println("Subarray with the Greatest Sum: " + maxSubarray[0] + " " + maxSubarray[1] + " " + maxSubarray[2]);
        System.out.println("Sum of the Subarray: " + maxSum);
    }
}

This program reads 30 integers from the user, stores them in an array, and then iterates over the array to find the subarray of size 3 with the greatest sum. It keeps track of the maximum sum found so far and the corresponding subarray. Finally, it prints the subarray with the greatest sum and its sum.

This problem has been solved

Similar Questions

Given an integer array arr, write a program to find the subarray which has the largest sum and return its sum. If the array is empty, return 0.Sample Input:9-2 -1 -3 4 -1 2 1 -5 4Sample Output: 6Explanation:The subarray is [4, -1, 2, 1] and the sum is 6

Given an array of integers A, print true if we can partition the array into three non-empty subarrays with equal sums.Input FormatThe first line of the input contains an integer N. Second line of input contains an array of size N.Output FormatPrint true if we can partition the array, otherwise false.Constraints3 ≤ N ≤ 104-104 ≤ Ai ≤ 104ExampleInput103 3 6 5 -2 2 5 1 -9 4OutputtrueExplanation(3 + 3) = (6) = (5 - 2 + 2 + 5 + 1 - 9 + 4) = 6.

Java program to sum values of an array.Write a Java program to sum values of an array. Constraints:N/AExample:Input:1, 2, 3, 4, 5, 6, 7, 8, 9, 10Output:55Explanation:-Public Test Cases:# INPUT EXPECTED OUTPUT1 1, 2, 3, 4, 5, 6, 7, 8, 9, 1055

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Given an array of integers of size N, print the sum of sum of all subarrays.Input FormatFirst line of input contains T - number of test cases. Its followed by 2T lines, the first line contains N - size of the array and second line contains the elements of the array.Output FormatFor each test case, print the sum of sum of all subarrays, separated by newline.Constraints10 points1 <= T <= 1001 <= N <= 10220 points1 <= T <= 1001 <= N <= 10370 points1 <= T <= 10001 <= N <= 104General Constraints-106 <= arr[i] <= 106ExampleInput333 4 521 231 -3 4Output4063ExplanationTest Case 1:[3] + [3,4] + [3,4,5] + [4] + [4,5] + [5] = 40Test Case 2:[1] + [1,2] + [2] = 6Test Case 3:[1] + [1,-3] + [1,-3,4] + [-3] + [-3,4] + [4] = 3

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.