1

I am not sure, is that question previously asked in SO or not. Well, I was checking frequency of a char in an array. I am pretty weak in determining complexity, so I thought this community can help me to get the point! I am very much sorry, if I posting it with some abstraction! If anyone can help me it will be great!

Here, is my code:

class SearchAChar{
private static int getOccurance(char [] a, char k, int l, int r, int count){
    if(l == r) return count;

    if(a[l] == k){a[l]='0';count++;}

    return getOccurance(a, k, l+1, r, count);   
}
public static void main(String [] args){
    char [] arr = {'a', 'e', 'b', 'c', 'b', 'c', 'd','a'};

    for(int i=0; i<arr.length; i++){
        if(arr[i] == '0') continue;
        System.out.println("Occurance of : " +arr[i] + " is "+ getOccurance(arr, arr[i], i, arr.length, 0) +" times!");
    }
}
}

what should be the run-time complexity of this problems??

3
  • Seems to be linear. Commented Feb 21, 2019 at 10:57
  • yup! it is linear only! :D Commented Feb 21, 2019 at 10:58
  • note that with a Map, you can make this O(n) instead of O(n²) Commented Feb 21, 2019 at 10:59

2 Answers 2

3

Since there is a for-loop and inside for-loop there is a recursive function which has run-time complexity of O(n), that makes worst time complexity is O(n^2) where n is the length of array of char.

Sign up to request clarification or add additional context in comments.

Comments

1

Let's try to decompose; I am talking about the worst case scenario complexity.

n = length of the array

  1. for(int i=0; i<arr.length; i++){} - this does not loop over n times because you update the array by setting seen char to 0 in the recursive function. And if the char is 0 you continue. So it's like O(n/2).

  2. getOccurance(a, k, l+1, r, count) - is recurse over each character until the length is == incrementer. The best way to represent recursive function call stack is as a tree. For example, this image shows how the call stack is being built calculating fibonacci.

enter image description here

But your getOccurance function doesn't invoke itself twice like in above fibonacci function picture. So we could say it has invocations like in one branch of the tree. In other words, here we see the call stack sequence is like 0,1,2... n-1, Therefore, we could calculate the complexity O(n).

If we put those two steps together we get. O(n/2 * n)

But also as @Coderino mentioned - Non dominant terms are not considered in worst case performance.

In conclusion, the complexity of above code is O(n^2).

Some useful resources - https://users.cs.duke.edu/~ola/ap/recurrence.html

Complexity of recursive factorial program

9 Comments

The getOccurance can be easily rewritten into a linear loop; no way this is even remotely O(2^n)
@CoderinoJavarino You may be rewrite it, but this is not a linear loop, can you please elaborate how?
Recursion and loops have same capabilities, and each can be rewritten as an equivalent of the other with the same complexity. This one is trivially a local count var, and a simple for (i = l; i <=r; i++) loop that increments it conditionally depending on a[i]
@CoderinoJavarino you are giving a general statement. How does that relate to the OP's code in the question? Of course, it's conditional for the loop but not for the recursive call. That's why I said the loop is not `O(n).
@LaksithaRanasingha thank you so much for giving time and well detailed explanation! :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.