Maximum Subarray Sum (Minimum Subarray Sum) [codingblocks]
You are given a one dimensional array that may contain both positive and negative integers, find the sum of contiguous subarray of numbers which has the largest sum. For example, if the given array is {-2, -5, 6, -2, -3, 1, 5, -6}, then the maximum subarray sum is 7.
Input Format
The first line consists of number of test cases T. Each test case consists of two lines.
The first line of each testcase contains a single integer N denoting the number of elements for the array A.
The second line of each testcase contains N space separated integers denoting the elements of the array.
Constraints
1 <= N <= 100000
1 <= t <= 20
-100000000 <= A[i] <= 100000000
Output Format
Output a single integer denoting the maximum subarray sum for each testcase in a new line.
Sample Input
2
4
1 2 3 4
3
-10 5 10
Sample Output
10
15
Explanation
For the first testcase, maximum subarray sum is generated by the entire array - 1+2+3+4 = 10
For the second testcase , maximum subarray sum is generated by the subarray {5,10} - 5+10 = 15
For the second testcase , maximum subarray sum is generated by the subarray {5,10} - 5+10 = 15
Java Solution (Maximum Subarray Sum)
IDE Code: https://ide.geeksforgeeks.org/pXco2wi7VR
/* Amit Kumar 12-12-2020 IDE Code: https://ide.geeksforgeeks.org/pXco2wi7VR */ package array; import java.util.Scanner; public class Array_MaximumSubarraySum { public static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int testCase = scanner.nextInt(); while (testCase-- >0) { int size = scanner.nextInt(); int[] array = new int[size]; for (int i=0; i<size; i++) { array[i] = scanner.nextInt(); } System.out.println(maximumSubarraySumHelper(array)); } } private static int maximumSubarraySumHelper(int[] array) { int max = array[0]; int currSum = array[0]; for (int i=1; i<array.length; i++) { currSum = Math.max(currSum + array[i], array[i]); max = Math.max(currSum, max); } return max; } }
Java Solution (Minimum Subarray Sum)
IDE Code: https://ide.geeksforgeeks.org/edPgCFER4R
/* Amit Kumar 12-12-2020 IDE Code: https://ide.geeksforgeeks.org/edPgCFER4R */ package array; import java.util.Scanner; public class Array_MinimumSubarraySum { public static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int testCase = scanner.nextInt(); while (testCase-- >0) { int size = scanner.nextInt(); int[] array = new int[size]; for (int i=0; i<size; i++) { array[i] = scanner.nextInt(); } System.out.println(minimumSubarraySumHelper(array)); } } private static int minimumSubarraySumHelper(int[] array) { int min = array[0]; int currMinSum = array[0]; for (int i=1; i<array.length; i++) { currMinSum = Math.min(currMinSum + array[i], array[i]); min = Math.min(currMinSum, min); } return min; } }
Paddy Power Casino Hotel Las Vegas, Nevada - Mapyro
ReplyDeleteFind the 의왕 출장샵 location of Paddy Power Casino Hotel Las Vegas, Nevada and other 청주 출장샵 local 거제 출장마사지 landmarks located in 삼척 출장샵 real-time 여주 출장안마 at Mapyro.