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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:15:33+00:00 2026-06-04T14:15:33+00:00

I am trying to implement the following algorithm using the divide and conquer method

  • 0

I am trying to implement the following algorithm using the divide and conquer method in order to get the running time to O(n*logn).

Given a sequence of numbers a_1, a_2,…, a_n and a number k, find i and j such that 1<= j – i <= k while maximizing a_i + a_j.

For example for the sequence 10,2,0,8,1,7,1,0,11 and k = 2, the maximum value is 15 = 8 + 7.

I have implemented some sort of divide and conquer method, but I’m struggling to figure out how to check values that go across each of the divide intervals. Here is what I have so far:

int MaxInterval(int array[], int left, int right, int k)
{
    int BestSum = 0;
    int sumL = 0;
    int sum = 0;
    int sumR = 0;
    int sumMid = 0;
    int count = 0;
    if( right - left <= 2*k-3 ) //
    {
      //elaborate straightforward search right way
      for(int i = left; i <= right; i++)
      {
          sum = 0;
          count = k;
          for(int j = i+1; j <= right; j++ )
          {
              if(count == 0) break;
              sum = array[i] + array [j];
              if(sum > BestSum) BestSum = sum;
              count--;
          }

      }
      return BestSum;
    }
    int mid = (right + left)/2;
    sumL = MaxInterval(array, left, mid, k);
    sumR = MaxInterval(array, mid + 1, right, k);
    sumMid = MaxInterval(array, max(left, mid - k + 2), min(right, mid + k - 1), k);
    return max(max(sumL, sumR), sumMid);
}

I think I am on some-what of the right track, I’m just struggling to figure out how to incorporate check sums of numbers that go across two of the intervals, without using a brute-force method which would yield O(n^2) complexity.

If there are any pointers or tips on how I can continue this, it would be greatly appreciated. Also, I am currently working under the assumption that there is an even number of integers in the array. Thanks guys.

  • 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-04T14:15:35+00:00Added an answer on June 4, 2026 at 2:15 pm

    Some clues in pseudocode.
    Example for n=8, k = 2 – this code will search the best result from a[0..3], a[4..7] and a[2..5]. Notice that I’ve removed additional arrays.

    int MaxInterval(int array[], int left, int right, int k)
    {
        if( right - left <= 2*k-1 ) //
        {
          //elaborate straightforward search right way
          return BestSum;
        }
        sumL = MaxInterval(array, left, mid, k);
        sumR = MaxInterval(array, mid + 1, right, k);
        sumMid = MaxInterval(array, max(left, mid - k + 1), min(right, mid + k), k);
        return max(sumL, sumR, sumMid);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well am referring the following paper and trying to implement the algorithm as given
I'm trying to implement autocomplete for a textbox using the following code, but it's
I am trying to implement Quick Sort algorithm. Following code works for unique elements
I'm trying to think of a fast algorithm for the following issue. Given a
I trying to implement synchronization algorithm in C#, without success. Why isn't the following
I'm trying to implement the Minimum Distance Algorithm for image classification using GDAL and
i am trying to implement following algorithm: suppose there is array a[3][3], my aim
I'm trying to implement Peterson's algorithm in Java, and have created the following for
I'm trying to implement a delegate class following Herb Sutter's Example . There is
i receive the following error when trying to implement auto-complete based on edismax type.

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.