Skip to content

Commit 0f5d895

Browse files
committed
Day - 86 work
1 parent 57f1e87 commit 0f5d895

File tree

5 files changed

+124
-5
lines changed

5 files changed

+124
-5
lines changed

README.md

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

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 123 |
10-
| Current Streak | 85 days |
11-
| Longest Streak | 85 ( August 17, 2015 - November 09, 2015 ) |
9+
| Total Problems | 125 |
10+
| Current Streak | 86 days |
11+
| Longest Streak | 86 ( August 17, 2015 - November 10, 2015 ) |
1212

1313
</center>
1414

15-
### LinkedList Problems
15+
### LinkedList Problems
1616
| Problem | Solution |
1717
| :------------ | :----------: |
1818
| Find the nth node of linked list from last. | [nthToLastNode.cpp] (linked_list_problems/nthToLastNode.cpp) |
@@ -32,7 +32,7 @@
3232
| Sort a linked list using merge sort | [merge_sort.cpp] (linked_list_problems/merge_sort.cpp) |
3333
| Given a singly linked list L<sub>0</sub> -> L<sub>1</sub> -> … -> L<sub>n-1</sub> -> L<sub>n</sub>. Rearrange the nodes in the list (in place) so that the new formed list is : L<sub>0</sub> -> L<sub>n</sub> -> L<sub>1</sub> -> L<sub>n-1</sub> -> L<sub>2</sub> -> L<sub>n-2<sub> ....| [rearrange_list.cpp](linked_list_problems/rearrange_list.cpp)|
3434

35-
### Include
35+
### Include
3636
Include contains single header implementation of data structures and some algorithms.
3737

3838
| DS/ALG | Implementation |
@@ -159,6 +159,7 @@ Include contains single header implementation of data structures and some algori
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)|
160160
| 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)|
161161
| Numeros, the Artist, had two lists A and B, such that B was a permutation of A. Numeros was very proud of these lists. Unfortunately, while transporting them from one exhibition to another, some numbers were left out of A. Can you find the missing numbers? Notes: <ul><li>If a number occurs multiple times in the lists, you must ensure that the frequency of that number in both lists is the same. If that is not the case, then it is also a missing number.</li></ul><ul><li>You have to print all the missing numbers in ascending order.</li></ul><ul><li>Print each missing number once, even if it is missing multiple times.</li></ul><ul><li>The difference between maximum and minimum number in B is less than or equal to 100.</li></ul>. <ul><li> There will be four lines of input: n - the size of the first list, This is followed by n space-separated integers that make up the first list. m - the size of the second list. This is followed by m space-separated integers that make up the second list. Output the missing numbers in ascending order.| [missingNumbers.cpp](leet_code_problems/missingNumbers.cpp)|
162+
| Find the closest pair from two sorted arrays. Given two sorted arrays and a number x, find the pair whose sum is closest to x and the pair has an element from each array. We are given two arrays ar1[0…m-1] and ar2[0..n-1] and a number x, we need to find the pair ar1[i] + ar2[j] such that absolute value of (ar1[i] + ar2[j] – x) is minimum.| [closestPairSorted.cpp](sort_search_problems/closestPairSorted.cpp)|
162163
### Graph Problems
163164
| Problem | Solution |
164165
| :------------ | :----------: |
@@ -199,6 +200,8 @@ Include contains single header implementation of data structures and some algori
199200
| Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time.| [minPath.cpp](leet_code_problems/minPath.cpp)|
200201
| Count the number of prime numbers less than a non-negative number, n.| [countPrimes.cpp](leet_code_problems/countPrimes.cpp)|
201202
| Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example : for k = 3, n = 9 result would be [[1,2,6], [1,3,5], [2,3,4]], similarly for k = 3, n = 7, result would be [[1,2,4]]. | [combinationSum3.cpp](leet_code_problems/combinationSum3.cpp) |
203+
| Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up:
204+
Could you do it without any loop/recursion in O(1) runtime?| [sumDigits.cpp](leet_code_problems/sumDigits.cpp)|
202205

203206

204207

