Maximum Subarray Sum

Given an array of numbers, find the maximum sum of any contiguous subarray of the array.

For example, given the array [34, -50, 42, 14, -5, 86], the maximum sum would be 137, since we would take elements 42, 14, -5, and 86.

Given the array [-5, -1, -8, -9], the maximum sum would be -1.

Follow up: Do this in O(N) time.

Input Format:

The first line of input contains size of array, which is denoted by N and second line of input contains N space separated integers.

Output Format:

The first and only line of output should print the maximum subarray sum, as described in the description.

CodingNinjas
author
2y

1. I explained the Naive approach which was taking 2 loops and considering every subarray once. It was an O(N^2 ) approach.
2. I optimised my approach to one loop making it O(N), by applying kadane alg...read more

CodingNinjas
author
2y
Brute Force Approach
  1. Create a nested loop. The outer loop will go from i = 0 to i = n - k. This will cover the starting indices of all k-subarrays
  2. The inner loop will go from j = i to j = i + k - 1. Thi...read more
CodingNinjas
author
2y
BST Approach
  1. Create a BST and insert the first k elements in it
  2. Print the largest element in the tree
  3. Now iterate through the remaining elements of the tree:
    1. Insert the current element in the BST
    2. Delete th...read more
CodingNinjas
author
2y
Deque Approach
  1. Create a double ended queue. Note that the deque will store the indices of the elements, not the elements themselves
  2. Insert the first k elements (the first subarray) in the deque. While i...read more
Shaik Naseema
9mo
-1
maryam kasadisss
2mo
Approach Create a nested loop. The outer loop will go from i = 0 to i = n - k. This will cover the starting indices of all k-subarrays The inner loop will go from j = i to j = i + k - 1. This will cov...read more
Anonymous
3mo
1
fulfillingsole
3mo
student at
Asian University Manipur msc complete
1
fashionableoggy
3mo
-1
sunnyflamingo
3mo
student at
RvNorth land higher education and traning
1
Md Imam
3mo
B.A complete
Preeti Margaret Bhengra
4mo
-1
Anonymous
4mo
1
UREVENUKA SHIVAJI
4mo
-1
Bukke Seva naik
4mo
student at
JNTU College of Engineering, Kakinada
-1
༒ SINGH༒
4mo
student at
Gems polytechnic college
-1
K sai swathi
4mo
-1
tremendoussage
4mo
student at
Baahubali College of Engineering
-1
Rajeswari NANDAVARAPU
5mo
-1
Rajeswari NANDAVARAPU
5mo
-1
endlesstaro
6mo
-1
avinash verma
6mo
-1
Anonymous
6mo
-1
Anonymous
6mo
-1
Dayakar Behra
6mo
To solve the problem of finding the maximum sum of any contiguous subarray of an array in O(N) time, we can use Kadane's Algorithm. This algorithm works by iterating through the array and at each step...read more
Vaishali
6mo
-1
Shaik Naseema
9mo
ARR1(34690)
Shaik Naseema
9mo
14
Shaik Naseema
9mo
N-(1) is smallest number
Shaik Naseema
9mo
N-(1) is smallest
Anonymous
11mo
-1
Anonymous
12mo
The maximum sum would be -1
chandankumar6301
1y
currently not working
The maximum sum would be -1
chandankumar6301
1y
currently not working
import java.util.Scanner; public class MaximumSubarraySum { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input size of array int N = scanner.nextInt(); // Inpu...read more
Anonymous
1y

The maximum sum would be -1

Anonymous
1y

The maximum sum would be -1

Sweet Swathi
1y
the maximum sum would be -1.
Anonymous
1y

Send line elements is maximum sum is -1

Ruchi Gupta
1y

/*

Time Complexity - O(N * K)

Space Complexity - O(1)

where N is the length of the array and K is the size of subarrays

*/

void printSubarrayMax(int *arr, int n, int k)

{

if (n == 0 || k == 0)

{

return;

...read more

Add answer anonymously...
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter