Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4099044
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:22:56+00:00 2026-05-20T20:22:56+00:00

Possible Duplicate: Find the maximum interval sum in a list of real numbers. I

  • 0

Possible Duplicate:
Find the maximum interval sum in a list of real numbers.

I was asked the following question today at Adobe interview for the position of software engineer.

Problem Given a array arr[1..n] of integers. Write an algorithm to find the sum of contiguous subarray within the array which has the largest sum. Return 0 if all the numbers are negative.

Example

Given array arr[1..6] = [ 12, 14, 0, -4, 61, -39 ]

Answer

83 constructed with [ 12, 14, 0, -4, 61 ].

I could come up with a solution running in O(n logn) but I don’t think it was very efficient. The interviewer asked to me to write an O(n) algorithm. I couldn’t come up with it.

Any idea about how to write an O(n) solution for this problem?
Algorithm to be implemented either in C/C++/Java.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:22:57+00:00Added an answer on May 20, 2026 at 8:22 pm

    You can use Kadane’s algorithm which runs in O(n).

    Here is the algorithm (shamelessly copied from here)

    Initialize:
        max_so_far = 0
        max_ending_here = 0
    
    Loop for each element of the array
      (a) max_ending_here = max_ending_here + a[i]
      (b) if(max_ending_here < 0)
                max_ending_here = 0
      (c) if(max_so_far < max_ending_here)
                max_so_far = max_ending_here
    return max_so_far
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to find positions of the list maximum? A question from homework:
Possible Duplicate: Algorithm to find Lucky Numbers I came across this question.A number is
Possible Duplicate: Pythonic way to find maximum value and its index in a list?
Possible Duplicate: Find never-called functions Hello I know that similar question was already asked
Possible Duplicate: Find integer not occurring twice in an array Accenture interview question -
Possible Duplicate: Getting the submatrix with maximum sum? Given a 2-dimensional array of positive
Possible Duplicate: How to find a list of wireless networks (SSID's) in Java, C#,
Possible Duplicate: Finding a submatrix with the maximum possible sum in O(n^2) I have
Possible Duplicate: Find text string using jQuery? How do you find a text string
Possible Duplicate: Find and Replace more than 1 word? I want to use jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.