Find BigO notation for the following function char funchar

Find Big-O notation for the following function :

char fun(char str[])
{
int i=0,count=0;
char ch=\' \';
for(i=0;i<strlen(str)-4;i++)
{
int j=i;
int tempcount=0;
do
{
if(str[i]==str[j])
{
tempcount++;
j++;
if(tempcount>count)
{
ch=str[i];
count=tempcount;
}
}
else
{
break;
}
}while(j<sizeof(str));
}
return ch;

Solution

If we look closely, we can see the purpose of the function is to find the charecter, which is repeated most consecutively.
There are two loops. The loop with i is repeated n-4 times and for each i, inner j loop is repeated n time in the worst case.

So the upper bound will definitely be O(n*n).

the sequence will be n + (n-1) + (n-2) + ....... = O(n*n)

Find Big-O notation for the following function : char fun(char str[]) { int i=0,count=0; char ch=\' \'; for(i=0;i<strlen(str)-4;i++) { int j=i; int tempcount

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site