Maximum Sum Subarray Problem Statement

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

Example:

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

The maximum sum of a contiguous subarray is 137, which is the sum of the elements [42, 14, -5, 86].

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

The maximum sum of a contiguous subarray is -1, as choosing any subarray cannot yield a greater sum.

Input:

N
array of N space-separated integers

Output:

Maximum sum of any contiguous subarray

Constraints:

  • 1 ≤ N ≤ 105
  • -104 ≤ array[i] ≤ 104
Note:

The solution should be accomplished in O(N) time complexity.

AnswerBot
4mo

Find the maximum sum of any contiguous subarray within an array in O(N) time complexity.

  • Use Kadane's algorithm to find the maximum sum subarray in O(N) time complexity

  • Initialize two variables: max_sum...read more

Ganesh Jadhav
1y

import java.util.Scanner;

public class MaximumSumSubarray {

public static int maxSubArraySum(int[] nums) {

int maxSum = nums[0]; // Initialize maxSum with the first element of the array

int currentSum = nums[0]; // Initialize currentSum with the first element of the array

for (int i = 1; i < nums.length; i++) {

// Compare currentSum + nums[i] with nums[i]

// If currentSum + nums[i] is greater, update currentSum, else start a new subarray

currentSum = Math.max(nums[i], currentSum + nums[i]);

// Update maxSum if currentSum is greater

maxSum = Math.max(maxSum, currentSum);

}

return maxSum;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int N = sc.nextInt(); // Size of the array

int[] nums = new int[N];

for (int i = 0; i < N; i++) {

nums[i] = sc.nextInt(); // Read the array elements

}

int result = maxSubArraySum(nums);

System.out.println(result); // Print the maximum subarray sum

}

}

Help your peers!
Select
Add answer anonymously...

ThoughtWorks Software Developer interview questions & answers

A Software Developer was asked Q. How would you add an API to the existing repository?
A Software Developer was asked Q. In a code pairing round, you will be given a story where you need to add an API ...read more
A Software Developer was asked Q. What is the difference between CHAR and VARCHAR2 data types in SQL?

Popular interview questions of Software Developer

A Software Developer was asked Q1. How would you add an API to the existing repository?
A Software Developer was asked Q2. In a code pairing round, you will be given a story where you need to add an API ...read more
A Software Developer was asked Q3. What is the difference between CHAR and VARCHAR2 data types in SQL?
ThoughtWorks Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
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
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits