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
Question
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
Solution
To find the subarray with the largest sum, we can use the Kadane's algorithm. Here are the steps to solve this problem:
- Initialize two variables, maxSum and currentSum, to 0.
- Iterate through the array arr from left to right.
- For each element in arr, add it to the currentSum.
- If the currentSum becomes negative, reset it to 0.
- If the currentSum is greater than the maxSum, update the maxSum.
- Continue this process until all elements in arr have been processed.
- Finally, return the maxSum as the result.
Here is the implementation in Python:
def findLargestSubarraySum(arr):
maxSum = 0
currentSum = 0
for num in arr:
currentSum += num
if currentSum < 0:
currentSum = 0
if currentSum > maxSum:
maxSum = currentSum
return maxSum
# Test case
arr = [9, -2, -1, -3, 4, -1, 2, 1, -5, 4]
result = findLargestSubarraySum(arr)
print(result) # Output: 6
In the given sample input, the subarray with the largest sum is [4, -1, 2, 1], and the sum of this subarray is 6.
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
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}
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
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.