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
Write a recursive, string-valued method, replace, that accepts a string and returns a new string consisting of the original stri
ch4aika [34]

Answer:

Check the explanation

Explanation:

public String replace(String sentence){

  if(sentence.isEmpty()) return sentence;

  if(sentence.charAt(0) == ' ')

     return '*' + replace(sentence.substring(1,sentence.length()));

  else

     return sentence.charAt(0) +            replace(sentence.substring(1,sentence.length()));

3 0
2 years ago
You would like to conduct a survey and ask your web page visitors to indicate the computer operating systems that they use. Each
Rudiy27

Answer:

<em>A. check box </em>

Explanation:

A check box, selection box, or tick box <em>is a small immersive box that the user can switch to demonstrate an affirmative or negative choice</em>.

It is often observed in applications and operating systems ' HTML input forms, dialog boxes, and GUIs.

A check mark appears inside the box when clicked to signify an affirmative (yes) option. The check mark will vanish when clicking again, suggesting a negative option (no).

6 0
2 years ago
The ____________ protocol enables two users to establish a secret key using a public-key scheme based on discrete logarithms
strojnjashka [21]

The answer in the space provided is the Diffie-Hellman Key Exchange. This is entitled to be the first published key algorithm in which has a limit in which the exchange occurs in secret values and that its purpose was to enable two users to have an exchange of key in a secure manner in means of using it for a subsequent symmetric encryption in terms of messages.

7 0
2 years ago
One subtask in the game is to roll the dice. explain why is roll the dice an abstraction.
LUCKY_DIMON [66]

Answer:

A game is built from a combination of sub-tasks in order to provide the best experience to the user and make sure that the interface is comprises of only the results of the ongoing sub-tasks to provide a higher degree of data abstraction.

Data abstraction refers to the process of representing the essential information without including the background details. Rolling a dice is preferred to be a sub-task so that the user only gets to know about the result of the roll and does not have to wait for or anticipate the result. Moreover, a game may consist of n number of sub-tasks so it is not a good idea to include them in the main framework and are preferred to be abstracted.

4 0
2 years ago
The most common data link layer protocol for wired connections is _____.
harkovskaia [24]

The answer is Ethernet

Ethernet is by far the most popular LAN by a mile. It is a group of protocols that work at either the Data link layer or the Physical layer of the OSI model. Ethernet in the engineering world is defined as the IEEE 802.3 specification.






7 0
2 years ago
Other questions:
  • Shaniya has misspelled a scientific name in her biology report. She needs to correct it, but she has no access to a computer. Sh
    13·2 answers
  • Read this excerpt from The Outsiders. Or I could have gotten one of the gang to come along, one of the four boys Darry and Soda
    6·2 answers
  • What is printed by the following program provided all necessary standard header files are included? Explain each line of the out
    10·1 answer
  • Identifying Characters
    11·2 answers
  • "In about 100 words, discuss the technologies that Walmart’s trucking fleet might use to better manage their operations. Include
    7·1 answer
  • What is retrieved from a search containing two terms separated<br> by the AND operator?
    12·1 answer
  • Write a function in the cell below that iterates through the words in file_contents, removes punctuation, and counts the frequen
    6·1 answer
  • What is the function of napier's bones<br>​
    8·1 answer
  • Sang ayon kaba sa pahayag na walang sinuman ang nabubuhay para sa sarili lamang? bakit?
    15·1 answer
  • Multiple Choice: We have been assigned the task of developing a software testing tool (tester) that can assess reachability of s
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!