retagged by
1,490 views

2 Answers

Best answer
1 votes
1 votes

Lets trace it,

i=1; then j=i=1 => inside A[ i ] != A[ j ] condition false => Execute 'else' part. Break inner loop

i=1; then j=i=2 => inside A[ i ] != A[ j ] condition false => Execute 'else' part. Break inner loop

repeat unto i's counter i.e., n....Thus, it only executes n times. Hence, O(n)

selected by
0 votes
0 votes
See, i=1,j=1, in checking a[i]!=a[j] , both i and j are 1, so a[1]!=a[1] is false; it breaks; hence executed once; similarly , for i=2; j starts with 2, comparison fails, it breaks; so only one time the inner loop gets executed for every value of outer loop;

hence O(n)
Answer:

Related questions

3 votes
3 votes
1 answer
1
gate-17 asked Aug 7, 2016
2,839 views
consider the following functions f(n)=log*(log n) , g(n)=log(log* n) relations between these 2 function. please help
0 votes
0 votes
3 answers
2
gshivam63 asked May 19, 2016
2,896 views
int f(int x){if(x<1) return 1;else return f(x-1) +g(x/2);}int g(int x){if(x<2) return 1;else return f(x-1) +g(x/2);}a. LogarithmicB. QuadraticC. LinearD. Exponential
0 votes
0 votes
1 answer
4
Devshree Dubey asked Jun 8, 2016
519 views
int unknown(int n){ int i, j,k=0;for(i=n/2;i<=n;i++) for(j=2;j<=n;j=j*2)k=k+n/2;return(k);}