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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:19:02+00:00 2026-05-20T06:19:02+00:00

I have a working solution for my problem but now I want to improve

  • 0

I have a working solution for my problem but now I want to improve it.

Consider the array

3,4,5,9,1,2,8

I need to find the max difference between two elements at position i and j such that i < j that is I want to find max difference between two elements where the 2nd element comes after 1st element.

In the input I gave the answer is 7 because 8-1 = 7 and 8 is after 1.

The program works but when I have a very large array it takes lot of time. Can we improve on it?

function fMax($arr) 
{
        $sum = $arr[1] - $arr[0];

        for($i=0;$i<count($arr);$i++) 
        {
                for($j=$i+1;$j<count($arr);$j++) 
                {
                        if($sum < $arr[$j] - $arr[$i]) 
                        {
                                $sum = $arr[$j] - $arr[$i];
                        }
                }
        }
        return $sum;
}

Thanks a lot to all the answers. I have used the code by codeaddict and it works fast.

  • 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-20T06:19:02+00:00Added an answer on May 20, 2026 at 6:19 am

    Your current approach is O(N^2) you can improve it to O(N).

    You are comparing each element with every other element. Instead you can keep track of the max difference and min element seen so far.

    So every time you test a new element you see

    • if its difference with the current
      min will give a better max sum if so
      update the max sum and
    • if that number is less than the min
      so far you update the min.

    PHP function:

    function ImprovedFindMax($arr) {
        // initial max sum.
        $sum = $arr[1] - $arr[0];
    
        // initial min.
        $min = $arr[0];
    
        // iterate for every other ele starting from 2nd.
        for($i=1;$i<count($arr);$i++) {
                // if this ele give larger sum then update sum.
            if($arr[$i] - $min > $sum) {
                $sum = $arr[$i] - $min;
            }
    
                // if this ele is smaller than min..update min.
            if($arr[$i] < $min) {
                $min = $arr[$i];
            }
        }
    
        // return $sum which will be the max sum.
        return $sum;
    }
    

    Ideone Link

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

Sidebar

Related Questions

I have a small problem but couldn't find solution for that. I have founded
I'm sure this has been asked before, but I couldn't find a working solution
I've been working on this problem for a few hours now and I have
Right now I have a 2d string array holding my data: //...find number of
Hi I have been working on a solution for days, and I've been trying
Iam working on small booking room system. In my solution I have a Reservation
I have working registration script the only problem is that i do not know
I'm working on a monte carol pricer and I need to improve the efficiency
This question is messy, i dont need a working solution, i need some psuedo
We are currently working with Delphi 2006, but we are now very ready to

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.