Skip to content

Commit e41b977

Browse files
committed
arrays
1 parent 96b9a1a commit e41b977

File tree

5 files changed

+296
-4
lines changed

5 files changed

+296
-4
lines changed

hackerrank/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,10 @@ add_executable(tree_traversal src/tree_traversal.cpp)
104104

105105
add_executable(tree_insert src/tree_insert.cpp)
106106

107-
add_executable(dynamic_seq src/dynamic_seq.cpp)
107+
add_executable(dynamic_seq src/dynamic_seq.cpp)
108+
109+
add_executable(bribe_array src/bribe_array.cpp)
110+
111+
add_executable(minimum_swaps src/minimum_swaps.cpp)
112+
113+
add_executable(LCA src/LCA.cpp)

hackerrank/src/LCA.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Node {
6+
public:
7+
int data;
8+
Node *left;
9+
Node *right;
10+
Node(int d) {
11+
data = d;
12+
left = NULL;
13+
right = NULL;
14+
}
15+
};
16+
17+
class Solution {
18+
public:
19+
Node* insert(Node* root, int data) {
20+
if(root == NULL) {
21+
return new Node(data);
22+
} else {
23+
Node* cur;
24+
if(data <= root->data) {
25+
cur = insert(root->left, data);
26+
root->left = cur;
27+
} else {
28+
cur = insert(root->right, data);
29+
root->right = cur;
30+
}
31+
32+
return root;
33+
}
34+
}
35+
36+
/*The tree node has data, left child and right child
37+
class Node {
38+
int data;
39+
Node* left;
40+
Node* right;
41+
};
42+
43+
*/
44+
Node *lca(Node *root, int v1,int v2) {
45+
// Write your code here.
46+
while(root != NULL)
47+
{
48+
if((root->data < v1) && (root->data < v2))
49+
{
50+
root = root->right;
51+
}
52+
else if((root->data > v1) && (root->data > v2))
53+
{
54+
root = root->left;
55+
}
56+
else
57+
{
58+
break;
59+
}
60+
61+
}
62+
return root;
63+
}
64+
65+
}; //End of Solution
66+
67+
int main() {
68+
69+
Solution myTree;
70+
Node* root = NULL;
71+
72+
int t;
73+
int data;
74+
75+
std::cin >> t;
76+
77+
while(t-- > 0) {
78+
std::cin >> data;
79+
root = myTree.insert(root, data);
80+
}
81+
82+
int v1, v2;
83+
std::cin >> v1 >> v2;
84+
85+
Node *ans = myTree.lca(root, v1, v2);
86+
87+
std::cout << ans->data;
88+
89+
return 0;
90+
}

hackerrank/src/bribe_array.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
vector<string> split_string(string);
6+
7+
// Complete the minimumBribes function below.
8+
void minimumBribes(vector<int> q) {
9+
vector<int> expect = {1,2,3};
10+
int count = 0;
11+
for(int i=0; i<q.size(); ++i)
12+
{
13+
if(q[i] == expect[0])
14+
{
15+
expect[0] = expect[1];
16+
expect[1] = expect[2];
17+
expect[2]++;
18+
}
19+
else if(q[i] == expect[1])
20+
{
21+
expect[1] = expect[2];
22+
expect[2]++;
23+
count++;
24+
}
25+
else if(q[i] == expect[2])
26+
{
27+
expect[2]++;
28+
count += 2;
29+
}
30+
else
31+
{
32+
cout<<"Too chaotic\n";
33+
return;
34+
}
35+
}
36+
cout<<count<<endl;
37+
}
38+
39+
int main()
40+
{
41+
int t;
42+
cin >> t;
43+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
44+
45+
for (int t_itr = 0; t_itr < t; t_itr++) {
46+
int n;
47+
cin >> n;
48+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
49+
50+
string q_temp_temp;
51+
getline(cin, q_temp_temp);
52+
53+
vector<string> q_temp = split_string(q_temp_temp);
54+
55+
vector<int> q(n);
56+
57+
for (int i = 0; i < n; i++) {
58+
int q_item = stoi(q_temp[i]);
59+
60+
q[i] = q_item;
61+
}
62+
63+
minimumBribes(q);
64+
}
65+
66+
return 0;
67+
}
68+
69+
vector<string> split_string(string input_string) {
70+
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
71+
return x == y and x == ' ';
72+
});
73+
74+
input_string.erase(new_end, input_string.end());
75+
76+
while (input_string[input_string.length() - 1] == ' ') {
77+
input_string.pop_back();
78+
}
79+
80+
vector<string> splits;
81+
char delimiter = ' ';
82+
83+
size_t i = 0;
84+
size_t pos = input_string.find(delimiter);
85+
86+
while (pos != string::npos) {
87+
splits.push_back(input_string.substr(i, pos - i));
88+
89+
i = pos + 1;
90+
pos = input_string.find(delimiter, i);
91+
}
92+
93+
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
94+
95+
return splits;
96+
}

