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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:54:53+00:00 2026-05-26T15:54:53+00:00

Given a snipplet of code, how will you determine the complexities in general. I

  • 0

Given a snipplet of code, how will you determine the complexities in general. I find myself getting very confused with Big O questions. For example, a very simple question:

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        System.out.println("*");
    }
}

The TA explained this with something like combinations. Like this is n choose 2 = (n(n-1))/2 = n^2 + 0.5, then remove the constant so it becomes n^2. I can put int test values and try but how does this combination thing come in?

What if theres an if statement? How is the complexity determined?

for (int i = 0; i < n; i++) {
    if (i % 2 ==0) {
        for (int j = i; j < n; j++) { ... }
    } else {
        for (int j = 0; j < i; j++) { ... }
    }
}

Then what about recursion …

int fib(int a, int b, int n) {
    if (n == 3) {
        return a + b;
    } else {
        return fib(b, a+b, n-1);
    }
}
  • 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-26T15:54:53+00:00Added an answer on May 26, 2026 at 3:54 pm

    In general, there is no way to determine the complexity of a given function

    Warning! Wall of text incoming!

    1. There are very simple algorithms that no one knows whether they even halt or not.

    There is no algorithm that can decide whether a given program halts or not, if given a certain input. Calculating the computational complexity is an even harder problem since not only do we need to prove that the algorithm halts but we need to prove how fast it does so.

    //The Collatz conjecture states that the sequence generated by the following
    // algorithm always reaches 1, for any initial positive integer. It has been
    // an open problem for 70+ years now.
    function col(n){
        if (n == 1){
            return 0;
        }else if (n % 2 == 0){ //even
            return 1 + col(n/2);
        }else{ //odd
            return 1 + col(3*n + 1);
        }
    }
    

    2. Some algorithms have weird and off-beat complexities

    A general “complexity determining scheme” would easily get too complicated because of these guys

    //The Ackermann function. One of the first examples of a non-primitive-recursive algorithm.
    function ack(m, n){
        if(m == 0){
            return n + 1;
        }else if( n == 0 ){
            return ack(m-1, 1);
        }else{
            return ack(m-1, ack(m, n-1));
        }
    }
    
    function f(n){ return ack(n, n); }
    
    //f(1) = 3
    //f(2) = 7
    //f(3) = 61
    //f(4) takes longer then your wildest dreams to terminate.
    

    3. Some functions are very simple but will confuse lots of kinds of static analysis attempts

    //Mc'Carthy's 91 function. Try guessing what it does without
    // running it or reading the Wikipedia page ;)
    function f91(n){
        if(n > 100){
            return n - 10;
        }else{
            return f91(f91(n + 11));
        }
    }
    

    That said, we still need a way to find the complexity of stuff, right? For loops are a simple and common pattern. Take your initial example:

    for(i=0; i<N; i++){
       for(j=0; j<i; j++){
           print something
       }
    }
    

    Since each print something is O(1), the time complexity of the algorithm will be determined by how many times we run that line. Well, as your TA mentioned, we do this by looking at the combinations in this case. The inner loop will run (N + (N-1) + … + 1) times, for a total of (N+1)*N/2.

    Since we disregard constants we get O(N2).

    Now for the more tricky cases we can get more mathematical. Try to create a function whose value represents how long the algorithm takes to run, given the size N of the input. Often we can construct a recursive version of this function directly from the algorithm itself and so calculating the complexity becomes the problem of putting bounds on that function. We call this function a recurrence

    For example:

    function fib_like(n){
        if(n <= 1){
            return 17;
        }else{
            return 42 + fib_like(n-1) + fib_like(n-2);
        }
     }
    

    it is easy to see that the running time, in terms of N, will be given by

    T(N) = 1 if (N <= 1)
    T(N) = T(N-1) + T(N-2) otherwise
    

    Well, T(N) is just the good-old Fibonacci function. We can use induction to put some bounds on that.

    For, example, Lets prove, by induction, that T(N) <= 2^n for all N (ie, T(N) is O(2^n))

    • base case: n = 0 or n = 1
        T(0) = 1 <= 1 = 2^0
        T(1) = 1 <= 2 = 2^1
    
    • inductive case (n > 1):
        T(N) = T(n-1) + T(n-2)
        aplying the inductive hypothesis in T(n-1) and T(n-2)...
        T(N) <= 2^(n-1) + 2^(n-2)
        so..
        T(N) <= 2^(n-1) + 2^(n-1)
             <= 2^n
    

    (we can try doing something similar to prove the lower bound too)

    In most cases, having a good guess on the final runtime of the function will allow you to easily solve recurrence problems with an induction proof. Of course, this requires you to be able to guess first – only lots of practice can help you here.

    And as f final note, I would like to point out about the Master theorem, the only rule for more difficult recurrence problems I can think of now that is commonly used. Use it when you have to deal with a tricky divide and conquer algorithm.


    Also, in your “if case” example, I would solve that by cheating and splitting it into two separate loops that don; t have an if inside.

    for (int i = 0; i < n; i++) {
        if (i % 2 ==0) {
            for (int j = i; j < n; j++) { ... }
        } else {
            for (int j = 0; j < i; j++) { ... }
        }
    }
    

    Has the same runtime as

    for (int i = 0; i < n; i += 2) {
        for (int j = i; j < n; j++) { ... }
    }
    
    for (int i = 1; i < n; i+=2) {
        for (int j = 0; j < i; j++) { ... }
    }
    

    And each of the two parts can be easily seen to be O(N^2) for a total that is also O(N^2).

    Note that I used a good trick trick to get rid of the “if” here. There is no general rule for doing so, as shown by the Collatz algorithm example

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

Sidebar

Related Questions

Which code snippet will give better performance? The below code segments were written in
Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath
Given the following code snippet: $i= 11; function get_num() { global $i; return (--$i
I need to give the user a snippet of js code that will insert
Given this code snippet, I know that if the user selects 25c the value
I need a code snippet for converting amount of time given by number of
Recently i have attended an interview . A code snippet is given to me.I
I have found a code snippet as given below which is used to disable
Someone has given me the following C++ code snippet to try out - and
I'm using Visual C++ 2008 SP1 Pro. Following code snippet will fail to compile:

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.