Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Let A[1, …n] be an array storing a bit ... Start Learning for Free
Let A[1, …n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:
counter = 0;
for (i=1; i<=n; i++)
{ if a[i] == 1) counter++;
else {f (counter);
counter = 0;}
}
The complexity of this program fragment is
  • a)
  • b)
  • c)
  • d)
Correct answer is option 'C'. Can you explain this answer?
Verified Answer
Let A[1, …n] be an array storing a bit (1 or 0) at each location...
The key part in the code is "counter = 0" in the else part as we can see below.
Lets take the best case. This happens when    for all i,and then the loop executes with time complexity θ(1) for each iteration and hence overall time complexity of θ(n) and we can say time complexity of the code fragment is Ω(n) and hence options a and b are false.
Now, consider the worst case. This happens when a[i] = 0 or when else part is executed. Here, the time complexity of each iteration will be θ(Counter) and after each else, counter is reset to 0. Let k iterations go to the else part during the worst case. Then the worst case time complexity will be 
where Xi is the value of the counter when,    is called. But due to Counter =0after each call to f(), we have, 
Since the time complexity is    we can say it is θ(n) Option (C). (Option D is false because the small o
needs the growth rate to be STRICTLY lower and not equal to or lower as the case for big 0)
IF Counter = 0was not there in else part, then time complexity would be    as in worst case we can have equal number of 0's and 1's in array a giving time complexity 
would give 
View all questions of this test
Explore Courses for Computer Science Engineering (CSE) exam

Similar Computer Science Engineering (CSE) Doubts

Top Courses for Computer Science Engineering (CSE)

Let A[1, …n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:counter = 0;for (i=1; i<=n; i++){ if a[i] == 1) counter++;else {f (counter);counter = 0;}}The complexity of this program fragment isa)b)c)d)Correct answer is option 'C'. Can you explain this answer?
Question Description
Let A[1, …n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:counter = 0;for (i=1; i<=n; i++){ if a[i] == 1) counter++;else {f (counter);counter = 0;}}The complexity of this program fragment isa)b)c)d)Correct answer is option 'C'. Can you explain this answer? for Computer Science Engineering (CSE) 2024 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about Let A[1, …n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:counter = 0;for (i=1; i<=n; i++){ if a[i] == 1) counter++;else {f (counter);counter = 0;}}The complexity of this program fragment isa)b)c)d)Correct answer is option 'C'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Let A[1, …n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:counter = 0;for (i=1; i<=n; i++){ if a[i] == 1) counter++;else {f (counter);counter = 0;}}The complexity of this program fragment isa)b)c)d)Correct answer is option 'C'. Can you explain this answer?.
Solutions for Let A[1, …n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:counter = 0;for (i=1; i<=n; i++){ if a[i] == 1) counter++;else {f (counter);counter = 0;}}The complexity of this program fragment isa)b)c)d)Correct answer is option 'C'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of Let A[1, …n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:counter = 0;for (i=1; i<=n; i++){ if a[i] == 1) counter++;else {f (counter);counter = 0;}}The complexity of this program fragment isa)b)c)d)Correct answer is option 'C'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Let A[1, …n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:counter = 0;for (i=1; i<=n; i++){ if a[i] == 1) counter++;else {f (counter);counter = 0;}}The complexity of this program fragment isa)b)c)d)Correct answer is option 'C'. Can you explain this answer?, a detailed solution for Let A[1, …n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:counter = 0;for (i=1; i<=n; i++){ if a[i] == 1) counter++;else {f (counter);counter = 0;}}The complexity of this program fragment isa)b)c)d)Correct answer is option 'C'. Can you explain this answer? has been provided alongside types of Let A[1, …n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:counter = 0;for (i=1; i<=n; i++){ if a[i] == 1) counter++;else {f (counter);counter = 0;}}The complexity of this program fragment isa)b)c)d)Correct answer is option 'C'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Let A[1, …n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is θ(m). Consider the following program fragment written in a C like language:counter = 0;for (i=1; i<=n; i++){ if a[i] == 1) counter++;else {f (counter);counter = 0;}}The complexity of this program fragment isa)b)c)d)Correct answer is option 'C'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev