`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
Please note firstly your example is wrong. Binary search uses sorted array to find index. Also, if there are let's say 7 4's then program should return the first and last 4 index because all other would be in between since array is sorted.
#include<bits/stdc++.h>
using namespace std;
/* if x is present in arr[] then returns the index of
FIRST occurrence of x in arr[0..n-1], otherwise
returns -1 */
int first(int arr[], int low, int high, int x, int n)
{
if(high >= low)
{
int mid = low + (high - low)/2;
if( ( mid == 0 || x > arr[mid-1]) && arr[mid] ==
x)
return mid;
else if(x > arr[mid])
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid -1), x, n);
}
return -1;
}
/* if x is present in arr[] then returns the index of
LAST occurrence of x in arr[0..n-1], otherwise
returns -1 */
int last(int arr[], int low, int high, int x, int n)
{
if (high >= low)
{
int mid = low + (high - low)/2;
if (( mid == n-1 || x < arr[mid+1]) && arr[mid] ==
x)
return mid;
else if (x < arr[mid])
return last(arr, low, (mid -1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
// Driver program
int main()
{
int arr[] = {1, 2, 2, 2, 2, 3, 4, 7, 8, 8};
int n = sizeof(arr)/sizeof(int);
int x = 8;
cout<<"First Occurrence = "<<first(arr, 0, n-1, x,
n);
cout<<"\nLast Occurrence = "<<last(arr, 0, n-1, x,
n)<<endl;
return 0;
}
Kindly revert for any queries
Thanks.
can anyone help me how to write binary search program in c++ that return multiple index.For...
C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...
This is binary search tree problem. The program reads the text file, and creates a binary search tree based on the words in the file. I can create the tree but I also have to store 'the order of insertion' in each node. For example, if text includes "the" 3 times and it is the 1st, 5th, and 9th word in the file, in binary search tree, one node should have string value "the" and array list{1, 5, 9}. How...
(Recursive Binary Search) Write a recursive method recursiveBinarySearch to perform a binary search of an array. The method should receive the search key, starting index and ending index as arguments. If the search key is found, return its index in the array. If the search key is not found, return –1. (NOTE: Complete the recursiveBinarySearch method in the BinaryArray class). java
Can anyone tell me how to write a program for a board game like this using C ++ with OpenGL?
Binary Search Tree Diagram: How do you draw the result of adding multiple elements to a binary search tree? Also, how do you know which level a value appears at, which nodes are the ancestor of the value and how many children does a value have. Thank you! I need help in understanding this.
C++ C++ C++ Write a program that will provide evidence as to which algorithm will search an array faster, Linear (sequential) search or Binary Search. Your program should test many different scenarios. Please help with right answer.
Can someone help me with these C program problems? Thanks 1. Write a C program that creates two arrays of integers, one on the stack and one on the heap. You will loop through these arrays and populate them with values (any value is fine). You will then loop through these arrays again and print each value. Your output should look something like “Value in index 1 is 100 from stack array and 100 from heap array.” Do not forget...
guys can you help me with finding a good Scenario of a c++ program which works by using a BST (binary search tree) in scenario i would just call the functions of the BTC functions like adding deleting ,,, etc
write a c++ program that conducts a binary search using a template in: a set of values of type int, a set of values of type float, a set of values of type double and a set of values of type char. Assume that the array may have up to 10 values.
can anyone help me write a thesis for an essay? my writing task is "Write an essay in which you explore how economics are connected with race OR gender issues in America." I'm stuck and I can't brainstorm anything