0

Below code is to find how many times a number is shown in an array. For Example:

1,2,2,2,3,4,5,5,5,5 number 2 = 3 times number 5 = 4 times.

What is the time complexity in Java for the below code? What is the best way to solve this problem in respect to Time complexity?

public static void main(String[]args)
    {
        int[] data = {1,1,2,3,4,4,4,5,6,7,8,8,8,8};
        System.out.println(count(data,8));


    }


public static int count(int[] a, int x)
{
    int count=0;
    int index=0;
    while(index<a.length)
    {
        if(a[index]==x)
        {
            count++;
        }
        index++;

    }
    return count;
}
3
  • 1
    You're iterating over the entire array once, so I would guess it's O(N). Commented Jan 6, 2013 at 9:43
  • Please fix your notation. O(n) and o(n) are not the same thing. In some sense they're the opposites of each other. See en.wikipedia.org/wiki/Big_O_notation Commented Jan 6, 2013 at 9:46
  • I am looking at the example above the code: are you trying to find the number of occurrences of all duplicates in a sorted array? Commented Jan 6, 2013 at 9:50

6 Answers 6

3

is it o(n) ,logN or else?

You look at every element once so it is O(n)

if it is o(n), can I can i do this task with LogN complicity?

Do a binary search with a low and high value searching for the value just less than the one you search for and just above. The difference between these two result will tell you how many there are. This is an O(Log N) search.

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

2 Comments

That would be N*log(N) then, if he needs to check for every element.
@iccthedral correct. Technically I would say O(M * log N) where M is the range of numbers. You won't do this if you need to count all elements unless there was a large amount of duplication.
1

while(index<a.length)

This loop is run once per value in data. This is O(n). If you want to do this in O(log n) you will need a sorted array and a binary search. You have a sorted array, so you would just need to do a binary search.

Comments

1

The answer is O(n).

You loop through the array looking once at every index.

e.g. array size is 10 --> 10 comparisons, array size is 100 --> 100 comparisons and so on.

Comments

1

You examine each element of the array, and therefore your code has O(n) time complexity.

To do it in O(log n) time, you have to exploit the fact that the array is sorted. This can be done with a variant of binary search. Since this looks like homework, I'll let you think about it for a bit before offering any more hints.

Comments

0

O(n) This code uses each element of the array.

while(index<a.length)

You can replace it on

for(int index = 0; index < a.length; i++)

Comments

0

O(n).When the code iterates through each and every element of the array,its 0(n).

Comments

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.