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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:51:11+00:00 2026-06-02T08:51:11+00:00

I have this problem , where given an array of positive numbers i have

  • 0

I have this problem , where given an array of positive numbers i have to find the maximum sum of elements such that no two adjacent elements are picked. The maximum has to be less than a certain given K. I tried thinking on the lines of the similar problem without the k , but i have failed so far.I have the following dp-ish soln for the latter problem

    int sum1,sum2 = 0;
    int sum = sum1 = a[0];

    for(int i=1; i<n; i++)
    {
        sum = max(sum2 + a[i], sum1);
        sum2 = sum1;
        sum1 = sum;
    }

Could someone give me tips on how to proceed with my present problem??

  • 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-02T08:51:14+00:00Added an answer on June 2, 2026 at 8:51 am

    The best I can think of off the top of my head is an O(n*K) dp:

    int sums[n][K+1] = {{0}};
    int i, j;
    for(j = a[0]; j <= K; ++j) {
        sums[0][j] = a[0];
    }
    if (a[1] > a[0]) {
        for(j = a[0]; j < a[1]; ++j) {
            sums[1][j] = a[0];
        }
        for(j = a[1]; j <= K; ++j) {
            sums[1][j] = a[1];
        }
    } else {
        for(j = a[1]; j < a[0]; ++j) {
            sums[1][j] = a[1];
        }
        for(j = a[0]; j <= K; ++j) {
            sums[1][j] = a[0];
        }
    }
    for(i = 2; i < n; ++i) {
        for(j = 0; j <= K && j < a[i]; ++j) {
            sums[i][j] = max(sums[i-1][j],sums[i-2][j]);
        }
        for(j = a[i]; j <= K; ++j) {
            sums[i][j] = max(sums[i-1][j],a[i] + sums[i-2][j-a[i]]);
        }
    }
    

    sums[i][j] contains the maximal sum of non-adjacent elements of a[0..i] not exceeding j. The solution is then sums[n-1][K] at the end.

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

Sidebar

Related Questions

This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
Consider this problem: You are given an array containing positive integers. All the integers
I have an optimization problem as follows. Given an array of positive integers, e.g.
i have this problem to find a particular xml node l have post this
I have this problem that my sites uses alot of ajax and when a
I have this problem decomposing a relation schema into a set of schemas that
I have the following homework problem: Given an array of scores, return true if
Suppose that you're given an array A of n distinct elements drawn from some
Possible Duplicate: Given two arrays a and b .Find all pairs of elements (a1,b1)
I've run into this problem with testing. Let's assume I have two models, User

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.