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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:10:52+00:00 2026-06-12T23:10:52+00:00

I am given a question to write a method with a time complexity of

  • 0

I am given a question to write a method with a time complexity of O(4^n).

I have came out with this algorithm:

public void test(int n){
   for(int i = 0; i<n;i++){
      test(4*i);
   }
}

Is this be considered to be running on O(4^n)?

  • 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-12T23:10:53+00:00Added an answer on June 12, 2026 at 11:10 pm

    You were quite close with your program. The correct function would go like this:

    public void test(int n) {
      if (n == 0) return;
      for (int i = 0; i < 4; i++) test(n-1);
    }
    

    Run this piece of code to check:

    static int runs;
    static void test(int n) {
      runs++;
      if (n == 0) return;
      for (int i = 0; i < 4; i++) test(n-1);
    }
    public static void main(String[] args) {
      for (int n = 1; n <= 5; n++) {
        runs = 0;
        test(n);
        System.out.format("%d: %d %d\n", n, 1<<(2*n), runs);
      }
    }
    

    It will print

    1: 4 5
    2: 16 21
    3: 64 85
    4: 256 341
    5: 1024 1365
    

    The run count is off by one, but the big-O complexity is satisfied.

    The reasoning as to why it is O(4n) is probably quite obvious once you see it, but a little explanation cannot hurt. The function is best imagined to be solving a complex problem by divide-and-conquer. It reduces an n-sized problem to four instances of an (n-1)-sized problem, recursing until the subproblem is trivial (size 0). A problem of size 1 is therefore solved in 1+4 steps (entry-point call + 4 trivial subproblems); a problem of size 2 in 1 + 4*(1 + 4) = 21 steps, and so on.

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

Sidebar

Related Questions

I came across this question: say given two weights 1 and 3, u can
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class
I know this is a bit bleeding edge, but here's the question anyway: Given
Please have a look at this: foo = function() { document.write(Unicorn <br>); return this;
Check out this test: http://jsperf.com/delegate-on-vs-bind-5 What I did was delegate the child with a
Hello yeah I'm asking this question a second time, sorry about that but I
I have a question. I am a newbie with Run Time Type Information from
A time ago I already asked this question and I also got a good
I have this method which is dependent on current date. It checks if today
This was an interview question: Given an amount, say $167.37 find all the possible

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.