Skip to content

Commit 264898f

Browse files
Two number, Three number and Smallest different question solutions (codedecks-in#59)
* Two number, Three number and Smallest different question solutions * Made the changes in readme, proper format followed
1 parent d7fe666 commit 264898f

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

Python/SmallestDifference.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""1200. Minimum Absolute Difference
2+
Easy
3+
4+
382
5+
6+
25
7+
8+
Add to List
9+
10+
Share
11+
Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.
12+
13+
Return a list of pairs in ascending order(with respect to pairs), each pair [a, b] follows
14+
15+
a, b are from arr
16+
a < b
17+
b - a equals to the minimum absolute difference of any two elements in arr
18+
19+
20+
Example 1:
21+
22+
Input: arr = [4,2,1,3]
23+
Output: [[1,2],[2,3],[3,4]]
24+
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
25+
Example 2:
26+
27+
Input: arr = [1,3,6,10,15]
28+
Output: [[1,3]]
29+
Example 3:
30+
31+
Input: arr = [3,8,-10,23,19,-4,-14,27]
32+
Output: [[-14,-10],[19,23],[23,27]]
33+
34+
35+
Constraints:
36+
37+
2 <= arr.length <= 10^5
38+
-10^6 <= arr[i] <= 10^6
39+
"""
40+
41+
def smallestDifference(arr1 : list, arr2 : list) :
42+
arr1.sort();arr2.sort()
43+
i = 0;j = 0
44+
45+
smallest = float("inf");current=float("inf")
46+
smallestPair = list()
47+
48+
while i < len(arr1) and j < len(arr2) :
49+
fnum = arr1[i];snum = arr2[j]
50+
51+
if fnum < snum :
52+
current = snum - fnum
53+
i += 1
54+
elif snum < fnum :
55+
current = snum - fnum
56+
j += 1
57+
else :
58+
return [fnum, snum]
59+
60+
if current < smallest :
61+
smallest = current
62+
smallestPair.append((fnum, snum))
63+
64+
65+
66+
if __name__ == '__main__' :
67+
print(smallestDifference([12,3,45,6], [2,4,3,5]))

Python/ThreeNumbersSum.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
15. 3Sum
3+
Medium
4+
5+
8121
6+
7+
881
8+
9+
Add to List
10+
11+
Share
12+
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
13+
14+
Notice that the solution set must not contain duplicate triplets.
15+
16+
17+
18+
Example 1:
19+
20+
Input: nums = [-1,0,1,2,-1,-4]
21+
Output: [[-1,-1,2],[-1,0,1]]
22+
Example 2:
23+
24+
Input: nums = []
25+
Output: []
26+
Example 3:
27+
28+
Input: nums = [0]
29+
Output: []
30+
"""
31+
32+
def sum_of_three(arr, target=0) :
33+
result = list()
34+
arr.sort()
35+
36+
for i in range(len(arr)-2) :
37+
left = i+1;right=len(arr)-1
38+
39+
while left < right :
40+
current_sum = arr[i] + arr[left] + arr[right]
41+
if current_sum == target :
42+
result.append((arr[i], arr[left], arr[right]))
43+
left+=1
44+
right-=1
45+
elif current_sum < target :
46+
left += 1
47+
elif current_sum > target :
48+
right -= 1
49+
else :
50+
print("Dont with the loop")
51+
return result
52+
53+
if __name__ == '__main__' :
54+
print(sum_of_three([3,5,-4,8,11,1,-1,6]))

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
101101
| 53 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [C++](./C++/maximum-subarray.cpp) | _O(N)_ | _O(1)_ |Easy | Array | |
102102
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [C++](./C++/two-sum.cpp) | _O(N^2)_ | _O(1)_ |Easy | Array | |
103103
| 495 | [Teemo Attacking](https://leetcode.com/problems/teemo-attacking) | [C++](./C++/teemo-attacking.cpp) | _O(n)_ | _O(1)_ | Medium| Array | |
104+
| 15 | [3 Sum](https://leetcode.com/problems/3sum/) | [Python](./Python/ThreeNumbersSum.py) | O( nLog(n) ) | O(1) | Medium | Array |
105+
| 1200 | [Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/) | [Python](./python/SmallestDifference.py) | O(n) | O(1) | Easy | Array |
106+
104107

105108
<br/>
106109
<div align="right">

0 commit comments

Comments
 (0)