#include <iostream>
int search(int arr[], int low, int high, int target) {
if (low > high) {
return -1; // Target not found
}
while (low <= high) {
int mid1 = low + (high - low) / 3;
int mid2 = high - (high - low) / 3;
if (arr[mid1] == target) {
return mid1;
} else if (arr[mid2] == target) {
return mid2;
} else if (target < arr[mid1]) {
high = mid1 - 1; // Search left sub-array
} else if (target > arr[mid2]){
low = mid2 + 1; // Search right sub-array
} else {
high = mid2 - 1;
low = mid1 + 1;
}
}
return -1; // Target not found
}
int main() {
int arr[] = {1, 4, 8, 11, 15, 19};
int target = 11;
int index = search(arr, 0, 5, target);
if (index != -1) {
std::cout << "Target " << target << " found at index " << index << std::endl;
} else {
std::cout << "Target " << target << " not found" << std::endl;
}
return 0;
}
#include <iostream>
using namespace std;
void f(int &a, int b) {
a = 3;
b = 10;
}
void g(int *i) {
*i = *i * 10;
}
void print(int* arr) {
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << "\n";
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = &arr[3];
print(arr);
cout << "\n";
int a = 10;
int b = 5;
f(a, b);
cout << a << " " << b << endl;
g(arr);
print(arr);
g(arr + 1);
print(arr);
ptr[0] = 400;
print(arr);
return 0;
}
"Hello"
"olleH"
gcd(a, b) = gcd(b, a % b)
gcd(a, 0) = a
GCD(12, 18) = 6
Missing to return the value. </br>
Using ch!=’a’ && ch!=’e’ … instead of … </br>
Confusing = with == </br>
do not use efficient power </br>
Writing calling code </br>