Skip to content
This repository has been archived by the owner on Oct 14, 2023. It is now read-only.

Update 0.cpp #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions Upload Your Code/Binary Search Tree/0.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
#include <iostream>
#include <iomanip>
#include <string.h>

using namespace std;

int main()
{
int binarySearch(int arr[], int left, int right, int target) {
while (left <= right) {
int mid = left + (right - left) / 2;

if (arr[mid] == target)
return mid;

if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}

return -1;
}

int main() {
cout << "enter the size of array";
int size;
cin>> size;
int arr[size];
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
int target ;
cout << "enter the target value"<<endl;
cin>>target;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, target);

if (result == -1)
cout << "Element not present in array" << endl;
else
cout << "Element found at index " << result << endl;

return 0;
}
}