Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Let A[1, ..., n] be an array storing a bit (1... 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)
    Ω(n2)
  • b)
    Ω(nlog n) and O(n2)
  • c)
    θ(n)
  • d)
    O(n)
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, ...
Please note that inside the else condition, f() is called first, then counter is set to 0. Consider the following cases:
a) All 1s in A[]: Time taken is Θ(n) as only counter++ is executed n times.
b) All 0s in A[]: Time taken is Θ(n) as only f(0) is called n times
c) Half 1s, then half 0s: Time taken is  Θ(n) as only f(n/2) is called once.
View all questions of this test
Most Upvoted Answer
Let A[1, ..., n] be an array storing a bit (1 or 0) at each location, ...
Please note that inside the else condition, f() is called first, then counter is set to 0. Consider the following cases:
a) All 1s in A[]: Time taken is Θ(n) as only counter++ is executed n times.
b) All 0s in A[]: Time taken is Θ(n) as only f(0) is called n times
c) Half 1s, then half 0s: Time taken is  Θ(n) as only f(n/2) is called once.
Explore Courses for Computer Science Engineering (CSE) exam

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)Ω(n2)b)Ω(nlog n) and O(n2)c)θ(n)d)O(n)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)Ω(n2)b)Ω(nlog n) and O(n2)c)θ(n)d)O(n)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)Ω(n2)b)Ω(nlog n) and O(n2)c)θ(n)d)O(n)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)Ω(n2)b)Ω(nlog n) and O(n2)c)θ(n)d)O(n)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)Ω(n2)b)Ω(nlog n) and O(n2)c)θ(n)d)O(n)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)Ω(n2)b)Ω(nlog n) and O(n2)c)θ(n)d)O(n)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)Ω(n2)b)Ω(nlog n) and O(n2)c)θ(n)d)O(n)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)Ω(n2)b)Ω(nlog n) and O(n2)c)θ(n)d)O(n)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)Ω(n2)b)Ω(nlog n) and O(n2)c)θ(n)d)O(n)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)Ω(n2)b)Ω(nlog n) and O(n2)c)θ(n)d)O(n)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