Given an integer array arr, write a program to find the subarray which has the largest sum and return its sum.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
Question
Given an integer array arr, write a program to find the subarray which has the largest sum and return its sum.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
Solution
Sure, here is a Python solution using Kadane's algorithm. This algorithm is used for finding the largest sum of a contiguous subarray in an array of integers.
def max_subarray_sum(arr):
max_current = max_global = arr[0]
for i in range(1, len(arr)):
max_current = max(arr[i], max_current + arr[i])
if max_current > max_global:
max_global = max_current
return max_global
arr = [9, -2, -1, -3, 4, -1, 2, 1, -5, 4]
print(max_subarray_sum(arr)) # Output: 6
Here's how it works:
- Initialize
max_currentandmax_globalwith the first element of the array. - Loop through the array starting from the second element.
- For each element, calculate
max_currentas the maximum of the current element and the sum of the current element andmax_current. - If
max_currentis greater thanmax_global, updatemax_globalwith the value ofmax_current. - After the loop ends,
max_globalwill hold the largest sum of a contiguous subarray.
Similar Questions
Given an integer array nums, find the subarray with the largest sum, and return its sum.
Given an integer array arr, partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray.Return the largest sum of the given array after partitioning. Test cases are generated so that the answer fits in a 32-bit integer. Example 1:Input: arr = [1,15,7,9,2,5,10], k = 3Output: 84Explanation: arr becomes [15,15,15,9,10,10,10]
Write an efficient C program to find the sum of contiguous subarray within a one-dimensionalarray of integer which returns the largest sum.Explanation:Lets take the example of array {5,-3, 4}Possible contiguous subarray combinations are{5}, {-3}, {4}, {5,-3}, {-3,4}, {5,-3,4}The contiguous subarray {5,-3,4} has got the largest sum 6
Given an array of n positive integers. Find the sum of the maximum sum subsequence of the given array such that the integers in the subsequence are sorted in strictly increasing order i.e. a strictly increasing subsequence. Example 1:Input: N = 5, arr[] = {1, 101, 2, 3, 100} Output: 106Explanation:The maximum sum of a increasing sequence is obtained from {1, 2, 3, 100},Example 2:Input: N = 4, arr[] = {4, 1, 2, 3}Output: 6Explanation:The maximum sum of a increasing sequence is obtained from {1, 2, 3}.Your Task: You don't need to read input or print anything. Complete the function maxSumIS() which takes N and array arr as input parameters and returns the maximum value.Expected Time Complexity: O(N2)Expected Auxiliary Space: O(N)Constraints:1 ≤ N ≤ 1031 ≤ arr[i] ≤ 105
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.*;34public class Solution {56 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}
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.