-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new questions in binary search topic
- Loading branch information
1 parent
f75cdf7
commit db88c6b
Showing
2 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...Topic Wise🔥/Algorithms/Searching/Questions/04. Count 1s in a Sorted Binary Array/code.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define ll long long | ||
#define db double | ||
#define ld long double | ||
#define el "\n" | ||
|
||
/* | ||
Input - | ||
1 | ||
7 | ||
0 0 0 1 1 1 1 | ||
Output - | ||
4 | ||
*/ | ||
|
||
int lastOccurrence(int *arr, int start, int end, int x){ | ||
int ans = -1; | ||
while (start <= end){ | ||
int mid = (start + end)/2; | ||
|
||
if(arr[mid] < x) | ||
start = mid + 1; | ||
else{ | ||
ans = mid; | ||
start = mid + 1; | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
int firstOccurrence(int *arr, int start, int end, int x){ | ||
int ans = -1; | ||
while (start <= end){ | ||
int mid = (start + end)/2; | ||
|
||
if(arr[mid] < x) | ||
start = mid+1; | ||
else{ | ||
ans = mid; | ||
end = mid - 1; | ||
} | ||
} | ||
return ans; | ||
} | ||
|
||
// Time Complexity : O(log n) | ||
// Auxiliary Space : O(1) | ||
int solution(int *arr, int start, int end){ | ||
int x = 1; | ||
|
||
int first = firstOccurrence(arr, start, end, x); | ||
|
||
if(first == -1) | ||
return 0; | ||
else | ||
return (lastOccurrence(arr, start, end, x) - first + 1); | ||
} | ||
|
||
int main(){ | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(NULL); | ||
|
||
int t; | ||
cin >> t; | ||
while (t--){ | ||
int n; | ||
cin >> n; | ||
|
||
int arr[n]; | ||
for (int i = 0; i < n; i++) | ||
cin >> arr[i]; | ||
|
||
cout << "Solution -> " << solution(arr, 0, n - 1) << el; | ||
} | ||
|
||
return 0; | ||
} |