Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

provide solution to binary search #377

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Beginner Level πŸ“/Binary Search/question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
**Binary Search**

**Problem**
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

You must write an algorithm with O(log n) runtime complexity.

**Example 1:**
**Input:** nums = [-1,0,3,5,9,12], target = 9
**Output:** 4
**Explanation:** 9 exists in nums and its index is 4

**Example 2:**
**Input:** nums = [-1,0,3,5,9,12], target = 2
**Output:** -1
**Explanation:** 2 does not exist in nums so return -1

**Constraints:**
- 1 <= nums.length <= 104
- -104 < nums[i], target < 104
- All the integers in nums are unique.
- nums is sorted in ascending order.
21 changes: 21 additions & 0 deletions Beginner Level πŸ“/Binary Search/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def search_in_a_rotated_sorted_array(arr,n,target):
low,high=0,n-1
while low<=high:
mid=(low+high)//2
if arr[mid]==target:
return mid
elif arr[low]<=arr[mid]: #left is sorted
if arr[low]<=target and target<=arr[mid]:
high=mid-1
else:
low=mid+1
else: #right is sorted
if arr[mid]<=target and target<=arr[high]:
low=mid+1
else:
high=mid-1
return -1

arr=list(map(int,input("Enter the list of numbers : ").split()))
target=int(input("Enter the target number you need to search : "))
print("The target is found in : ",search_in_a_rotated_sorted_array(arr,len(arr),target))
19 changes: 19 additions & 0 deletions Beginner Level πŸ“/ceil in a sorted array/question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
**Ceil The Floor**

**Problem**
Given an unsorted array arr[] of integers and an integer x, find the floor and ceiling of x in arr[].

Floor of x is the largest element which is smaller than or equal to x. Floor of x doesn’t exist if x is smaller than smallest element of arr[].
Ceil of x is the smallest element which is greater than or equal to x. Ceil of x doesn’t exist if x is greater than greatest element of arr[].

Return an array of integers denoting the [floor, ceil]. Return -1 for floor or ceiling if the floor or ceiling is not present.

**Examples:**

**Input:** x = 7 , arr[] = [5, 6, 8, 9, 6, 5, 5, 6]
**Output:** 6, 8
**Explanation:** Floor of 7 is 6 and ceil of 7 is 8.

**Input:** x = 10 , arr[] = [5, 6, 8, 8, 6, 5, 5, 6]
**Output:** 8, -1
**Explanation:** Floor of 10 is 8 but ceil of 10 is not possible.
12 changes: 12 additions & 0 deletions Beginner Level πŸ“/ceil in a sorted array/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def ceil(nums,target):
n=len(nums)
low,high=0,n-1
ans=-1
while low<=high:
mid=(low+high)//2
if nums[mid]>=target:
ans=nums[mid]
high=mid-1
else:
low=mid+1
return ans
16 changes: 16 additions & 0 deletions Beginner Level πŸ“/search insert position/question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Problem
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You must write an algorithm with O(log n) runtime complexity.

Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2

Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1

Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4
16 changes: 16 additions & 0 deletions Beginner Level πŸ“/search insert position/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def search_insert(nums,n,target):
low=0
high=n-1
ans=n
while low<=high:
mid=(low+high)//2
if nums[mid]>=target:
ans=mid
high=mid-1
else:
low=mid+1
return ans

nums=list(map(int,input("Enter the list of numbers : ").split()))
target=int(input("Enter the target number you need to search : "))
print("The lower bound is : ",search_insert(nums,len(nums),target))