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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:02:40+00:00 2026-06-04T16:02:40+00:00

I am asking for your ideas regarding this problem: I have one array A,

  • 0

I am asking for your ideas regarding this problem:

I have one array A, with N elements of type double (or alternatively integer). I would like to find an algorithm with complexity less than O(N2) to find:

    max A[i] - A[j]

For 1 < j <= i < n. Please notice that there is no abs(). I thought of:

  • dynamic programming
  • dichotomic method, divide and conquer
  • some treatment after a sort keeping track of indices

Would you have some comments or ideas? Could you point at some good ref to train or make progress to solve such algorithm questions?

  • 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-04T16:02:41+00:00Added an answer on June 4, 2026 at 4:02 pm

    Make three sweeps through the array. First from j=2 up, filling an auxiliary array a with minimal element so far. Then, do the sweep from the top i=n-1 down, filling (also from the top down) another auxiliary array, b, with maximal element so far (from the top). Now do the sweep of the both auxiliary arrays, looking for a maximal difference of b[i]-a[i].

    That will be the answer. O(n) in total. You could say it’s a dynamic programming algorithm.

    edit: As an optimization, you can eliminate the third sweep and the second array, and find the answer in the second sweep by maintaining two loop variables, max-so-far-from-the-top and max-difference.

    As for “pointers” about how to solve such problems in general, you usually try some general methods just like you wrote – divide and conquer, memoization/dynamic programming, etc. First of all look closely at your problem and concepts involved. Here, it’s maximum/minimum. Take these concepts apart and see how these parts combine in the context of the problem, possibly changing order in which they’re calculated. Another one is looking for hidden order/symmetries in your problem.

    Specifically, fixing an arbitrary inner point k along the list, this problem is reduced to finding the difference between the minimal element among all js such that 1<j<=k, and the maximal element among is: k<=i<n. You see divide-and-conquer here, as well as taking apart the concepts of max/min (i.e. their progressive calculation), and the interaction between the parts. The hidden order is revealed (k goes along the array), and memoization helps save the interim results for max/min values.

    The fixing of arbitrary point k could be seen as solving a smaller sub-problem first (“for a given k…”), and seeing whether there is anything special about it and it can be abolished – generalized – abstracted over.

    There is a technique of trying to formulate and solve a bigger problem first, such that an original problem is a part of this bigger one. Here, we think of find all the differences for each k, and then finding the maximal one from them.

    The double use for interim results (used both in comparison for specific k point, and in calculating the next interim result each in its direction) usually mean some considerable savings. So,

    • divide-and-conquer
    • memoization / dynamic programing
    • hidden order / symmetries
    • taking concepts apart – seeing how the parts combine
    • double use – find parts with double use and memoize them
    • solving a bigger problem
    • trying arbitrary sub-problem and abstracting over it
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Asking for your help on this Oracle query. It's giving me the error 2
i dont exactly know how write down this query, so im asking your guys
I have seen in many questions on SO, asking for your opinion of how
I am asking for your ideas about how to combine multiple GWT based web
I really have tried researching this problem, but I'm now getting so close to
I have two related questions hence I am asking them in this single thread.
I know, I am asking stupid question but your experience will help me to
Simply Asking, Is there any function available in mysql to split single row elements
Im asking to see if anyone could help me with my problem which is
Just asking if I have the right understanding System.out.println(); System is the package out

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.