Skip to content
Merged
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
26 changes: 26 additions & 0 deletions DSA/three_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Three Sum dynamic problem solution
def threeSum(self, nums: List[int]) -> List[List[int]]:
res = []
nums.sort()

for i, num1 in enumerate(nums):

if i > 0 and num1 == nums[i-1]:
continue

start, end = i+1, len(nums) - 1

while start < end:
sum = num1 + nums[start] + nums[end]

if sum > 0:
end -= 1
elif sum < 0:
start += 1
else:
res.append([num1, nums[start], nums[end]])
start += 1
while nums[start] == nums[start-1] and start < end:
start += 1

return res
14 changes: 14 additions & 0 deletions Search Algorithms/Exponential Search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h1>Exponential Search</h1>

<h4>
Exponential search is also known as doubling or galloping search. This mechanism is used to find the range where the search key may present. If L and U are the upper and lower bound of the list, then L and U both are the power of 2. For the last section, the U is the last position of the list. For that reason, it is known as exponential.
</h4>
<br>
<h3> Complexity of Exponential Search Technique </h3>
<hr>

<h4>
Time Complexity: O(1) for the best case. O(log2 i) for average or worst case. Where i is the location where search key is present.
<br>
Space Complexity: O(1)
</h4>
27 changes: 27 additions & 0 deletions Search Algorithms/Exponential Search/exponential_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Exponential Search
def binary_search(arr, low, high, x):
while low <= high:
mid = low + (high - low)/2;

if arr[mid] == x:
return mid
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1

return -1

def exponential_search(arr, low, high, x):
if arr[low] == x:
return low

i = (low + 1) * 2
while i <= high and arr[i] <= x:
i *= 2

return binary_search(arr, i / 2, min(i, high), x)

# arr = range(0,500)
# x = 42
# print(exponential_search(arr, 0, len(arr), x))