Skip to content

Commit 52916b8

Browse files
committed
Practise 16-Jun-2020
1 parent 78a7407 commit 52916b8

11 files changed

+579
-1
lines changed

array/k_closest_num_max_heap.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Given an unsorted array and two numbers x and k, find k closest values to x.
3+
Input : arr[] = {10, 2, 14, 4, 7, 6}, x = 5, k = 3
4+
*/
5+
6+
7+
8+
9+
10+
#include<bits/stdc++.h>
11+
using namespace std;
12+
13+
int main(){
14+
ios_base::sync_with_stdio(false);
15+
cin.tie(NULL);
16+
cout.tie(NULL);
17+
int t;
18+
cin>>t;
19+
while(t--){
20+
int n;
21+
cin>>n;
22+
23+
int arr[n];
24+
for(int i=0;i<n;i++) cin>>arr[i];
25+
26+
int k, x;
27+
cin>>k>>x;
28+
29+
priority_queue<pair<int, int> > maxh;
30+
31+
for(int i=0;i<n;i++){
32+
maxh.push({abs(x-arr[i]), arr[i]});
33+
if(maxh.size()>k) maxh.pop();
34+
}
35+
36+
vector<int> res;
37+
while(!maxh.empty()){
38+
res.push_back(maxh.top().second);
39+
maxh.pop();
40+
}
41+
42+
sort(res.begin(), res.end());
43+
44+
for(auto x: res) cout<<x<<" ";
45+
cout<<endl;
46+
}
47+
48+
return 0;
49+
}

array/k_largest_num_min_heap.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Given an array of N positive integers, print k largest elements from the array. The output elements should be printed in decreasing order.
3+
4+
Input:
5+
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N and k, N is the size of array and K is the largest elements to be returned. The second line of each test case contains N input C[i].
6+
7+
Output:
8+
Print the k largest element in descending order.
9+
10+
Constraints:
11+
1 ≤ T ≤ 100
12+
1 ≤ N ≤ 100
13+
K ≤ N
14+
1 ≤ C[i] ≤ 1000
15+
16+
Example:
17+
Input:
18+
2
19+
5 2
20+
12 5 787 1 23
21+
7 3
22+
1 23 12 9 30 2 50
23+
24+
Output:
25+
787 23
26+
50 30 23
27+
28+
Explanation:
29+
Testcase 1: 1st largest element in the array is 787 and second largest is 23.
30+
Testcase 2: 3 Largest element in the array are 50, 30 and 23.
31+
*/
32+
33+
34+
35+
#include<bits/stdc++.h>
36+
using namespace std;
37+
38+
int main(){
39+
ios_base::sync_with_stdio(false);
40+
cin.tie(NULL);
41+
cout.tie(NULL);
42+
int t;
43+
cin>>t;
44+
while(t--){
45+
int n,k;
46+
cin>>n>>k;
47+
int a[n];
48+
for(int i=0;i<n;i++) cin>>a[i];
49+
50+
// Implement min heap
51+
priority_queue<int, vector<int>, greater<int> > minh;
52+
for(int i=0;i<n;i++){
53+
minh.push(a[i]);
54+
if(minh.size()> k) minh.pop();
55+
}
56+
stack<int> s;
57+
while(!minh.empty()){
58+
s.push(minh.top());
59+
minh.pop();
60+
}
61+
// printing in descending order
62+
while(!s.empty()){
63+
cout<<s.top()<<" ";
64+
s.pop();
65+
}
66+
cout<<endl;
67+
}
68+
69+
return 0;
70+
}

array/kth_smallest_num_max_heap.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/8
2+
Given an array and a number k where k is smaller than size of array, we need to find the k’th smallest element in the given array. It is given that all array elements are distinct.
3+
4+
Example:
5+
Input: arr[] = {7, 10, 4, 3, 20, 15}
6+
k = 3
7+
Output: 7
8+
*/
9+
10+
11+
12+
#include<bits/stdc++.h>
13+
using namespace std;
14+
15+
int main(){
16+
ios_base::sync_with_stdio(false);
17+
cin.tie(NULL);
18+
cout.tie(NULL);
19+
int t;
20+
cin>>t;
21+
while(t--){
22+
int n,k;
23+
cin>>n;
24+
int a[n];
25+
for(int i=0;i<n;i++) cin>>a[i];
26+
cin>>k;
27+
// Implement max heap
28+
priority_queue<int> maxh;
29+
for(int i=0;i<n;i++){
30+
maxh.push(a[i]);
31+
if(maxh.size()>k) maxh.pop();
32+
}
33+
cout<<maxh.top()<<endl;
34+
}
35+
36+
return 0;
37+
}