hackerrank/src/minimum_swaps.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
vector<string> split_string(string);
6+
7+
// Complete the minimumSwaps function below.
8+
int minimumSwaps(vector<int> arr) {
9+
int swap = 0;
10+
int i = 0;
11+
const int t = arr.size();
12+
while(true)
13+
{
14+
if(arr[i] != i+1)
15+
{
16+
// swap arr[i]
17+
int swapIdx = arr[i]-1;
18+
int dataTemp = arr[swapIdx];
19+
arr[swapIdx] = arr[i];
20+
arr[i] = dataTemp;
21+
swap++;
22+
// Since we swapped
23+
// Reset i
24+
i = 0;
25+
}
26+
else
27+
{
28+
if(i<arr.size()-1)
29+
{
30+
++i;
31+
}
32+
else
33+
{
34+
break;
35+
}
36+
37+
}
38+
}
39+
40+
return swap;
41+
}
42+
43+
int main()
44+
{
45+
ofstream fout(getenv("OUTPUT_PATH"));
46+
47+
int n;
48+
cin >> n;
49+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
50+
51+
string arr_temp_temp;
52+
getline(cin, arr_temp_temp);
53+
54+
vector<string> arr_temp = split_string(arr_temp_temp);
55+
56+
vector<int> arr(n);
57+
58+
for (int i = 0; i < n; i++) {
59+
int arr_item = stoi(arr_temp[i]);
60+
61+
arr[i] = arr_item;
62+
}
63+
64+
int res = minimumSwaps(arr);
65+
66+
fout << res << "\n";
67+
68+
fout.close();
69+
70+
return 0;
71+
}
72+
73+
vector<string> split_string(string input_string) {
74+
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
75+
return x == y and x == ' ';
76+
});
77+
78+
input_string.erase(new_end, input_string.end());
79+
80+
while (input_string[input_string.length() - 1] == ' ') {
81+
input_string.pop_back();
82+
}
83+
84+
vector<string> splits;
85+
char delimiter = ' ';
86+
87+
size_t i = 0;
88+
size_t pos = input_string.find(delimiter);
89+
90+
while (pos != string::npos) {
91+
splits.push_back(input_string.substr(i, pos - i));
92+
93+
i = pos + 1;
94+
pos = input_string.find(delimiter, i);
95+
}
96+
97+
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
98+
99+
return splits;
100+
}

hackerrank/src/tree_traversal.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ int main() {
150150
}
151151

152152
//myTree.postOrder(root);
153-
//myTree.inOrder(root);
153+
myTree.inOrder(root);
154154
//myTree.preOrder(root);
155-
//myTree.inOrder(root);
156-
myTree.topView(root);
155+
//myTree.levelOrder(root);
156+
//myTree.topView(root);
157157
return 0;
158158
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy