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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:18:20+00:00 2026-05-30T15:18:20+00:00

Couple very basic time complexity related questions here: What is the time complexity of

  • 0

Couple very basic time complexity related questions here:

  1. What is the time complexity of array initialization in java? e.g.,

    int arr[] = new int[5000];

    I am guessing it to be O(n) or does the JVM invokes some crazy hardware voodoo magic that it takes only O(1)?

  2. What about inserting or retrieving elements using array index?

    void setNumber(int number, int arrIndex) {
      arr[arrIndex] = number;
    }
    
    int getNumber(int arrIndex) {
      return arr[arrIndex];
    }
    

    I am guessing this to be O(1). But say if arrIndex is 4999 then, wouldn’t the pointer that is pointing to the head of the array calculates that the item to be retrieved is at head+(size of int)*arrIndex and ‘moves’ n (~5000) position to retrieve that item thus making the complexity O(n) and not O(1).

    Perhaps we do not consider things at hardware level and conveniently call it the ‘constant’ associated with the time complexity? Could someone please clarify? Thanks!

  • 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-30T15:18:21+00:00Added an answer on May 30, 2026 at 3:18 pm

    You’ve asked about two thing, which are usually O(1). The former is more complex than the latter, so I will address them in reverse order.

    Array Indexing

    Recall that ‘RAM’ stands for “Random Access Memory”. This means that (cache effects aside) accessing one portion of ram will be equally as fast as accessing any other. This “moving” of n you refer to simply doesn’t happen; the ram circuitry is designed such that whatever you set the address pins to will be on the output pins the next cycle.

    Memory Allocation

    This is complicated by the fact that there’s an allocator doing a bunch of work behind the scenes. There’s a wealth of information on Wikipedia, but in a general (but ugly) sense, the answer here is “O(1) except when it isn’t.”

    Even if the array was guaranteed to be zero-filled, you could still see O(1), since many platforms map memory which has already been zero-filled into your process. If you’re really curious you could look at the calloc(zero-filled allocation) implementation in Doug Lea’s Malloc. Of course, to undersand what goes on below that, you’ll need another book, or have to venture into the kernel source.

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

Sidebar

Related Questions

A couple of very basic questions. I am new to WCF and I am
I am a very new programmer, I have made a couple basic applications, however
I am very new to Flex (started learning a couple of days back), I
When we initially setup version control we had very limited time and only basic
im very new to this and have a very basic knowledge php and SQL
I'm very new to ruby & Sinatra and I have a basic question: The
There have been a couple of questions very close to this topic, but none
Couple of months ago, we revamped our web site. We adopted totally new site
couple of quick questions 1) In the local search results - we can get
A couple of recent questions discuss strategies for naming columns, and I was rather

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.