Skip to content

Commit

Permalink
Added new questions in binary search topic
Browse files Browse the repository at this point in the history
  • Loading branch information
KaranSiddhu committed Aug 3, 2022
1 parent f75cdf7 commit db88c6b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,55 @@ Input -
1 10 10 10 20 20 40
Output -
5
2
*/

// Time Complexity : O(log n)
// Auxiliary Space : O(1)
int solution(int *arr, int start, int end, int x){

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)
end = mid - 1;
else 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)
end = mid-1;
else if(arr[mid] < x)
start = mid+1;
else{
ans = mid;
end = mid - 1;
}
}
return ans;
}

int solution(int *arr, int start, int end, int x){
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);
Expand Down
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;
}

0 comments on commit db88c6b

Please sign in to comment.