Given an array arr[] sorted in ascending order of size N and an integer K. Check if K is present in the array or not.
Example 1:
Input:
N = 5, K = 6
arr[] = {1,2,3,4,6}
Output: 1
Exlpanation: Since, 6 is present in 
the array at index 4 (0-based indexing),
output is 1.
 
Example 2:
Input:
N = 5, K = 2
arr[] = {1,3,4,5,6}
Output: -1
Exlpanation: Since, 2 is not present 
in the array, output is -1.
 
Your Task:
You don't need to read input or print anything. Complete the function searchInSorted() which takes the sorted array arr[], its size N and the element K as input parameters and returns 1 if K is present in the array, else it returns -1. 
Expected Time Complexity: O(Log N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 10^{6}
1 <= K <= 10^{6}
1 <= arr[i] <= 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Complete search

Starter code: #User function Template for python3

class Solution:
    ##Complete this function
    def searchInSorted(self,arr, N, K):
        #Your code here

Solutions:
class Solution:

	def searchInSorted(self, arr, N, K):
		left = 0
		right = N - 1
		while left <= right:
			mid = (left + right) // 2
			if arr[mid] == K:
				return 1
			elif arr[mid] > K:
				right = mid - 1
			else:
				left = mid + 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, K):
		low = 0
		high = N
		while low <= high:
			mid = (low + high) // 2
			if arr[mid] > K:
				high = mid - 1
			elif arr[mid] < K:
				low = mid + 1
			else:
				return 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, K):
		(l, r) = (0, len(arr) - 1)
		while r >= l:
			mid = (l + r) // 2
			if arr[mid] == K:
				return 1
			elif arr[mid] > K:
				r = mid - 1
			else:
				l = mid + 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, k):
		(first, last) = (0, N - 1)
		while first <= last:
			mid = first + (last - first) // 2
			if arr[mid] == k:
				return 1
			elif arr[mid] > k:
				last = mid - 1
			else:
				first = mid + 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, K):
		for i in range(N):
			if arr[i] == K:
				return 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, K):
		start = 0
		end = N - 1
		while start <= end:
			mid = (start + end) // 2
			if arr[mid] == K:
				return 1
			elif arr[mid] > K:
				end = mid - 1
			else:
				start = mid + 1
		return -1


def binary(arr, k):
	mid = len(arr) // 2
	if mid == 0 and arr[mid] != k:
		return -1
	if arr[mid] == k:
		return 1
	elif arr[mid] < k:
		return binary(arr[mid + 1:], k)
	elif arr[mid] > k:
		return binary(arr[:mid], k)

class Solution:

	def searchInSorted(self, arr, N, K):
		return binary(arr, K)


class Solution:

	def searchInSorted(self, arr, N, K):
		if K in arr:
			return 1
		else:
			return -1


class Solution:

	def searchInSorted(self, arr, N, K):
		for i in arr:
			if K in arr:
				return 1
				break
		else:
			return -1


class Solution:

	def searchInSorted(self, arr, N, k):
		if k in arr:
			return 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, K):
		s = 0
		e = N
		while s <= e:
			mid = s + (e - s) // 2
			if arr[mid] == K:
				return 1
			elif arr[mid] > K:
				e = mid - 1
			else:
				s = mid + 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, K):
		try:
			value = arr.index(K)
			return 1
		except:
			return -1


class Solution:

	def searchInSorted(self, arr, N, k):
		l = 0
		h = N - 1
		while l <= h:
			m = (l + h) // 2
			if k == arr[m]:
				return 1
			elif k > arr[m]:
				l = m + 1
			else:
				h = m - 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, K):
		s = 0
		l = N - 1
		while s <= l:
			m = s + (l - s) // 2
			if arr[m] == K:
				return 1
			elif arr[m] > K:
				l = m - 1
			elif arr[m] < K:
				s = m + 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, k):
		l = 0
		hi = N - 1
		while l <= hi:
			mid = (l + hi) // 2
			if arr[mid] < k:
				l = mid + 1
			elif arr[mid] == k:
				return 1
			elif arr[mid] > k:
				hi = mid - 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, K):
		i = 0
		maxs = N
		mins = 0
		while mins <= maxs:
			mid = (maxs + mins) // 2
			if arr[mid] == K:
				return 1
			if arr[mid] < K:
				mins = mid + 1
			if arr[mid] > K:
				maxs = mid - 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, K):
		count = 0
		for i in range(0, N):
			if arr[i] == K:
				count = count + 1
		if count == 1:
			return '1'
		else:
			return '-1'


class Solution:

	def searchInSorted(self, arr, N, K):
		i = 0
		j = N - 1
		while i <= j:
			mid = i + (j - i) // 2
			if arr[mid] == K:
				return 1
			elif arr[mid] > K:
				j = mid - 1
			else:
				i = mid + 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, k):
		j = 0
		n = N - 1
		while j <= n:
			m = (j + n) // 2
			if arr[m] == k:
				return 1
			elif k < arr[m]:
				n = m - 1
			else:
				j = m + 1
		return -1


class Solution:

	def searchInSorted(self, arr, N, K):
		for val in arr:
			if val == K:
				return 1
		return -1


import math

class Solution:

	def searchInSorted(self, arr, N, K):
		m = math.floor(N / 2)
		while m >= 0 and m < N:
			if arr[m] > K:
				m -= 1
			elif arr[m] < K:
				m += 1
			else:
				return 1
		return -1


class Solution:

	def searchInSorted(self, arr, n, k):
		low = 0
		high = n
		for _ in range(n // 2 + 1):
			mid = (low + high) // 2
			if arr[mid] == k:
				return 1
			elif k > arr[mid]:
				low = mid
			elif k < arr[mid]:
				high = mid
		else:
			return -1