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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:25:33+00:00 2026-05-26T15:25:33+00:00

Given an array of integers, what is the maximum sum of a subset of

  • 0

Given an array of integers, what is the maximum sum of a subset of the integers such that the integers in the subset were not originally next to each other?

Examples:

[3, 8, 4] => max sum is 8, since 8 > (3+4)
[12, 8, 9, 10] => max sum is 12 + 10 = 22, since this is greater than 12 + 9 and 8 + 10

I’m interested in figuring out the algorithm for doing this. Methodology / thinking process = greatly appreciated.

EDIT:
The integers range from 1 to 1000, inclusive. (Since this is entirely a learning exercise, I’d still be interested in learning about different methods if the integers ranged from, say, -1000 to 1000.)

  • 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-26T15:25:34+00:00Added an answer on May 26, 2026 at 3:25 pm

    Let you have array A {Ai, 1 <= i <= n }

    F(i) – Maximum sum of subarray Aj { 1 <= j <= i }, then

    F(0) = 0 – empty subarray

    F(1) = A(1) – only first element

    F(i) = max(F(i-2) + A(i), F(i-1)) , 2 <= i <= n
    

    F(n) – answer

    C++ implementation:

    int GetMaximumSubarraySum(const vector<int>& a)
    {
        // note that vector a have 1-based index
        vector<int> v(a.size());
        v[0] = 0;
        v[1] = a[1];
        for(int i =2; i < a.size(); i++)
            v[i] = max(v[i-2] + a[i], v[i-1]);
    
        return v.back();
    }
    

    Explanation:

    First, main idea is to use dynamic programming.
    We try to solve task for array with N element by using known answer for array with N-1 and N-2 first elements. If N = 0 the answer is 0 and if N = 1 the answer is A[1]. It’s clear. For N >= 2 we have 2 different ways:

    1. Use element A[N], then the answer is A[N] + F[N-2] (because we can’t use A[N-1] element and F[N-2] is the best solution for subarray 1..N-2, we don’t care about if F[N-2] element is used or not, this is just the best solution for subarray 1..N-2.

    2. Don’t use element A[N], then the answer is F[N-1] (because we can use A[N-1] element and F[N-1] is the best solution for subarray 1..N-1, also we don’t care about if F[N-1] element is used or not.

    So we need to get max of this 2 situations.
    To solve the task you need calculate F[N] in increasing order and memorize answers.

    Let see at your example:

    [12, 8, 9, 10]

    F[0] = 0

    F[1] = 12 – use 1-st element

    F[2] = max(F[0]+A[2], F[1]) = max(8, 12) = 12 – use 1-st element

    F[3] = max(F[1]+A[3], F[2]) = max(21, 12) = 21 – use 1-st, 3-rd element

    F[4] = max(F[2]+A[4], F[3]) = max(22, 21) = 22 – use 1-st, 4-th element

    The answer is F[4] = 22.

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

Sidebar

Related Questions

Possible Duplicate: Getting the submatrix with maximum sum? Given a 2-dimensional array of positive
Surely there is a framework method that given an array of integers, strings etc
Start with an array of integers so that the sum of the values is
Given an array with integers, with each integer being at most n positions away
This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
Given an array of integers A[1...n-1] where N is the length of array A.
I'm tring to find the maximum weight subsequence of an array of positive integers
Given a minimum integer and maximum integer, I want to create an array which
Given a array of random integers, sort the odd elements in descending order and
I have a 2D array of integers that represent groupings (crystal grains) on a

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.