Question

In: Computer Science

Suppose that each row of an n × n array A consists of 1’s and 0’s...

Suppose that each row of an n × n 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. Assuming A is already
in memory, describe a method running in O(n log n) time (not O(n2) time!) for
counting the number of 1’s in A.

Solutions

Expert Solution

the solution for this problem can be done using left binary search

left binary search is used to find the first occurrence of a number in a sorted array

we can use left binary search algorithm to find the occurrence of first zero in the array and the can subtract the index from total length to get the count of 1's in that array

it operates in o(logn) time

since it is an n*n array the total complexity is o(nlogn)

we can use the following method implemented in c to get the result:

#include<stdio.h>
#include<stdlib.h>
int countones(int *a,int n)//left bin search, works in logn time
{
   int low=0,high=n-1,mid,ind=-1;
   while(low<=high)//until we find 0
   {
       mid=(low+high)/2;//checking mid element
       if(a[mid]==0)//checking if we find 0
       {
           ind=mid;//updating mid
           high=mid-1;//finding 0 in the left part of mid
       }
       else if(a[mid]!=0)//if we find 1
           low=mid+1;
       else
           high=mid-1;
   }
   return ind==-1?n:ind;
}
int main()
{
   int a[1000][1000],n,i,j,count=0;
   printf("Enter n: ");
   scanf("%d",&n);//reading n times
   printf("enter n*n array");
   for(i=0;i<n;i++)//reading array
   {
       for(j=0;j<n;j++)
           scanf("%d",&a[i][j]);
   }
   for(i=0;i<n;i++){//repeats n times
       count+=countones(a[i],n);
   }
   printf("%d",count);
   return 0;
}

output:


Related Solutions

Suppose that the array X consists of real numbers X[1], X[2], …, X[N]. Write a pseudocode...
Suppose that the array X consists of real numbers X[1], X[2], …, X[N]. Write a pseudocode program to compute the minimum of these numbers.
An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus...
An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus one integer in this range is missing.) Design an algorithm in ​(Theta(log n)) to find the missing integer. Your algorithm should be given in pseudo code. For example, the array A could be {1, 2, 3, 4, 6, 7, 8, 9, 10} in which 5 is missing.
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n –...
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n – 1)] of n integers. Output: The largest index b such that A[b] is the largest value in A[0..(n – 1)]. For example, if n = 10 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 1, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 4, since the largest value in A is 8 and...
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
Problem: Given an integer array consisting of only 0’s and 1’s and a value k that...
Problem: Given an integer array consisting of only 0’s and 1’s and a value k that denotes distance, determine if all the 1’s are at least k spaces away from each other. Details: The distance between each 1 in the array can be greater than or equal to k places. It doesn’t have to be exactly k Assume the array will only contain 0’s and 1’s – no need to do validation checking If k is greater than the size...
write a function declaration for a 2d array where each row size is 8 and the...
write a function declaration for a 2d array where each row size is 8 and the function does not return anything.
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. IN C Program.
Suppose that every row of M sums to k. Prove that M^n has constant row sums,...
Suppose that every row of M sums to k. Prove that M^n has constant row sums, and find that row sum.
Consider a Markov chain {Xn|n ≥ 0} with state space S = {0, 1, · ·...
Consider a Markov chain {Xn|n ≥ 0} with state space S = {0, 1, · · · } and transition matrix (pij ) given by pij = 1 2 if j = i − 1 1 2 if j = i + 1, i ≥ 1, and p00 = p01 = 1 2 . Find P{X0 ≤ X1 ≤ · · · ≤ Xn|X0 = i}, i ≥ 0 . Q2. Consider the Markov chain given in Q1. Find P{X1,...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a pointer array where each element points to a string). Write an expression that obtains the length of the third string of 's'. You may assume that string.h has been included. 2. Consider the following function whose purpose is to return an array of 3 strings that are initialized to the strings in the character arrays s1, s2, and s3: #include <string.h> #include <stdlib.h> char**...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT