From 2b4a2f42c67ae460365163c0a2f8625c0da8ff88 Mon Sep 17 00:00:00 2001 From: meshajamil <55927465+meshajamil@users.noreply.github.com> Date: Sun, 23 Oct 2022 21:18:44 +0500 Subject: [PATCH 1/3] Update Area of Triangle C++ --- Area of Triangle C++ | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Area of Triangle C++ b/Area of Triangle C++ index 3642502..2462481 100644 --- a/Area of Triangle C++ +++ b/Area of Triangle C++ @@ -1,3 +1,5 @@ +#hacktoberfest + #include #include //to use sqrt function using namespace std; From f82e8cf078811069efe059e34890569d9758553d Mon Sep 17 00:00:00 2001 From: meshajamil <55927465+meshajamil@users.noreply.github.com> Date: Sun, 23 Oct 2022 21:20:32 +0500 Subject: [PATCH 2/3] Update Binary search using C++ --- Binary search using C++ | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/Binary search using C++ b/Binary search using C++ index 25a0a4a..77cd5d6 100644 --- a/Binary search using C++ +++ b/Binary search using C++ @@ -6,38 +6,3 @@ using namespace std; // A recursive binary search function. It returns // location of x in given array arr[l..r] is present, // otherwise -1 -int binarySearch(int arr[], int l, int r, int x) -{ - if (r >= l) { - int mid = l + (r - l) / 2; - - // If the element is present at the middle - // itself - if (arr[mid] == x) - return mid; - - // If element is smaller than mid, then - // it can only be present in left subarray - if (arr[mid] > x) - return binarySearch(arr, l, mid - 1, x); - - // Else the element can only be present - // in right subarray - return binarySearch(arr, mid + 1, r, x); - } - - // We reach here when element is not - // present in array - return -1; -} - -int main(void) -{ - int arr[] = { 2, 3, 4, 10, 40 }; - int x = 10; - int n = sizeof(arr) / sizeof(arr[0]); - int result = binarySearch(arr, 0, n - 1, x); - (result == -1) ? cout << "Element is not present in array" - : cout << "Element is present at index " << result; - return 0; -} From c054db8083253195f99280efcc57b793ae3926cc Mon Sep 17 00:00:00 2001 From: meshajamil <55927465+meshajamil@users.noreply.github.com> Date: Sun, 23 Oct 2022 21:20:45 +0500 Subject: [PATCH 3/3] Update Binary search using C++ --- Binary search using C++ | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/Binary search using C++ b/Binary search using C++ index 77cd5d6..2d6dcfa 100644 --- a/Binary search using C++ +++ b/Binary search using C++ @@ -2,7 +2,39 @@ // C++ program to implement recursive Binary Search #include using namespace std; +int binarySearch(int arr[], int l, int r, int x) +{ + if (r >= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the middle + // itself + if (arr[mid] == x) + return mid; + + // If element is smaller than mid, then + // it can only be present in left subarray + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + // Else the element can only be present + // in right subarray + return binarySearch(arr, mid + 1, r, x); + } + + // We reach here when element is not + // present in array + return -1; +} + +int main(void) +{ + int arr[] = { 2, 3, 4, 10, 40 }; + int x = 10; + int n = sizeof(arr) / sizeof(arr[0]); + int result = binarySearch(arr, 0, n - 1, x); + (result == -1) ? cout << "Element is not present in array" + : cout << "Element is present at index " << result; + return 0; +} -// A recursive binary search function. It returns -// location of x in given array arr[l..r] is present, -// otherwise -1