-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion7.c
58 lines (53 loc) · 1.18 KB
/
question7.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Given an array containing 1's and 0's in which all 0's appear before all 1's, count the number of 1's in
the array
METHOD1:
linear search and sum
Time complexity: O(n)
Space complexity: O(1)
METHOD2:
Binary search to find where 1 starts
Time complexity: O(logn)
Space complexity: o(1) or O(logn) //iterative or recursive
*/
#include <stdio.h>
#include <stdlib.h>
int countOnes(int *arr, int start, int end,int size){
if(start > end){
return -1;
}
int mid = (start + end)/2;
if(arr[mid] && !arr[mid-1]){
return (size-mid);
}
if(arr[mid] && arr[mid-1]){
return countOnes(arr,start,mid-1, size);
}
return countOnes(arr,mid+1,end,size);
}
int countOnesIterative(int *arr, int start, int end, int size){
while(start <= end){
int mid = (start + end)/2;
if(arr[mid] && !arr[mid-1]){
return (size-mid);
}
if(arr[mid] && arr[mid-1]){
start = start;
end = mid-1;
}else{
start = mid + 1;
end = end;
}
}
}
int main(){
int arr[] = {0,0,0,0,1,1,1,1,1};
int size = sizeof(arr)/sizeof(arr[0]);
int count = countOnesIterative(arr,0,size-1, size);
if(count < 0){
printf("there are no 1s in the array\n");
}else{
printf("number of 1s are %d\n", count);
}
return 0;
}