diff --git a/DSA/three_sum.py b/DSA/three_sum.py new file mode 100644 index 0000000..2c26099 --- /dev/null +++ b/DSA/three_sum.py @@ -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 \ No newline at end of file diff --git a/Search Algorithms/Exponential Search/README.md b/Search Algorithms/Exponential Search/README.md new file mode 100644 index 0000000..70132b8 --- /dev/null +++ b/Search Algorithms/Exponential Search/README.md @@ -0,0 +1,14 @@ +

Exponential Search

+ +

+ 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. +

+
+

Complexity of Exponential Search Technique

+
+ +

+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. +
+Space Complexity: O(1) +

diff --git a/Search Algorithms/Exponential Search/exponential_search.py b/Search Algorithms/Exponential Search/exponential_search.py new file mode 100644 index 0000000..f7cc0d7 --- /dev/null +++ b/Search Algorithms/Exponential Search/exponential_search.py @@ -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)) \ No newline at end of file