answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ozzi
2 years ago
15

Suppose that each row of an n×n array A consists of 1’s and 0’s such that, in any row i of A, all the 1’s come before any 0’s in

that row. Suppose further that the number of 1’s in row i is at least the number in row i+ 1, for i = 0, 1,...,n− 2. Assuming A is already in memory, describe a method running in O(n) time (not O(n2) time) for counting the number of 1’s in the array A.
Computers and Technology
1 answer:
Anna71 [15]2 years ago
8 0

Answer:

Check the explanation

Explanation:

  •    Each row of nxn array A consists of 1’s and 0’s such that , in any row of A, all the 1’s come before any 0’s in that row.
  •    Use binary search algorithm to find the index of the last 1 in a row.
  •    Perform this process for each row.
  •    Now, searching for last occurrence of 1 in a row will take O (log n) time.
  •    There are n such rows, therefore total time will be O (n log n).

Complexity analysis:

   The method would be to use binary search for each row to find the first zero starting with index of A[i][n/2+1].

   Let’s say j=n/2.

   The number of 1’s in a row would be j+1.

   This would take O (log n).

   An algorithm that divides by 2 until the number gets sufficiently small then it terminates in O (log n) steps.

   As there are n rows the complexity would be O (n log n).

Pseudo-code:

A = [[1,0,0,0],[0,0,0,0],[1,1,1,1],[1,1,0,0]]

n=4

c=0

for i in range(n): # Loop in rows

  j = n/2 # Search from middle index

  while j>0: # Loop in column

      if(A[i][j]==0): # search for first zero

          if(A[i][j-1]==1): # confirm first zero

              c = c+j # add 1's count to c

              break

          else: # reduce index by 1 or j/2

              if(j/2 == 0):

                  j = j-1

              else:

                  j = j - j/2

      else: # increase index by 1 or j/2

      if(j/2 == 0):

      j = j+1

      else:

          j = j + j/2

      if(j==n): # For all 1's

      c = c+n

      break  

print c

You might be interested in
The primary function of application software is to apply the power of the computer to enable people, workgroups, and the entire
Nadusha1986 [10]

Answer:

True

Explanation:

In this question, some information is missing. The question does not describe which type of question is this.So the correct question is it is a True/False question.

Application software is the collection of the program which is design for the purpose of end-user without the system software the application software is nothing. The main objective to introduce application software for reducing the effort of humans and we can do any task in a very easy manner.

The main objective of application software to enable the people to work groups or the entire enterprise to solve the problems and perform the specific tasks.

5 0
2 years ago
A signal travels from point A to point B. At point A, the signal power is 100 W. At point B, the power is 90 W. What is the atte
11Alexandr11 [23.1K]

Answer:

Attenuation = 0.458\ db

Explanation:

Given

Power at point A = 100W

Power at point B = 90W

Required

Determine the attenuation in decibels

Attenuation is calculated using the following formula

Attenuation = 10Log_{10}\frac{P_s}{P_d}

Where P_s = Power\ Input and P_d  = Power\ output

P_s = 100W

P_d = 90W

Substitute these values in the given formula

Attenuation = 10Log_{10}\frac{P_s}{P_d}

Attenuation = 10Log_{10}\frac{100}{90}

Attenuation = 10 * 0.04575749056

Attenuation = 0.4575749056

Attenuation = 0.458\ db <em>(Approximated)</em>

7 0
2 years ago
Sarah maintains a blog about her soap-making business, and she has hired someone to create a database for this business. She mak
ivann1987 [24]
The database planner would most likely create a table that contains customer contact information since these would be the individuals who placed an order for Sarah's products after visiting her blog. 
4 0
2 years ago
Can you find the reason that the following pseudocode function does not return the value indicated in the comments? // The calcD
Alexus [3.1K]

Answer:

see explaination

Explanation:

Function Real calcDiscountPrice(Real price, Real percentage) // Calculate the discount.

Declare Real discount= (price * percentage) / 100.0 // Subtract the discount from the price.

//dividing by 100.0 because of % concept

Declare Real discountPrice = price - discount // Return the discount price.

Return discount End Function

5 0
1 year ago
Which type of drivers must always be certified in order to be installed in windows?
motikmotik
64 bit drivers must be certified in order to work. 
7 0
2 years ago
Read 2 more answers
Other questions:
  • Which act was used to penalize Sara? Sara was found to be in possession of a controlled substance for which she had no justifica
    5·1 answer
  • Digital cellphones use __________ to reduce the size of the channels that are required by scattering the digital fragments of co
    9·1 answer
  • Which two factors most influenced the growth of the Internet during the 1970s?
    7·2 answers
  • Which of the following facts determines how often a nonroot bridge or switch sends an 802.1D STP Hello BPDU message?
    14·1 answer
  • PYTHON QUESTION
    15·1 answer
  • Question 1 :Which type of unshielded twisted pair (UTP) cable is commonly used for 1000BASE-T Ethernet networks and is often mad
    8·1 answer
  • array of String objects, words, has been properly declared and initialized. Each element of words contains a String consisting o
    11·1 answer
  • 3.14 LAB: Simple statistics for Python
    7·1 answer
  • You resurrected an old worksheet. It appears to contain most of the information that you need, but not all of it. Which step sho
    10·1 answer
  • The given SQL creates a Movie table with an auto-incrementing ID column. Write a single INSERT statement immediately after the C
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!