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

  • Home
  • SEARCH
  • 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 6157625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:49:04+00:00 2026-05-23T20:49:04+00:00

Someone asked me a brainteaser, and I don’t know; my knowledge slows down after

  • 0

Someone asked me a brainteaser, and I don’t know; my knowledge slows down after amortized analysis, and in this case, this is O(n).

public int findMax(array) {
  int count = 0;
  int max = array[0];
  for (int i=0; i<array.length; i++) {
    if (array[i] > max) {
      count++;
      max = array[i];
    }
  } 
  return count;
}

What’s the expected value of count for an array of size n?

Numbers are randomly picked from a uniform distribution.

  • 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-23T20:49:04+00:00Added an answer on May 23, 2026 at 8:49 pm

    Let f(n) be the average number of assignments.

    Then if the last element is not the largest, f(n) = f(n-1).

    If the last element is the largest, then f(n) = f(n-1) + 1.

    Since the last number is largest with probability 1/n, and not the largest with probability (n-1)/n, we have:

    f(n) = (n-1)/n*f(n-1) + 1/n*(f(n-1) + 1)
    

    Expand and collect terms to get:

    f(n) = f(n-1) + 1/n
    

    And f(1) = 0. So:

    f(1) = 0
    f(2) = 0 + 1/2
    f(3) = 0 + 1/2 + 1/3
    f(4) = 0 + 1/2 + 1/3 + 1/4
    

    That is, f(n) is the n_th “Harmonic number”, which you can get in closed form only approximately. (Well, one less than the n_th Harmonic number. The problem would be prettier if you initialized max to INT_MIN and just let the loop run, so that f(1) = 1.)

    The above is not a rigorous proof, since I was sloppy about expected values versus actual values. But I believe the answer is right anyway :-).

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

Sidebar

Related Questions

Someone asked me this today after a failed file move, and I was not
In this question someone asked for ways to display disk usage in Linux. I'd
In this SO question, someone asked for calculating an angle from three points .
In this question , someone asked how to write a validation method for Core
OK so I checked and it doesn't seem someone asked this question. So I
This is a fairly odd question that someone asked me recently and I couldn't
While writing some particularly complex exception handling code, someone asked, don't you need to
Someone asked this question about a year ago but I was unable to turn
Someone asked this a year ago and there were none. Are there any now?
Someone asked me this the other day, and I couldn't think of a good

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.