array/sort_k_sorted_array.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Given an array of n elements, where each element is at most k away from its target position. The task is to print array in sorted form.
3+
4+
Input:
5+
First line consists of T test cases. First line of every test case consists of two integers N and K, denoting number of elements in array and at most k positions away from its target position respectively. Second line of every test case consists of elements of array.
6+
7+
Output:
8+
Single line output to print the sorted array.
9+
10+
Constraints:
11+
1<=T<=100
12+
1<=N<=100
13+
1<=K<=N
14+
15+
Example:
16+
Input:
17+
2
18+
3 3
19+
2 1 3
20+
6 3
21+
2 6 3 12 56 8
22+
Output:
23+
1 2 3
24+
2 3 6 8 12 56
25+
*/
26+
27+
28+
29+
30+
31+
32+
#include<bits/stdc++.h>
33+
using namespace std;
34+
35+
int main(){
36+
ios_base::sync_with_stdio(false);
37+
cin.tie(NULL);
38+
cout.tie(NULL);
39+
int t;
40+
cin>>t;
41+
while(t--){
42+
int n,k;
43+
cin>>n>>k;
44+
int a[n];
45+
for(int i=0;i<n;i++) cin>>a[i];
46+
priority_queue<int, vector<int>, greater<int> >minh;
47+
for(int i=0;i<n;i++){
48+
minh.push(a[i]);
49+
if(minh.size()>k){
50+
cout<<minh.top()<<" ";
51+
minh.pop();
52+
}
53+
}
54+
while(!minh.empty()){
55+
cout<<minh.top()<<" ";
56+
minh.pop();
57+
}
58+
cout<<endl;
59+
}
60+
61+
return 0;
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL.
2+
3+
For example,
4+
5+
Given the tree:
6+
4
7+
/ \
8+
2 7
9+
/ \
10+
1 3
11+
12+
And the value to search: 2
13+
You should return this subtree:
14+
15+
2
16+
/ \
17+
1 3
18+
In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL.
19+
20+
Note that an empty tree is represented by NULL, therefore you would see the expected output (serialized tree format) as [], not null.
21+
22+
23+
24+
25+
26+
27+
28+
29+
/**
30+
* Definition for a binary tree node.
31+
* struct TreeNode {
32+
* int val;
33+
* TreeNode *left;
34+
* TreeNode *right;
35+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
36+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
37+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
38+
* };
39+
*/
40+
41+
42+
class Solution {
43+
public:
44+
TreeNode* searchBST(TreeNode* root, int val) {
45+
if(!root) return root;
46+
47+
if(val==root->val) return root;
48+
else if(val<root->val) return searchBST(root->left, val);
49+
else return searchBST(root->right, val);
50+
}
51+
};
52+
53+
54+
55+
56+
57+
58+
/**
59+
Iterative
60+
* Definition for a binary tree node.
61+
* struct TreeNode {
62+
* int val;
63+
* TreeNode *left;
64+
* TreeNode *right;
65+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
66+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
67+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
68+
* };
69+
*/
70+
class Solution {
71+
public:
72+
TreeNode* searchBST(TreeNode* root, int val) {
73+
while(root && root->val!=val){
74+
root=val<root->val ? root->left: root->right;
75+
}
76+
return root;
77+
}
78+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Given a sorted array arr, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred.
2+
3+
4+
5+
Example 1:
6+
7+
Input: arr = [1,2,3,4,5], k = 4, x = 3
8+
Output: [1,2,3,4]
9+
Example 2:
10+
11+
Input: arr = [1,2,3,4,5], k = 4, x = -1
12+
Output: [1,2,3,4]
13+
14+
15+
Constraints:
16+
17+
1 <= k <= arr.length
18+
1 <= arr.length <= 10^4
19+
Absolute value of elements in the array and x will not exceed 104
20+
21+
22+
23+
24+
25+
26+
class Solution {
27+
public:
28+
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
29+
int n=arr.size();
30+
priority_queue<pair<int, int> > maxh;
31+
for(int i=0;i<n;i++){
32+
maxh.push({abs(x-arr[i]), arr[i]});
33+
if(maxh.size()>k) maxh.pop();
34+
}
35+
vector<int> res;
36+
while(!maxh.empty()){
37+
res.push_back(maxh.top().second);
38+
maxh.pop();
39+
}
40+
sort(res.begin(), res.end());
41+
return res;
42+
}
43+
};

0 commit comments

Comments
 (0)