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

  • SEARCH
  • Home
  • 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 8064321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:19:59+00:00 2026-06-05T11:19:59+00:00

I Implemented the Kadane’s Max Sub array problem in javascript but seems i end

  • 0

I Implemented the Kadane’s Max Sub array problem in javascript but seems i end up always getting 0 in the console even though there exists higher numbers( I understand that it does what it’s doing because of for loop from 0 - size where size = subarray size).

  • So how do i implement the algorithm correctly?

  • Does it also work for all positive integer array?

JsBin

http://jsbin.com/ajivan/edit#javascript,live

  • 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-06-05T11:20:00+00:00Added an answer on June 5, 2026 at 11:20 am

    You’re passing n=3 as an argument while your array has length 6. I changed your algorithm to use length:

    function SubSequence(a){
      var now = 0,prev =0;
      for(var i = 0;i < a.length;i++){  
        prev = Math.max(0,prev + a[i]);
        now = Math.max(prev,now);
      }
      return now;
    }
    
    console.log(SubSequence([-1,-2,-3,4,6,7]));
    

    and it gives 17.

    Does it also work for all positive integer array?

    Yes, it will then give sum of all elements in the array.


    If you want maximal subsequence of length 3, use

    function SubSequence(a,n){
      var now = 0;
      for(var i = 0; i < n; i++) {
        now += a[i];
      }
      var best = now;
      for(var i = n;i < a.length;i++) {
        now += a[i] - a[i-n];
        best = Math.max(now,best);
      }
      return best;
    }
    
    console.log(SubSequence([-1,-2,-3,4,6,7,1,4],3));
    

    the best is 4+6+7, while 4+6+7+1+4 is not allowed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implemented sherlockactionbar in my project and it works great, but I don't know
I implemented a quick search in my application. But when I turned the screen,
I implemented a javascript menu onto my site http://www.ogormanconstruction.co.uk/work If you select 'Work' then
I implemented the delegate-callback pattern between two classes without retaining the delegate. But in
I implemented an http server using javascript and Node.js. for some reason when the
I implemented a solution to the problem meant to be cross-platform and adherent to
I implemented in my app MFMessageComposeViewController for send sms. It works well, but I
I implemented RestSharp succesfully in my WP7 application, but one issue remains: When I
I implemented my Cub3r like this <script type=text/javascript> var flashvars = {}; flashvars.xml =
I implemented an rpc call. It works on local machine but does not work

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.