Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

53. Maximum Subarray

Medium51.4% Acceptance
ArrayDivide and ConquerDynamic Programming
Asked by:
LinkedIn
ProblemSolutions (12)VideosCompanies (23)Notes

Problem Statement

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.

Example 2:

Input: nums = [1]
Output: 1
Explanation: The subarray [1] has the largest sum 1.

Example 3:

Input: nums = [5,4,-1,7,8]
Output: 23
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.

Constraints:

  • 1 <= nums.length <= 105
  • -104 <= nums[i] <= 104

Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
L
A
Amazon
A
Apple
M
Microsoft
A
Adobe
+18

Approach

The Maximum Subarray problem asks you to find a contiguous subarray within an array that has the largest possible sum. A brute-force approach would check all subarrays, but that results in O(n^2) or worse time complexity.

The most efficient solution uses Kadane’s Algorithm, a dynamic programming technique. The key idea is to iterate through the array while maintaining the current running sum. If the running sum becomes negative, it is better to start a new subarray from the next element. At every step, update the global maximum sum encountered so far. This allows the algorithm to solve the problem in linear time.

Another approach uses Divide and Conquer. The array is split into two halves, and the solution considers the best subarray in the left half, the right half, and one that crosses the midpoint. Although conceptually interesting, it is typically less efficient than Kadane’s algorithm for this problem.

Complexity

ApproachTime ComplexitySpace Complexity
Kadane's Algorithm (Dynamic Programming)O(n)O(1)
Divide and ConquerO(n log n)O(log n)

Video Solution Available

CS Dojo

View all video solutions

Solutions (12)

Approach 1: Kadane's Algorithm

This approach uses Kadane's Algorithm, which is an efficient way to find the maximum subarray sum in linear time.

We iterate through the array, keeping track of the maximum sum of the subarray ending at the current position and the overall maximum sum found so far.

The algorithm maintains two variables: current_max, which is the maximum sum of the subarray that ends at the current index, and global_max, which is the maximum sum found so far.

Time Complexity: O(n), where n is the number of elements in the array.

