Maximum Subarray Sum Problem Statement

Given an array of integers, determine the maximum possible sum of any contiguous subarray within the array.

Example:

Input:
array = [34, -50, 42, 14, -5, 86]
Output:
137
Explanation:

The maximum sum is achieved by the subarray [42, 14, -5, 86].

Input:
array = [-5, -1, -8, -9]
Output:
-1
Explanation:

The maximum sum is -1, achieved by the subarray [-1].

Constraints:

  • 1 ≤ N ≤ 106, where N is the number of elements in the array.
  • -104 ≤ array[i] ≤ 104
Note:

The solution should have a time complexity of O(N).

Input:

The first line contains a single integer, N, the size of the array. The second line contains N space-separated integers representing the elements of the array.

Output:

A single integer, the maximum subarray sum.
AnswerBot
3mo

Find the maximum sum of any contiguous subarray within an array of integers.

  • Iterate through the array and keep track of the maximum sum of subarrays encountered so far.

  • At each index, decide whether to...read more

Usha R reddy
3mo
-1
Rolex Sir
4mo
To solve this problem in time, we can use Kadane's Algorithm. This algorithm efficiently computes the maximum sum of a contiguous subarray in linear time. Here's the step-by-step solution: Algorithm 1...read more
Anonymous
4mo
-1
K.Deepthi
4mo
-1
Prajkta Shendge
5mo
student at
Savitribai phule Pune University
-1
Akshay Rajput
5mo
To solve this problem in time, we can use Kadane's Algorithm. This algorithm efficiently computes the maximum sum of a contiguous subarray in linear time. Here's the step-by-step solution: Algorithm 1...read more
supremetoad
5mo
ex -
Test
maryam kasadisss
8mo
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
9mo
1
fulfillingsole
9mo
student at
Asian University Manipur msc complete
1
fashionableoggy
9mo
-1
sunnyflamingo
9mo
student at
RvNorth land higher education and traning
1
Md Imam
9mo
B.A complete
Preeti Margaret Bhengra
10mo
-1
Anonymous
10mo
1
UREVENUKA SHIVAJI
10mo
-1
Bukke Seva naik
10mo
student at
JNTU College of Engineering, Kakinada
-1
༒ SINGH༒
10mo
student at
Gems polytechnic college
-1
K sai swathi
10mo
-1
tremendoussage
10mo
student at
Baahubali College of Engineering
-1
Rajeswari NANDAVARAPU
11mo
-1
Rajeswari NANDAVARAPU
11mo
-1
endlesstaro
1y
-1
avinash verma
1y
-1
Anonymous
1y
-1
Anonymous
12mo
-1
Dayakar Behra
1y
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
1y
-1
Shaik Naseema
1y
ARR1(34690)
Shaik Naseema
1y
14
Shaik Naseema
1y
N-(1) is smallest number
Shaik Naseema
1y
N-(1) is smallest
Anonymous
1y
-1
Anonymous
1y
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
2y

/*

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 & answers

A Software Developer was asked 1mo agoQ. What is Java?
A Software Developer was asked 1mo agoQ. What is PHP?
A Software Developer was asked 1mo agoQ. What is CSS?

Popular interview questions of Software Developer

A Software Developer was asked 1mo agoQ1. What is Java?
A Software Developer was asked 1mo agoQ2. What is PHP?
A Software Developer was asked 1mo agoQ3. What is CSS?

Top HR questions asked in Amazon Software Developer

A Software Developer was asked 1mo agoQ1. Tell me about a time you had to get to the root cause of a problem
A Software Developer was asked 5mo agoQ2. What are the short-term and long-term goals for the team or organization?
A Software Developer was asked 5mo agoQ3. Why do you want to work at Amazon?
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
Contribute to help millions!
Write a review
Share interview
Contribute salary
Add office photos
Add office benefits