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.
Question
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.
Solution 1
Sure, here is a Python program that accomplishes the task:
def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
N = int(input("Enter the size of the array: "))
arr = list(map(int, input("Enter the elements of the array in ascending order: ").strip().split()))
x = int(input("Enter the target value: "))
result = binary_search(arr, 0, len(arr)-1, x
Solution 2
Here is a Python program that accomplishes the task:
def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return mid
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
N = int(input("Enter the size of the array: "))
arr = list(map(int, input("Enter the elements of the array in ascending order: ").strip().split()))
x = int(input("Enter the target value: "))
result = binary_search(arr, 0, len(arr)-1, x)
if result != -1:
print(f"The target value {x} is present at index {result}")
else:
print(f"The target value {x} is not present in the array")
This program first defines a function binary_search that performs the binary search operation. It then takes the size of the array, the array elements, and the target value as inputs from the user. It calls the binary_search function with the array, the target value, and the initial low and high indices. If the target value is found, it prints the index at which it is found. If it is not found, it prints a message indicating that the target value is not present in the array.
Similar Questions
Single File Programming QuestionProblem StatementAlex 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.Refer to the sample output for formatting specifications.Code constraints :The given test cases will fall under the following constraints:2 ≤ N ≤ 101 ≤ elements ≤ 1000Sample test cases :Input 1 :514 29 37 48 5948Output 1 :The target value 48 is present at index 3Input 2 :10125 137 248 358 489 587 652 741 823 912850Output 2 :The target value 850 is not present in the array
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
Program to demonstrate the use of Binary Search to search a given element in a sorted array in ascending order.
Write a function that performs binary search on a sorted array.
Sharon is working on a sorted array of integers and wants to determine the count of elements that are less than or equal to a specific key. As her supportive programmer, you are tasked with creating a program to assist Sharon in counting the number of elements that meet this criterion (less than or equal to the given key) using the binary search technique.Input format :The first line of input consists of an integer N, representing the size of the array.The second line consists of N space-separated integers, the elements of the array.The third line consists of an integer key, the value to search for in the array.Output format :The output prints a single integer, representing the count of elements that are less than or equal to the given key.
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.