leet_code_problems/addDigits.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
3+
*
4+
* For example:
5+
*
6+
* Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
7+
*
8+
* Follow up:
9+
* Could you do it without any loop/recursion in O(1) runtime?
10+
*
11+
* solution approach:
12+
* Look at the pattern which emerges when we sum the values and eventually reduce it to single digit. By observing carefully, we will see a pattern.
13+
*
14+
* This is calculating digital root of a number (https://en.wikipedia.org/wiki/Digital_root)
15+
* The formula is:
16+
* dr(n) = 0 if n = 0,
17+
* 9 if n != 0 and (n % 9 == 0)
18+
* n % 9
19+
*
20+
*/
21+
22+
#include <iostream>
23+
24+
25+
int sumDigits( int num ) {
26+
if ( num == 0 ) {
27+
return 0;
28+
} else if ( num % 9 == 0 ) {
29+
return 9;
30+
}
31+
return num % 9;
32+
}
33+
34+
int main() {
35+
int n;
36+
std::cout << "Enter a number:";
37+
std::cin >> n;
38+
std::cout << "Digital root of " << n << " is :" << sumDigits(n) << std::endl;
39+
return 0;
40+
}
41+

leet_code_problems/run

-37.5 KB
Binary file not shown.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Find the closest pair from two sorted arrays
3+
* Given two sorted arrays and a number x, find the pair whose sum is closest to x and the pair has an element from each array.
4+
* We are given two arrays ar1[0…m-1] and ar2[0..n-1] and a number x, we need to find the pair ar1[i] + ar2[j] such that absolute value of (ar1[i] + ar2[j] – x) is minimum.
5+
* Input: ar1[] = {1, 4, 5, 7};
6+
* ar2[] = {10, 20, 30, 40};
7+
* x = 32
8+
* Output: 1 and 30
9+
*
10+
* Input: ar1[] = {1, 4, 5, 7};
11+
* ar2[] = {10, 20, 30, 40};
12+
* x = 50
13+
* Output: 7 and 40
14+
*/
15+
16+
#include <iostream>
17+
#include <vector>
18+
#include <utility>
19+
#include <limits>
20+
#include <cmath>
21+
22+
std::pair<int,int> closestPair( std::vector<int> & vec1, std::vector<int> & vec2, int x ) {
23+
size_t left = 0;
24+
size_t right = vec2.size() - 1;
25+
int minDiff = std::numeric_limits<int>::max();
26+
int leftRes, rightRes;
27+
while (left < vec1.size() && right >= 0 ) {
28+
int diff = std::abs(vec1[left] + vec2[right] - x);
29+
if ( diff < minDiff ) {
30+
minDiff = diff;
31+
leftRes = left;
32+
rightRes = right;
33+
}
34+
if ( vec1[left] + vec2[right] > x ) {
35+
--right;
36+
} else {
37+
++left;
38+
}
39+
}
40+
return std::make_pair(vec1[leftRes], vec2[rightRes]);
41+
}
42+
43+
void printVec( std::vector<int> & vec ) {
44+
for ( auto v : vec ) {
45+
std::cout << v << " ";
46+
}
47+
std::cout << std::endl;
48+
}
49+
50+
int main() {
51+
std::vector<int> vec1 {1, 4, 5, 7};
52+
std::vector<int> vec2 {10, 20, 30, 40};
53+
std::cout << "Vec1: ";
54+
printVec(vec1);
55+
std::cout << "Vec2: ";
56+
printVec(vec2);
57+
int x = 32;
58+
std::cout << "x : " << x << std::endl;
59+
std::pair<int,int> result = closestPair(vec1, vec2, x);
60+
std::cout << "The closest pair from the above two sorted vectors to " << x << " is "
61+
<< "(" << result.first << ", " << result.second << ")" << std::endl;
62+
63+
std::vector<int> vec3 {1, 4, 5, 7};
64+
std::vector<int> vec4 {10, 20, 30, 40};
65+
std::cout << "Vec3: ";
66+
printVec(vec3);
67+
std::cout << "Vec4: ";
68+
printVec(vec4);
69+
x = 50;
70+
std::cout << "x : " << x << std::endl;
71+
std::pair<int,int> result2 = closestPair(vec3, vec4, x);
72+
std::cout << "The closest pair from the above two sorted vectors to " << x << " is "
73+
<< "(" << result2.first << ", " << result2.second << ")" << std::endl;
74+
return 0;
75+
}

sort_search_problems/run

-53 KB
Binary file not shown.

0 commit comments

Comments
 (0)