Skip to content

Commit 9beeaec

Browse files
committed
day 2
1 parent 726d4e1 commit 9beeaec

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

Diff for: DSA Crack Sheet/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
- [Merge Two Balanced Binary Search Trees](https://www.geeksforgeeks.org/merge-two-balanced-binary-search-trees/ "view post")
214214
- [Kth largest element in BST](https://practice.geeksforgeeks.org/problems/kth-largest-element-in-bst/1# "view question") - [Cpp Solution](./solutions/Kth%20largest%20element%20in%20BST.cpp)
215215
- [k-th smallest element in BST](https://practice.geeksforgeeks.org/problems/find-k-th-smallest-element-in-bst/1 "view question") - [Cpp Solution](./solutions/k-th%20smallest%20element%20in%20BST.cpp)
216+
- [Brothers From Different Roots](https://practice.geeksforgeeks.org/problems/brothers-from-different-root/1# "view question") - [Cpp Solution](./solutions/Brothers%20From%20Different%20Roots.cpp)
216217
- []( "view question") - [Cpp Solution](./solutions/.cpp)
217218

218219
### Greedy Method
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Brothers From Different Roots
3+
=============================
4+
5+
Given two BSTs containing N1 and N2 distinct nodes respectively and given a value x. Your task is to complete the function countPairs(), that returns the count of all pairs from both the BSTs whose sum is equal to x.
6+
7+
Examples:
8+
Input : BST 1: 5
9+
/ \
10+
3 7
11+
/ \ / \
12+
2 4 6 8
13+
14+
BST 2: 10
15+
/ \
16+
6 15
17+
/ \ / \
18+
3 8 11 18
19+
x = 16
20+
21+
Output : 3
22+
The pairs are:
23+
(5, 11), (6, 10) and (8, 8)
24+
Input:
25+
The function takes three arguments as input, first the reference pointer to the root(root1) of the BST1, then reference pointer to the root(root2) of the BST2 and last the element X.
26+
There will be T test cases and for each test case the function will be called separately.
27+
28+
Output:
29+
For each test cases print the required number of pairs on new line.
30+
31+
Constraints:
32+
1<=T<=100
33+
1<=N<=103
34+
35+
Example:
36+
Input:
37+
2
38+
7
39+
5 3 7 2 4 6 8
40+
7
41+
10 6 15 3 8 11 18
42+
16
43+
6
44+
10 20 30 40 5 1
45+
5
46+
25 35 10 15 5
47+
30
48+
Output:
49+
3
50+
2
51+
*/
52+
53+
bool search(Node *root, int x)
54+
{
55+
if (!root)
56+
return false;
57+
if (root->data == x)
58+
return true;
59+
else if (root->data < x)
60+
return search(root->right, x);
61+
return search(root->left, x);
62+
}
63+
64+
void dfs(Node *root1, Node *root2, int x, int &ans)
65+
{
66+
if (!root1)
67+
return;
68+
int tar = root1->data;
69+
if (search(root2, x - tar))
70+
ans++;
71+
dfs(root1->left, root2, x, ans);
72+
dfs(root1->right, root2, x, ans);
73+
}
74+
75+
int countPairs(Node *root1, Node *root2, int x)
76+
{
77+
int ans = 0;
78+
dfs(root1, root2, x, ans);
79+
return ans;
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Find K Closest Elements
3+
=======================
4+
5+
Given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array. The result should also be sorted in ascending order.
6+
7+
An integer a is closer to x than an integer b if:
8+
9+
|a - x| < |b - x|, or
10+
|a - x| == |b - x| and a < b
11+
12+
Example 1:
13+
Input: arr = [1,2,3,4,5], k = 4, x = 3
14+
Output: [1,2,3,4]
15+
16+
Example 2:
17+
Input: arr = [1,2,3,4,5], k = 4, x = -1
18+
Output: [1,2,3,4]
19+
20+
Constraints:
21+
1 <= k <= arr.length
22+
1 <= arr.length <= 104
23+
arr is sorted in ascending order.
24+
-104 <= arr[i], x <= 104
25+
*/
26+
27+
class Solution
28+
{
29+
public:
30+
vector<int> findClosestElements(vector<int> &arr, int k, int x)
31+
{
32+
sort(arr.begin(), arr.end(), [x](int &a, int &b)
33+
{
34+
if (abs(a - x) == abs(b - x))
35+
return a < b;
36+
return abs(a - x) < abs(b - x);
37+
});
38+
39+
vector<int> ans = vector<int>(arr.begin(), arr.begin() + k);
40+
sort(ans.begin(), ans.end());
41+
return ans;
42+
}
43+
};

Diff for: Leetcode Daily Challenge/July-2021/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
| Day | Question Links | Solutions |
44
| :-: | :------------------------------------------------------------------------------------------------------------------------- | :----------------------------: |
55
| 1. | [Gray Code](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3799/) | [cpp](./01.%20Gray%20Code.cpp) |
6+
| 2. | [Find K Closest Elements](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3800/) | [cpp](./02.%20Find%20K%20Closest%20Elements.cpp) |

0 commit comments

Comments
 (0)