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 612183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:51:56+00:00 2026-05-13T17:51:56+00:00

Been optimizing an algorithm and came down to the last part. I have an

  • 0

Been optimizing an algorithm and came down to the last part. I have an array of integers like this:

[ 1, 1, 2, 5, 0, 5, 3, 1, 1 ]

My requirement are as follows:

  1. input: numbers of integers to sum over
  2. the max sum should consist of integers next to each other
  3. if an integer has the value 0 the sum in the range would be invalid
  4. the max sum of integers and the index of each integer is returned

Expected results:

Given input of 2 (2 wanted) with the array as mentioned should therefor return
[ 8, [ 5, 6 ] ] where 8 being the sum of integers at index 5 and 6

Given input of 3 (3 wanted) with the array as mentioned should therefor return
[ 9, [ 5, 6, 7 ] ] where 9 being the sum of integers at index 5, 6 and 7 (notice that even though integers at indexes 3, 4, 5 have a higher sum the result is invalid due to index 4 being 0)

I’m currently managing this by doing a lot of looping, but was wondering if somebody had a better way of achieving this. My coding language of choice is currently C# – I would therefor appreciate if possible replies would be in C#. Any use of linq and other fancy Math features is ok as long as it’s the fastest way.

  • 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-13T17:51:56+00:00Added an answer on May 13, 2026 at 5:51 pm

    I think you can solve this in linear time. First, replace all 0s with a very small number (-2^30 for example) so that it won’t affect our sum.

    Then:

    let s[i] = sum of first i integers
    let k = number of integers required
    let max = -inf    
    for ( int i = k; i <= N; ++i )
      if ( s[i] - s[i - k - 1] > max )
         max = s[i] - s[i - k - 1]
    

    You can probably avoid replacing zeros with a few extra conditions. if s[i] = sum of first i integers, then s[i] – s[k – 1] gives you the sum of the integers k through i.

    Edit:
    You can do this in O(1) extra space like this: first replace all 0s.

    then:

    max = cr = sum of first k integers.
    for ( int i = k + 1; i <= N; ++i )
    {
      cr = cr + numbers[i] - numbers[i - k] 
      if ( cr > max )
        max = cr; // also update positions
    }
    

    To avoid replacing zeroes in the first solution, just skip k spaces ahead when encountering a zero. In the second solution, skip k or k + 1 (depends on how you choose to implement this special case) spaces ahead, but be sure to rebuild your cr variable when doing the skip!

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

Sidebar

Ask A Question

Stats

  • Questions 340k
  • Answers 340k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think the answer is NO, but I don't know… May 14, 2026 at 4:48 am
  • Editorial Team
    Editorial Team added an answer You must now use namespace references in css files with… May 14, 2026 at 4:48 am
  • Editorial Team
    Editorial Team added an answer You might be getting bitten by endianness. Is BitConverter.GetBytes returning… May 14, 2026 at 4:48 am

Related Questions

The Situation: I'm optimizing a pure-java implementation of the LZF compression algorithm, which involves
I've been tasked with taking an existing single threaded monte carlo simulation and optimising
I've been reading things here and there for a while now about using an
I am looking for a tool like ltrace or strace that can trace locally
Once upon a time, to write x86 assembler, for example, you would have instructions

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.