Skip to content

Commit 74966b1

Browse files
committed
Day - 79 work
1 parent 054179e commit 74966b1

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 114 |
10-
| Current Streak | 78 days |
11-
| Longest Streak | 78 ( August 17, 2015 - November 02, 2015 ) |
9+
| Total Problems | 116 |
10+
| Current Streak | 79 days |
11+
| Longest Streak | 79 ( August 17, 2015 - November 03, 2015 ) |
1212

1313
</center>
1414

@@ -157,6 +157,7 @@ Include contains single header implementation of data structures and some algori
157157
| Given a list of unsorted integers, A={a<sub>1</sub>,a<sub>2</sub>,…,a<sub>N</sub>}, Find the pair of elements that have the smallest absolute difference between them? If there are multiple pairs, find them all.| [closest_numbers.cpp](sort_search_problems/closest_numbers.cpp)|
158158
| Given a sorted array, determine index of fixed point in this array. If array does not have a fixed point return -1. An array has a fixed point when index of the element is same as index i.e. i == arr[i], Expected time complexity O(logn)| [fixedPoint.cpp](sort_search_problems/fixedPoint.cpp)|
159159
| Find the maximum element in an array which is first increasing and then decreasing. Input: arr[] = {8, 10, 20, 80, 100, 200, 400, 500, 3, 2, 1}, output : 500. Array may be strictly increasing or decreasing as well. ExpectedTime complexity is O(logn).| [findMaximum.cpp](sort_search_problems/findMaximum.cpp)|
160+
| Given an array of positive and/or negative integers, find a pair in the array whose sum is closest to 0.| [findClosestPairToZero.cpp](sort_search_problems/findClosestPairToZero.cpp)|
160161

161162
### Graph Problems
162163
| Problem | Solution |
@@ -191,3 +192,4 @@ Include contains single header implementation of data structures and some algori
191192
|Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is *"((()))", "(()())", "(())()", "()(())", "()()()"*| [generate_parenthesis.cpp](leet_code_problems/generate_parenthesis.cpp)|
192193
|Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example, Given nums = [0, 1, 3] return 2.| [missing_number.cpp](leet_code_problems/missing_number.cpp)|
193194
|Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array.| [find_min_rotated.cpp](leet_code_problems/find_min_rotated.cpp)|
195+
|Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.| [threeSumClosest.cpp](leet_code_problems/threeSumClosest.cpp)|

leet_code_problems/run

30.1 KB
Binary file not shown.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
3+
* Return the sum of the three integers. You may assume that each input would have exactly one solution.
4+
* For example, given array S = {-1 2 1 -4}, and target = 1.
5+
* The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
6+
*/
7+
8+
9+
#include <iostream>
10+
#include <vector>
11+
#include <algorithm>
12+
13+
14+
int threeSumClosest(std::vector<int>& nums, int target) {
15+
int closestSum = (nums[0] + nums[1] + nums[2]);
16+
std::sort(nums.begin(), nums.end());
17+
for ( size_t i = 0 ;i < nums.size() - 2; ++i ) {
18+
size_t k = nums.size() - 1;
19+
size_t j = i + 1;
20+
while( j < k ) {
21+
int currSum = (nums[i] + nums[j] + nums[k]);
22+
if ( currSum == target ) {
23+
return currSum;
24+
}
25+
if ( abs(target - closestSum) > abs(target - currSum)) {
26+
closestSum = currSum;
27+
}
28+
if ( currSum > target ) {
29+
--k;
30+
} else {
31+
++j;
32+
}
33+
}
34+
}
35+
return closestSum;
36+
}
37+
38+
int main() {
39+
std::vector<int> vec{ -1,2,1,-4 };
40+
std::cout << "Vec:";
41+
for ( auto c : vec ) {
42+
std::cout << c << " ";
43+
}
44+
std::cout << std::endl;
45+
std::cout << "Three sum closest to 1 in above vector is :" << threeSumClosest(vec, 1) << std::endl;
46+
return 0;
47+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* An Array of integers is given, both +ve and -ve. You need to find the two elements such that their sum is closest to zero.
3+
* Example:
4+
* vec : { 1, 60, -10, 70, -80, 85 }
5+
*
6+
* output: -80, 85
7+
*
8+
* If no such pair exists return a { 0, 0 } pair
9+
*
10+
*/
11+
12+
#include <iostream>
13+
#include <vector>
14+
#include <algorithm>
15+
#include <utility>
16+
#include <limits>
17+
18+
std::pair<int, int> pairSumClosestToZero( std::vector<int> & vec ) {
19+
std::pair<int, int> minPair( 0, 0 );
20+
if ( vec.size() < 2 ) {
21+
return minPair;
22+
}
23+
24+
size_t minLeft = 0, minRight = vec.size()-1;
25+
size_t left = 0, right = vec.size() -1;
26+
int minSum = std::numeric_limits<int>::max();
27+
28+
std::sort( vec.begin(), vec.end() );
29+
30+
while( left < right ) {
31+
int sum = vec[left] + vec[right];
32+
if ( std::abs(minSum) > std::abs(sum) ) {
33+
minSum = sum;
34+
minLeft = left;
35+
minRight = right;
36+
}
37+
if ( sum > 0 ) {
38+
--right;
39+
} else {
40+
++left;
41+
}
42+
}
43+
minPair.first = vec[minLeft];
44+
minPair.second = vec[minRight];
45+
return minPair;
46+
}
47+
48+
49+
int main() {
50+
std::vector<int> vec{ 1, 60, -10, 70, -80, 85 };
51+
std::cout << "Vec: ";
52+
for ( auto v : vec ) {
53+
std::cout << v << " ";
54+
}
55+
std::pair<int, int> minPair = pairSumClosestToZero(vec);
56+
std::cout << "Pair whose sum is closes to 0 in above vector is: ("
57+
<< minPair.first << ", " << minPair.second << ")" << std::endl;
58+
return 0;
59+
}

sort_search_problems/run

30.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)