Question

can anyone help me how to write binary search program in c++ that return multiple index.For...

can anyone help me how to write binary search program in c++ that return multiple index.For instance
if i have 124564.It should give the index of both for value 4 .
0 0
Add a comment Improve this question Transcribed image text
Answer #1

`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.

Add a comment
Know the answer?
Add Answer to:
can anyone help me how to write binary search program in c++ that return multiple index.For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Vectors and Binary Search Trees • Write a program that takes from the user n...

    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...

    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....

    (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...

    Can anyone tell me how to write a program for a board game like this using C ++ with OpenGL?

  • Write a program (a3.py) that has 3 function definitions: magic-sequential(), magic-binary(), and main(). You will write...

    Write a program (a3.py) that has 3 function definitions: magic-sequential(), magic-binary(), and main(). You will write the code for the above functions. Both magic functions (magic-sequential() and magic-binary()) will look for a magic index in a given sorted list of distinct integers. A magic index in a list myList[0 ... n-1] is defined to be an index i such that myList[i] = i . Both functions receive the list as a parameter and return an integer: either the magic index,...

  • Binary Search Tree Diagram: How do you draw the result of adding multiple elements to a...

    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.

  • In C++: Write a C++ function binsearch that carries out the binary search algorithm on a...

    In C++: Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. Using the code below, add your binsearch function to this C++ program. (Do...

  • C++ C++ C++ Write a program that will provide evidence as to which algorithm will search...

    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 write a retirement calculator program in c code. I want to write...

    Can someone help me write a retirement calculator program in c code. I want to write a program that the user can enter their age, amount they wish to save each month, the interest rate, and the age they wish to retire. I would like to use a switch loop, if else loop or do while loop. the output should show them how much they should have in a retirement savings account at the age they wish to retire at....

  • Can someone help me with these C program problems? Thanks 1. Write a C program that...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT