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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:28:05+00:00 2026-06-12T13:28:05+00:00

The following method op belongs to a class with two private integer-valued instance variables,

  • 0

The following method op belongs to a class with two private integer-valued instance variables, n
and counter, both of which are initialised to the value zero in the constructor, and subsequently only
modified by method op.

public void op()
{
    if(counter<100)
    {
        op1(); //method with O(1) time complexity
        counter++;
    }else {
        op2(); //method with O(n^2) time complexity
        counter = 0;
    }
    n++;
}

Assuming that method op1 has time complexity O(1) , and method op2 has time complexity O(n^2), which of the following best represents the amortized time complexity of method op?

A) O(n)

B) O(n log n)

C) O(1)

D) O(n^2)

E) O(n3)

where the answer to the exam was D. I think it should have been C as from my understanding of amortized time, you count what will occur most of the time. In this situation, the worst case is O(n^2), however most of the time the algorithm will run in O(1). Why is it O(n^2)?

  • 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-12T13:28:06+00:00Added an answer on June 12, 2026 at 1:28 pm

    When talking about amortized runtime, you can not count what will occur most of the time.
    To start, how do you even define most of the time?
    The amortized runtime of an operation can be seen as the average runtime of the operation.

    Now to your problem:

    For simplicity, I will assume that you wrote if (counter < 99) instead of if (counter < 100). This way, the operation repeats itself after 100 cycles instead of after 101 cycles.

    When writing O(...), in the following, I will actually mean Θ(...), because otherwise the answer to your question would be trivial, as everything which is O(1) is also O(n^2).

    After calling op() 100 times, the total run time would be 99 + 100^2.
    After calling op() 200 times, the total run time would be 2 * 99 + 100^2 + 200^2.
    Now let’s just forget about those 99 or 2 * 99, as they are dominated by the n^2 values.
    So after calling op() n times, the total run time would be something like 100^2 + 200^2 + ... + n^2 (for simplicity, let’s assume that n is divisible by 100).

    Now I will show that this is in O(n^3).

    Let k = n/100
    
    100^2 + 200^2 + ... + n^2
    = 100^2 * (1^2 + 2^2 + ... + k^2)
    =(*) O(100^2 * k * k^2)
    = O(k^3)
    = O(n^3)
    
    (*): sum from 1 to k of i^2 is k (k+1) (2k+1) / 6 = O(k^3)
    

    So finally, the average runtime of op() is O(n^3 / n) = O(n^2). Therefore, the amortized runtime of op() is O(n^2).

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

Sidebar

Related Questions

The following method calculates cost. It access other classes when required. Cloud class holds
I have the following method, which takes in the name of a file as
I encountered the following problem with MongoID. I've created two models which related by
I have two questions: Does method f_1 belong to the metaclass anonymous class? Does
I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I have the following two simple models for users and user groups: class User
I have the following two models, User.. class User < ActiveRecord::Base has_and_belongs_to_many :sites end
I have the following two models: class PhotoAlbum < ActiveRecord::Base belongs_to :project has_many :photos,
The following method, when called with something like String val = getCell(SELECT col FROM
The following method is launched from the constructor of UserControl. A cross thread exception

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.