Space Complexity: O(1), because we are using a constant amount of extra space.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <limits.h>
3
4int maxSubArray(int* nums, int numsSize) {
5    int current_max =



Explanation

This C code implements Kadane's Algorithm. We initialize current_max and global_max to the first element of the array, then iterate through the array, updating these values based on the logic of Kadane's algorithm.

Approach 2: Divide and Conquer

This approach splits the array into two halves and finds the maximum subarray sum for each half recursively. It also considers the possibility of the maximum subarray crossing the midpoint.

To find the maximum crossing subarray, we begin at the midpoint and expand outward to the left and right, keeping track of the maximum sum.

This approach effectively divides the problem into smaller subproblems and conquers each independently, then combines their results.

Time Complexity: O(n log n)

Space Complexity: O(log n) for the recursion stack

CC++JavaPythonC#JavaScript
1







Video Solutions

Watch expert explanations and walkthroughs

Kadane's Algorithm to Maximum Sum Subarray Problem

CS Dojo
11:17745,717 views

Asked By Companies

23 companies
L
LinkedIn
A
Amazon
A
Apple
M
Microsoft
A
Adobe
G
Google
F
Facebook
C
Cisco
J
JPMorgan
S
Shopee
B
Bloomberg
U
Uber
V
VMware
O
Oracle
S
Salesforce
B
Bytedance
D
Docusign
S
Samsung
G
Goldman Sachs
S
ServiceNow
W
Walmart Global Tech
P
PayTM
I
Infosys

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Median of Two Sorted ArraysHard
Construct Binary Tree from Preorder and Inorder TraversalMedium
Construct Binary Tree from Inorder and Postorder TraversalMedium
Convert Sorted Array to Binary Search TreeEasy
More similar problems

Related Topics

ArrayDivide and ConquerDynamic Programming

Problem Stats

Acceptance Rate51.4%
DifficultyMedium
Companies23

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Maximum Subarray asked in FAANG interviews?

Yes, Maximum Subarray is a very common interview question at FAANG and other top tech companies. It tests understanding of dynamic programming, greedy thinking, and the ability to optimize brute-force solutions.

What is the optimal approach for Maximum Subarray?

The optimal approach is Kadane’s Algorithm, a dynamic programming technique that processes the array in a single pass. It keeps track of the current subarray sum and the global maximum sum, achieving O(n) time complexity with constant space.

Why does Kadane’s Algorithm work for Maximum Subarray?

Kadane’s Algorithm works because any negative running sum will only reduce the total of future subarrays. By resetting the running sum when it becomes negative, the algorithm always keeps the most beneficial starting point for the maximum sum.

What data structure is used in the Maximum Subarray problem?

The problem mainly relies on array traversal and does not require complex data structures. The optimized solution only tracks running sums using variables while iterating through the array.

Previous Problem

N-Queens

Next Problem

Spiral Matrix

nums
[
0
]
;
6
int
global_max
=
nums
[
0
]
;
7
8
for
(
int
i
=
1
;
i
<
numsSize
;
i
++
)
{
9
if
(
current_max
<
0
)
{
10
current_max
=
nums
[
i
]
;
11
}
else
{
12
current_max
+=
nums
[
i
]
;
13
}
14
15
if
(
current_max
>
global_max
)
{
16
global_max
=
current_max
;
17
}
18
}
19
20
return
global_max
;
21
}
22
23
int
main
(
)
{
24
int
nums
[
]
=
{
-
2
,
1
,
-
3
,
4
,
-
1
,
2
,
1
,
-
5
,
4
}
;
25
int
size
=
sizeof
(
nums
)
/
sizeof
(
nums
[
0
]
)
;
26
int
max_sum
=
maxSubArray
(
nums
,
size
)
;
27
printf
(
"Maximum subarray sum is %d\n"
,
max_sum
)
;
28
return
0
;
29
}
#
include
<stdio.h>
2
#
include
<limits.h>
3
4
int
maxCrossingSum
(
int
arr
[
]
,
int
l
,
int
m
,
int
h
)
{
5
int
sum
=
0
;
6
int
left_sum
=
INT_MIN
;
7
for
(
int
i
=
m
;
i
>=
l
;
i
--
)
{
8
sum
=
sum
+
arr
[
i
]
;
9
if
(
sum
>
left_sum
)
10
left_sum
=
sum
;
11
}
12
13
sum
=
0
;
14
int
right_sum
=
INT_MIN
;
15
for
(
int
i
=
m
+
1
;
i
<=
h
;
i
++
)
{
16
sum
=
sum
+
arr
[
i
]
;
17
if
(
sum
>
right_sum
)
18
right_sum
=
sum
;
19
}
20
21
return
left_sum
+
right_sum
;
22
}
23
24
int
maxSubArraySum
(
int
arr
[
]
,
int
l
,
int
h
)
{
25
if
(
l
==
h
)
26
return
arr
[
l
]
;
27
28
int
m
=
(
l
+
h
)
/
2
;
29
30
int
left_max
=
maxSubArraySum
(
arr
,
l
,
m
)
;
31
int
right_max
=
maxSubArraySum
(
arr
,
m
+
1
,
h
)
;
32
int
cross_max
=
maxCrossingSum
(
arr
,
l
,
m
,
h
)
;
33
34
return
left_max
>
right_max
?
(
left_max
>
cross_max
?
left_max
:
cross_max
)
:
(
right_max
>
cross_max
?
right_max
:
cross_max
)
;
35
}
36
37
int
main
(
)
{
38
int
nums
[
]
=
{
-
2
,
1
,
-
3
,
4
,
-
1
,
2
,
1
,
-
5
,
4
}
;
39
int
size
=
sizeof
(
nums
)
/
sizeof
(
nums
[
0
]
)
;
40
int
max_sum
=
maxSubArraySum
(
nums
,
0
,
size
-
1
)
;
41
printf
(
"Maximum subarray sum is %d\n"
,
max_sum
)
;
42
return
0
;
43
}

Explanation

This C implementation uses divide and conquer to find the maximum contiguous subarray sum. It divides the array into two halves, recursively finds the maximum subarray sum in each half, and also finds the maximum crossing sum that can be obtained by including elements from both halves.