Binary search problem

pull/1205/head
Bharath 2023-06-21 23:32:03 +05:30
parent d3c2184af8
commit 78db2d6ec5
1 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,43 @@
// You are given a sorted array of distinct integers.
// However, the array has been rotated an unknown number of times in a clockwise direction.
// Your task is to find the smallest element in the rotated array using the binary search algorithm.
// Write a function findSmallestElement that takes in an array of integers, nums, and its size, n, as parameters.
// The function should return the smallest element in the rotated array.
// Note:
// The array nums is rotated in a clockwise direction, which means that some elements from the beginning of the original
// sorted array have been moved to the end in an unknown pattern.
// The array does not contain any duplicate elements.
// C++ program to implement binary search on an array of integers, rotated an unknown number of times.
#include <iostream>
using namespace std;
//Binary search function. It returns the smallest element in the rotated array.
int findSmallestElement(int nums[], int n) {
int low = 0;
int high = n - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (nums[mid] > nums[high]) {
low = mid + 1;
} else {
high = mid;
}
}
return nums[low];
}
int main() {
int nums[] = {5, 6, 8, 9, 10, 2, 3, 4};
int n = sizeof(nums) / sizeof(nums[0]);
int smallest = findSmallestElement(nums, n);
cout << "The smallest element in the rotated array is: " << smallest << endl;
return 0;
}