Knowee
Questions
Features
Study Tools

Given an array of integers nums sorted in non-decreasing order, write a program to find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. Do it using binary search.Input: nums = [5,7,7,8,8,10], target = 8Output: [3,4]Explanation: The first occurrence of 8 is at index 3 and last occurrence of 8 is 4

Question

Given an array of integers nums sorted in non-decreasing order, write a program to find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. Do it using binary search.Input: nums = [5,7,7,8,8,10], target = 8Output: [3,4]Explanation: The first occurrence of 8 is at index 3 and last occurrence of 8 is 4

🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, we can use a modified version of the binary search algorithm. Here are the steps to find the starting and ending positions of the target value in the given array:

  1. Initialize two variables, "start" and "end", to -1. These variables will store the starting and ending positions of the target value.

  2. Perform a binary search on the array to find the first occurrence of the target value. To do this, set the "start" variable to the index of the middle element of the array.

  3. If the middle element is equal to the target value, check if it is the first occurrence by comparing it with the previous element. If it is the first occurrence or there is no previous element, update the "start" variable to the current index.

  4. If the middle element is greater than the target value, update the "end" variable to the current index minus one and continue the binary search on the left half of the array.

  5. If the middle element is less than the target value, update the "start" variable to the current index plus one and continue the binary search on the right half of the array.

  6. Repeat steps 2-5 until the binary search is complete and the "start" variable is the index of the first occurrence of the target value.

  7. If the "start" variable is still -1, it means the target value is not present in the array. In this case, return [-1, -1].

  8. Reset the "end" variable to -1 and perform another binary search to find the last occurrence of the target value. To do this, set the "end" variable to the index of the middle element of the array.

  9. If the middle element is equal to the target value, check if it is the last occurrence by comparing it with the next element. If it is the last occurrence or there is no next element, update the "end" variable to the current index.

  10. If the middle element is greater than the target value, update the "end" variable to the current index minus one and continue the binary search on the left half of the array.

  11. If the middle element is less than the target value, update the "start" variable to the current index plus one and continue the binary search on the right half of the array.

  12. Repeat steps 8-11 until the binary search is complete and the "end" variable is the index of the last occurrence of the target value.

  13. Return the values of the "start" and "end" variables as the starting and ending positions of the target value in the array.

By following these steps, we can efficiently find the starting and ending positions of the target value in the given array using binary search.

This problem has been solved

Similar Questions

Alex wants to create a program to search a target value in a sorted array. The program should input the array size and elements, as well as the target value. Utilizing binary search, it should determine if the target is present, and if so, provide the index. If not, display a message indicating its absence.Assist Alex in completing the program efficiently.Input format :The first line of input consists of an integer N, representing the size of the sorted array.The second line consists of N space-separated integers, the elements of the sorted array in ascending order.The third line consists of an integer target, the value to search for in the array.Output format :If the target is present in the array, print "The target value X is present at index Y", where X is the target element and Y is the index position (index starts from 0).If the target is not present, print "The target value X is not present in the array", where X is the target element.

The following steps are required to implement the binary search for an array sorted in ascending order.1) 1)Initialize Pointers: - Set two pointers, `low` and `high`, initially pointing to the start and end of the array, respectively. 2) Calculate Midpoint: - Calculate the midpoint index using the formula `mid = (low + high) // 2`. 3) Compare Midpoint Element: - Compare the element at the midpoint with the target element you are searching for: - If the midpoint element is equal to the target, the search is successful, and you can return the index. - If the midpoint element is less than the target, update `low` to `mid + 1` and continue the search in the right half. - If the midpoint element is greater than the target, update `high` to `mid - 1` and continue the search in the left half. 4) Repeat: - Repeat steps 3-4 until the `low` pointer is greater than the `high` pointer. This means the search range has become empty. 5) Return Result: - If the target element is found, return its index. If the target is not found, return a sentinel value (e.g.,-1) to indicate that the element is not in the array.Which steps would require modification if the array were sorted in descending order? Step 2 Step 4 Steps 2 and 4 No modification in steps

Program to demonstrate the use of Binary Search to search a given element in a sorted array in ascending order.

Given a sorted array A of size N, along with an integer target K, implement Binary Search to find the target K in the given array.Input FormatThe first line of input contains an integer N - the size of an array and target K. The second line contains the elements of the array.Output FormatFor each iteration of Binary Search, print the values of low, high, and mid. At the end, if the target K is found in the array, print "True" otherwise, print "False".Constraints1 <= N <= 201 <= A[i] <= 103ExampleInput 19 121 4 6 7 10 11 12 20 23Output 10 8 45 8 6TrueInput 210 213 5 8 11 15 17 19 23 26 30Output 20 9 45 9 75 6 56 6 6False

You are given a sorted array of integers. Write a program that implements a binary search algorithm to find the element with the minimum difference from the given target.Note: This question was asked in CTS coding test.Input format :The first line input consists of an integer N, representing the number of array elements.The second line consists of N space-separated integers, representing the sorted array elements.The third line consists of an integer representing the target element.Output format :The output prints an integer representing the element with the minimum difference from the given target.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.