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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:38:01+00:00 2026-06-10T20:38:01+00:00

I am trying to figure out the complexity of the function below. I am

  • 0

I am trying to figure out the complexity of the function below. I am guessing it would be O(n), because the Random class produces a random number in O(1) and put() and containsKey() are also O(1).

But since there is a do-while inside the loop, I wasn’t sure if the complexity would change since random() could be called multiple times if the value is contained in the HashMap. I would appreciate any help!

HashMap<Integer, Integer> values = new HashMap();
for(int i=0 ; i<a.length; i++){
    do{
        // set variable random to some integer between 0 and a.length using Random class in Java, 0 is included. 
    }while(values.containsKey(random) == true);
    b[i] = a[random]
    values.put(random,0);
}

The length of the array is around 1000 and the random number generated is anywhere from 0 to 999.

  • 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-10T20:38:03+00:00Added an answer on June 10, 2026 at 8:38 pm
     Building the map elements: O(n)--->for loop
    
     Checking if the random value is in the map: O(1+α) with a good hash function
     Trying to find a random value which your map does not have: O(n)
     (because you are using integer. Even floats would give very good resolution)
    
    
     array length=1000, random-value range=1000(from 0 to 999)
     think you are at the last element. probability of getting proper value is:
     %0.1 which takes an average of 1000 trials only for the last element (n)
     nearly same for the (n-1)st element (%0.2-->n/2 trials)
     still nearly same for (n-2)nd element (%0.3-->n/3 trials)
    
     what is n+n/2 + n/3 + n/4 + n/5  ... + 1 = a linear function(O(n)) +1 
     α2=1 but neglecting :) and this is on top of a O(n)
    
    
     Result: O(n*n+α*n*n) close to O(n*n)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out the complexity of a for loop using Big
Trying to figure out how to apply a common class to jQuery Hover but
I'm trying to figure out how to give a worst case time complexity. I'm
After spending a couple of days trying figure out why opencv DFT would give
Trying to figure out if it's possible and difficultly level around the below problem
Trying to figure out how I can do this properly. The print_r looks like
trying to figure out why this is happening - I have an input text
Trying to figure out why my silverlight app suddenly just displays nothing (right click
Trying to figure out how to type (via events not set the value) on
Trying to figure out a regex for validating a network path ie: \\comp\xyz or

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.