Given a sorted array of integers, search a given key in the array using binary search

Posted on | Wednesday, 17 May 2023 | No Comments

 Given a sorted array of integers, search a given key in the array using binary search.

Note: Do not use any inbuilt functions/libraries for your main logic.
(Try to practice both iterative and recursive codes for Binary Search)

Input Format

First line of input contains two integers, N - size of the array and K - search key. Second line contains the elements of the sorted array.

Constraints

1 <= N <= 102
0 <= ar[i] <= 109

Output Format

Print "true" if key is present in the array, otherwise, print false.

Sample Input 0

5 19
2 19 23 35 38

Sample Output 0

true
x,y=map(int,input().split())
p=list(map(int,input().split()))
a=0
for i in range(x):
    if p[i]==y:
        print("true")
        break
for i in range(x):
    if p[i]!=y:
        a=a+1
    
if a==x:
    print("false")

Comments

Leave a Reply

Search

Followers