├── uniquebst.cpp ├── stringtreelevelsum.cpp ├── urinal.cpp ├── mrKim.cpp ├── calendar.cpp ├── maxpathsum.cpp ├── bipartite.cpp ├── cows.cpp ├── cycledirected.cpp ├── wormhole.cpp ├── sumTree.cpp ├── frogjump.cpp ├── cycleundirected.cpp ├── spaceshipBomb.cpp ├── oilmines.cpp ├── chessboard.cpp ├── mrLee.cpp ├── maze.cpp ├── crowpots.cpp ├── bstinbinary.cpp ├── oldmobile.cpp ├── rareElements.cpp ├── endoscope.cpp ├── distancetoleaf.cpp └── readme.md /uniquebst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int n) 5 | { 6 | int ans = 1, j = 2 * n, k = 1; 7 | for (int i = 0; i < n; i++) 8 | { 9 | ans *= (j - i); 10 | ans /= (k + i); 11 | // cout << ans << endl; 12 | } 13 | return ans / (n + 1); 14 | } 15 | 16 | int main() 17 | { 18 | int t, n, a; 19 | cin >> t; 20 | while (t--) 21 | { 22 | cin >> n; 23 | for (int i = 0; i < n; i++) 24 | { 25 | cin >> a; 26 | } 27 | cout << "#" << t + 1 << " " << solve(n) << endl; 28 | } 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /stringtreelevelsum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(string s, int k) 5 | { 6 | int level = -1, l = s.length(), sum = 0; 7 | for (int i = 0; i < l; i++) 8 | { 9 | // cout << s[i]; 10 | if (s[i] == '(') 11 | { 12 | level++; 13 | } 14 | else if (s[i] == ')') 15 | { 16 | level--; 17 | } 18 | else if (level == k) 19 | { 20 | sum += (s[i] - '0'); 21 | // cout << sum << " " << s[i] << endl; 22 | } 23 | } 24 | return sum; 25 | } 26 | 27 | int main() 28 | { 29 | int t, k; 30 | string s; 31 | cin >> t; 32 | while (t--) 33 | { 34 | cin >> s; 35 | cin >> k; 36 | cout << "#" << t + 1 << " " << solve(s, k) << endl; 37 | } 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /urinal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void solve(int n, int k, int arr[]) 5 | { 6 | if (k == 0) 7 | { 8 | return; 9 | } 10 | 11 | int len = 0, ans = 0, start, last, mid; 12 | for (int i = 0; i < n; i++) 13 | { 14 | if (arr[i] == 0) 15 | { 16 | len++; 17 | } 18 | if (arr[i] == 1) 19 | { 20 | len = 0; 21 | } 22 | if (len >= ans) 23 | { 24 | ans = len; 25 | last = i; 26 | } 27 | } 28 | start = last - ans + 1; 29 | mid = (start + last) / 2; 30 | // cout << start << " " << mid << " " << last << endl; 31 | arr[mid] = 1; 32 | return solve(n, k - 1, arr); 33 | } 34 | 35 | int main() 36 | { 37 | int t, n, k; 38 | cin >> t; 39 | for (int i = 0; i < t; i++) 40 | { 41 | cin >> n >> k; 42 | int arr[n] = {0}; 43 | solve(n, k, arr); 44 | for (int j = 0; j < n; j++) 45 | { 46 | cout << arr[j] << " "; 47 | } 48 | cout << endl; 49 | } 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /mrKim.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, ox, oy, hx, hy; 5 | 6 | struct point 7 | { 8 | int x; 9 | int y; 10 | }; 11 | 12 | point cust[10]; 13 | bool vis[10] = {false}; 14 | 15 | int solve(int x, int y, int count) 16 | { 17 | if (count == n) 18 | { 19 | return abs(x - hx) + abs(y - hy); 20 | } 21 | 22 | int ans = INT_MAX, val; 23 | 24 | for (int i = 0; i < n; i++) 25 | { 26 | if (!vis[i]) 27 | { 28 | vis[i] = true; 29 | val = abs(x - cust[i].x) + abs(y - cust[i].y); 30 | ans = min(ans, val + solve(cust[i].x, cust[i].y, count + 1)); 31 | vis[i] = false; 32 | } 33 | } 34 | return ans; 35 | } 36 | 37 | int main() 38 | { 39 | int t; 40 | cin >> t; 41 | while (t--) 42 | { 43 | cin >> n; 44 | cin >> ox >> oy >> hx >> hy; 45 | for (int i = 0; i < n; i++) 46 | { 47 | cin >> cust[i].x >> cust[i].y; 48 | } 49 | cout << "#" << t + 1 << " " << solve(ox, oy, 0); 50 | } 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /calendar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int monthnl[12] = {5, 1, 1, 4, 6, 2, 4, 0, 3, 5, 1, 3}; 5 | int monthl[12] = {4, 0, 1, 4, 6, 2, 4, 0, 3, 5, 1, 3}; 6 | 7 | int solve(int year, int month, int date) 8 | { 9 | int century = year / 100; 10 | int yearc = year % 100; 11 | 12 | if (century % 4 == 0) 13 | { 14 | century = 1; 15 | } 16 | else if (century % 4 == 1) 17 | { 18 | century = 6; 19 | } 20 | else if (century % 4 == 2) 21 | { 22 | century = 4; 23 | } 24 | else if (century % 4 == 3) 25 | { 26 | century = 2; 27 | } 28 | 29 | if (yearc % 4 == 0 && yearc % 400 != 0) 30 | { 31 | month = monthl[month - 1]; 32 | } 33 | else 34 | { 35 | month = monthnl[month - 1]; 36 | } 37 | 38 | yearc = (yearc + yearc / 4) % 7; 39 | 40 | int ans = century + yearc + month + date; 41 | return ans % 7; 42 | } 43 | 44 | int main() 45 | { 46 | int year, month, date; 47 | cin >> date >> month >> year; 48 | cout << solve(year, month, date); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /maxpathsum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, m; 5 | int mat[100][100]; 6 | int solve() 7 | { 8 | cout << n << " " << m << " " << mat[0][0] << endl; 9 | for (int i = 1; i < n; i++) 10 | { 11 | mat[i][0] += mat[i - 1][0]; 12 | cout << i << " " 13 | << "0" 14 | << " " << mat[i][0] << endl; 15 | } 16 | for (int i = 1; i < m; i++) 17 | { 18 | mat[0][i] += mat[0][i - 1]; 19 | cout << 0 << " " << i << " " << mat[0][i] << endl; 20 | } 21 | for (int i = 1; i < n; i++) 22 | { 23 | for (int j = 1; j < m; j++) 24 | { 25 | mat[i][j] += max(mat[i - 1][j], mat[i][j - 1]); 26 | cout << i << " " << j << " " << mat[i][j] << endl; 27 | } 28 | } 29 | return mat[n - 1][m - 1]; 30 | } 31 | 32 | int main() 33 | { 34 | int t; 35 | cin >> t; 36 | while (t--) 37 | { 38 | cin >> n >> m; 39 | // int mat[n][m]; 40 | for (int i = 0; i < n; i++) 41 | { 42 | for (int j = 0; j < m; j++) 43 | { 44 | cin >> mat[i][j]; 45 | } 46 | } 47 | cout << "#" << t + 1 << " " << solve() << endl; 48 | } 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /bipartite.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | int map[100][100] = {0}; 6 | 7 | int isBipartite(int node, int color[]) 8 | { 9 | int flag = 1; 10 | for (int j = 0; j < n; j++) 11 | { 12 | if (map[node][j] == 1) 13 | { 14 | if (color[j] == -1) 15 | { 16 | color[j] = 1 - color[node]; 17 | flag = flag & isBipartite(j, color); 18 | } 19 | else if (color[j] == color[node]) 20 | { 21 | return 0; 22 | } 23 | } 24 | } 25 | return flag; 26 | } 27 | 28 | int main() 29 | { 30 | cin >> n; 31 | int color[n]; 32 | 33 | for (int i = 0; i < n; i++) 34 | { 35 | color[i] = -1; 36 | for (int j = 0; j < n; j++) 37 | { 38 | cin >> map[i][j]; 39 | } 40 | } 41 | 42 | for (int i = 0; i < n; i++) 43 | { 44 | if (color[i] == -1) 45 | { 46 | color[i] = 0; 47 | if (!isBipartite(i, color)) 48 | { 49 | cout << "-1"; 50 | return 0; 51 | } 52 | } 53 | } 54 | 55 | for (int i = 0; i < n; i++) 56 | { 57 | cout << color[i] << " "; 58 | if (color[i] == 1) 59 | { 60 | cout << i << " "; 61 | } 62 | cout << endl; 63 | } 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /cows.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int c, n; 5 | 6 | int check(int arr[], int diff) 7 | { 8 | cout << "diff = " << diff << endl; 9 | int count = 1, pos = arr[0]; //count = 1 imp 10 | cout << pos << " p "; 11 | for (int i = 1; i < n; i++) 12 | { 13 | if (arr[i] - pos >= diff) 14 | { 15 | pos = arr[i]; 16 | count++; 17 | cout << arr[i] << " p "; 18 | if (count == c) 19 | { 20 | cout << endl; 21 | return 1; 22 | } 23 | } 24 | } 25 | cout << endl; 26 | return 0; 27 | } 28 | 29 | int solve(int arr[]) 30 | { 31 | int start = 0, end = arr[n - 1], mid, max = -1; 32 | while (start < end) 33 | { 34 | mid = (start + end) / 2; 35 | if (check(arr, mid) == 1) 36 | { 37 | if (max < mid) 38 | { 39 | max = mid; 40 | cout << max << " " << start << " " << end << endl; 41 | } 42 | start = mid + 1; 43 | } 44 | else 45 | { 46 | end = mid; 47 | } 48 | } 49 | return max; 50 | } 51 | 52 | int main() 53 | { 54 | int t; 55 | cin >> t; 56 | while (t--) 57 | { 58 | cin >> n >> c; 59 | int arr[n]; 60 | for (int i = 0; i < n; i++) 61 | { 62 | cin >> arr[i]; 63 | } 64 | cout << solve(arr); 65 | } 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /cycledirected.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | int graph[100][100] = {0}; 6 | bool vis[100] = {false}; 7 | bool recstack[100] = {false}; 8 | 9 | void addedge(int u, int v) 10 | { 11 | graph[u][v] = 1; 12 | } 13 | 14 | bool iscyclic(int u, bool vis[], bool recstack[]) 15 | { 16 | if (vis[u] == false) 17 | { 18 | vis[u] = true; 19 | recstack[u] = true; 20 | for (int v = 0; v < n; v++) 21 | { 22 | if (graph[u][v] == 1) 23 | { 24 | if (!vis[v] && iscyclic(v, vis, recstack)) 25 | { 26 | return true; 27 | } 28 | else if (recstack[v]) 29 | { 30 | return true; 31 | } 32 | } 33 | } 34 | } 35 | recstack[u] = false; 36 | return false; 37 | } 38 | 39 | int main() 40 | { 41 | int t, e, u, v; 42 | cin >> t; 43 | while (t--) 44 | { 45 | cin >> n >> e; 46 | for (int i = 0; i < e; i++) 47 | { 48 | cin >> u >> v; 49 | addedge(u, v); 50 | } 51 | bool flag = false; 52 | for (int i = 0; i < n; i++) 53 | { 54 | if (iscyclic(i, vis, recstack)) 55 | { 56 | flag = true; 57 | cout << i << " cyclic" << endl; 58 | } 59 | } 60 | if (flag == false) 61 | { 62 | cout << "acyclic"; 63 | } 64 | } 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /wormhole.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, ans; 5 | struct point 6 | { 7 | int x; 8 | int y; 9 | }; 10 | 11 | struct wormhole 12 | { 13 | point p1; 14 | point p2; 15 | int cost; 16 | }; 17 | 18 | int vis[100] = {false}; 19 | 20 | int dist(point src, point dest) 21 | { 22 | return (abs(src.x - dest.x) + abs(src.y - dest.y)); 23 | } 24 | 25 | void solve(point src, point dest, int cost, wormhole worm[]) 26 | { 27 | int tempans = cost + dist(src, dest); 28 | ans = min(ans, tempans); 29 | 30 | for (int i = 0; i < n; i++) 31 | { 32 | if (!vis[i]) 33 | { 34 | vis[i] = true; 35 | 36 | int val = dist(src, worm[i].p1); 37 | solve(worm[i].p2, dest, cost + val + worm[i].cost, worm); 38 | 39 | val = dist(src, worm[i].p2); 40 | solve(worm[i].p1, dest, cost + val + worm[i].cost, worm); 41 | 42 | vis[i] = false; 43 | } 44 | } 45 | } 46 | 47 | int main() 48 | { 49 | int t; 50 | cin >> t; 51 | while (t--) 52 | { 53 | point src, dest; 54 | cin >> src.x >> src.y >> dest.x >> dest.y; 55 | cin >> n; 56 | wormhole worm[n]; 57 | for (int i = 0; i < n; i++) 58 | { 59 | cin >> worm[i].p1.x >> worm[i].p1.y >> worm[i].p2.x >> worm[i].p2.y >> worm[i].cost; 60 | } 61 | ans = 10000; 62 | solve(src, dest, 0, worm); 63 | cout << ans << endl; 64 | } 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /sumTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node 5 | { 6 | int data; 7 | node *left; 8 | node *right; 9 | }; 10 | 11 | node *newnode(int x) 12 | { 13 | node *newnode = new node; 14 | newnode->data = x; 15 | newnode->left = NULL; 16 | newnode->right = NULL; 17 | return newnode; 18 | } 19 | 20 | node *insert(node *head, int x) 21 | { 22 | if (head == NULL) 23 | { 24 | return newnode(x); 25 | } 26 | else if (x < head->data) 27 | { 28 | head->left = insert(head->left, x); 29 | } 30 | else 31 | { 32 | head->right = insert(head->right, x); 33 | } 34 | return head; 35 | } 36 | 37 | int sumtree(node *head) 38 | { 39 | if (head == NULL) 40 | { 41 | return 0; 42 | } 43 | 44 | int oldval = head->data; 45 | head->data = sumtree(head->left) + sumtree(head->right); 46 | return head->data + oldval; 47 | } 48 | 49 | void inorder(node *head) 50 | { 51 | if (head == NULL) 52 | { 53 | return; 54 | } 55 | inorder(head->left); 56 | cout << head->data << " "; 57 | inorder(head->right); 58 | } 59 | 60 | int main() 61 | { 62 | int t, n, x; 63 | cin >> t; 64 | while (t--) 65 | { 66 | cin >> n; 67 | node *head = NULL; 68 | for (int i = 0; i < n; i++) 69 | { 70 | cin >> x; 71 | head = insert(head, x); 72 | } 73 | inorder(head); 74 | cout << endl; 75 | sumtree(head); 76 | inorder(head); 77 | cout << endl; 78 | } 79 | 80 | return 0; 81 | } -------------------------------------------------------------------------------- /frogjump.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, m, ans; 5 | int map[100][100]; 6 | int dp[100][100]; 7 | int vis[100][100]; 8 | 9 | int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; 10 | 11 | bool valid(int x, int y) 12 | { 13 | return (x >= 0 && x < n && y >= 0 && y < m && map[x][y] && !vis[x][y]); 14 | } 15 | 16 | void solve(int sx, int sy, int dx, int dy, int ans) 17 | { 18 | cout << sx << " " << sy << " " << ans << endl; 19 | vis[sx][sy] = 1; 20 | if (dp[sx][sy] > ans) 21 | { 22 | dp[sx][sy] = ans; 23 | } 24 | if (sx == dx && sy == dy) 25 | { 26 | return; 27 | } 28 | 29 | for (int i = 0; i < 4; i++) 30 | { 31 | int x = sx + dir[i][0]; 32 | int y = sy + dir[i][1]; 33 | int temp; 34 | if (valid(x, y)) 35 | { 36 | if (x == sx) 37 | { 38 | temp = 0; 39 | } 40 | if (y == sy) 41 | { 42 | temp = 1; 43 | } 44 | solve(x, y, dx, dy, ans + temp); 45 | } 46 | } 47 | 48 | return; 49 | } 50 | 51 | int main() 52 | { 53 | int t, sx, sy, dx, dy; 54 | cin >> t; 55 | while (t--) 56 | { 57 | cin >> n >> m; 58 | for (int i = 0; i < n; i++) 59 | { 60 | for (int j = 0; j < m; j++) 61 | { 62 | cin >> map[i][j]; 63 | dp[i][j] = 10000; 64 | vis[i][j] = 0; 65 | } 66 | } 67 | cin >> sx >> sy >> dx >> dy; 68 | solve(sx, sy, dx, dy, 0); 69 | cout << "#" << t + 1 << " " << dp[dx][dy]; 70 | } 71 | 72 | return 0; 73 | } -------------------------------------------------------------------------------- /cycleundirected.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | int graph[100][100] = {0}; 6 | bool vis[100] = {false}; 7 | 8 | void addedge(int u, int v) 9 | { 10 | graph[u][v] = 1; 11 | graph[v][u] = 1; 12 | return; 13 | } 14 | 15 | bool iscyclic(int u, int parent) 16 | { 17 | if (!vis[u]) 18 | { 19 | // cout << u << " p " << parent << endl; 20 | vis[u] = true; 21 | for (int v = 0; v < n; v++) 22 | { 23 | // cout << u << " o " << v << endl; 24 | if (graph[u][v] == 1) 25 | { 26 | // cout << u << " c " << v << endl; 27 | if (!vis[v] && iscyclic(v, u)) 28 | { 29 | return true; 30 | } 31 | else if (v != parent) 32 | { 33 | return true; 34 | } 35 | } 36 | } 37 | } 38 | return false; 39 | } 40 | 41 | int main() 42 | { 43 | int t, e, u, v; 44 | cin >> t; 45 | while (t--) 46 | { 47 | cin >> n >> e; 48 | for (int i = 0; i < n; i++) 49 | { 50 | vis[i] = false; 51 | for (int j = 0; j < n; j++) 52 | { 53 | graph[i][j] = 0; 54 | } 55 | } 56 | 57 | for (int i = 0; i < e; i++) 58 | { 59 | cin >> u >> v; 60 | addedge(u, v); 61 | } 62 | 63 | bool flag = false; 64 | for (int i = 0; i < n; i++) 65 | { 66 | if (iscyclic(i, -1)) 67 | { 68 | flag = true; 69 | cout << i << " cyclic" << endl; 70 | return 0; 71 | } 72 | } 73 | if (flag == false) 74 | { 75 | cout << "acyclic"; 76 | } 77 | } 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /spaceshipBomb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int map[100][5] = {0}; 5 | int dp[100][5] = {-1}; 6 | int n, ans = 0; 7 | 8 | bool isValid(int r, int c) 9 | { 10 | return (r < n && r >= 0 && c < 5 && c >= 0); 11 | } 12 | 13 | int solve(int r, int c, int bomb, int effect) 14 | { 15 | if (r < 0) 16 | { 17 | return 0; 18 | } 19 | 20 | int ans = 0, temp = 0, x, y; 21 | 22 | for (int i = -1; i <= 1; i++) 23 | { 24 | x = r - 1; 25 | y = c + i; 26 | if (isValid(x, y)) 27 | { 28 | // cout << map[x][y] << " " << x << " " << y << " " << i << endl; 29 | if (dp[x][y] >= 0) 30 | { 31 | temp = dp[x][y]; 32 | } 33 | else if (map[x][y] == 2) 34 | { 35 | if (bomb == 0 && effect > 0) 36 | { 37 | temp = solve(x, y, 0, effect - 1); 38 | } 39 | else if (bomb == 1) 40 | { 41 | temp = solve(x, y, 0, 5); 42 | } 43 | } 44 | else 45 | { 46 | if (bomb == 0) 47 | { 48 | temp = solve(x, y, 0, effect - 1); 49 | } 50 | else 51 | { 52 | temp = solve(x, y, bomb, 5); 53 | } 54 | } 55 | ans = max(ans, temp); 56 | } 57 | } 58 | if (map[r][c] == 1) 59 | { 60 | ans++; 61 | } 62 | dp[r][c] = ans; 63 | cout << r << " " << c << " " << dp[r][c] << endl; 64 | return ans; 65 | } 66 | 67 | int main() 68 | { 69 | cin >> n; 70 | for (int i = 0; i < n; i++) 71 | { 72 | for (int j = 0; j < 5; j++) 73 | { 74 | cin >> map[i][j]; 75 | dp[i][j] = -1; 76 | } 77 | } 78 | 79 | solve(n, 2, 1, 5); 80 | 81 | cout << dp[n][2]; 82 | return 0; 83 | } -------------------------------------------------------------------------------- /oilmines.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, m, ans = 100000; 5 | 6 | void finddiff(int comp[]) 7 | { 8 | int maxx = -1, minn = 100000; 9 | for (int i = 0; i < n; i++) 10 | { 11 | cout << comp[i] << " "; 12 | if (comp[i] > maxx) 13 | { 14 | maxx = comp[i]; 15 | } 16 | if (comp[i] < minn) 17 | { 18 | minn = comp[i]; 19 | } 20 | } 21 | cout << endl; 22 | cout << maxx << " " << minn << " "; 23 | ans = min(ans, maxx - minn); 24 | } 25 | 26 | void solve(int curr, int end, int comp[], int oilmines[], int coma, int remo) 27 | { 28 | if (remo < n - coma) 29 | { 30 | return; 31 | } 32 | if ((curr + 1) % m == end) 33 | { 34 | if (coma == n - 1) 35 | { 36 | comp[coma] += oilmines[curr]; 37 | finddiff(comp); 38 | cout << ans << " t" << endl; 39 | comp[coma] -= oilmines[curr]; 40 | } 41 | return; 42 | } 43 | if (coma >= n) 44 | { 45 | return; 46 | } 47 | 48 | comp[coma] += oilmines[curr]; 49 | 50 | solve((curr + 1) % m, end, comp, oilmines, coma, remo - 1); 51 | solve((curr + 1) % m, end, comp, oilmines, coma + 1, remo - 1); 52 | 53 | comp[coma] -= oilmines[curr]; 54 | if (comp[coma] > 0) 55 | { 56 | solve(curr, end, comp, oilmines, coma + 1, remo); 57 | } 58 | } 59 | 60 | int main() 61 | { 62 | int t; 63 | cin >> t; 64 | while (t--) 65 | { 66 | cin >> n >> m; 67 | int oilmines[m], comp[n] = {0}; 68 | for (int i = 0; i < m; i++) 69 | { 70 | cin >> oilmines[i]; 71 | } 72 | if (m < n) 73 | { 74 | cout << "-1" << endl; 75 | continue; 76 | } 77 | 78 | for (int i = 0; i < m; i++) 79 | { 80 | solve(i, i, comp, oilmines, 0, m); 81 | } 82 | cout << ans << endl; 83 | } 84 | 85 | return 0; 86 | } -------------------------------------------------------------------------------- /chessboard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, m, r, c, s, k; 5 | bool vis[100][100] = {false}; 6 | 7 | struct node 8 | { 9 | int x; 10 | int y; 11 | int l; 12 | }; 13 | 14 | node q[1000]; 15 | int front, back; 16 | 17 | void init() 18 | { 19 | front = 0; 20 | back = 0; 21 | return; 22 | } 23 | 24 | bool isempty() 25 | { 26 | return front == back; 27 | } 28 | 29 | node pop() 30 | { 31 | return q[front++]; 32 | } 33 | 34 | void push(int x, int y, int l) 35 | { 36 | q[back].x = x; 37 | q[back].y = y; 38 | q[back].l = l; 39 | back++; 40 | return; 41 | } 42 | 43 | int dir[8][2] = {{-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}}; 44 | 45 | bool isvalid(int x, int y) 46 | { 47 | return (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y]); 48 | } 49 | 50 | int solve() 51 | { 52 | vis[r][c] = true; 53 | push(r, c, 0); 54 | while (!isempty()) 55 | { 56 | node temp = pop(); 57 | int x = temp.x; 58 | int y = temp.y; 59 | int l = temp.l; 60 | 61 | if (x == s && y == k) 62 | { 63 | return l; 64 | } 65 | 66 | for (int i = 0; i < 8; i++) 67 | { 68 | int r = x + dir[i][0]; 69 | int c = y + dir[i][1]; 70 | 71 | if (isvalid(r, c)) 72 | { 73 | vis[r][c] = true; 74 | push(r, c, l + 1); 75 | } 76 | } 77 | } 78 | return 0; 79 | } 80 | 81 | int main() 82 | { 83 | int t; 84 | cin >> t; 85 | while (t--) 86 | { 87 | cin >> n >> m; 88 | cin >> r >> c >> s >> k; 89 | for (int i = 0; i < n; i++) 90 | { 91 | for (int j = 0; j < m; j++) 92 | { 93 | vis[i][j] = false; 94 | } 95 | } 96 | cout << solve() << endl; 97 | cout << endl; 98 | } 99 | 100 | return 0; 101 | } -------------------------------------------------------------------------------- /mrLee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | int map[12][12]; 6 | int dp[4096][12] = {-1}; 7 | 8 | int solve(int stat, int city, int comp) 9 | { 10 | if (stat == comp) 11 | { 12 | // cout << ans + map[city][0] << " " << city << endl; 13 | return map[city][0]; 14 | } 15 | 16 | if (dp[stat][city] != -1) 17 | { 18 | return dp[stat][city]; 19 | } 20 | 21 | int ans = INT_MAX, temp = 0; 22 | for (int i = 0; i < n; i++) 23 | { 24 | // cout << ans << " " << temp << " " << (stat & (1 << i)); 25 | if ((stat & (1 << i)) == 0) 26 | { 27 | // cout << city << " " << i << " " << stat << " " << ans << endl; 28 | temp = map[city][i] + solve((stat | (1 << i)), i, comp); 29 | // cout << " " << temp << endl; 30 | if (temp < ans) 31 | { 32 | ans = temp; 33 | // cout << "city = " << city << " " << i << " " << ans << endl; 34 | } 35 | else 36 | { 37 | // cout << "rejected = " << ans << " temp = " << temp << " city = " << city << " " << i << endl; 38 | } 39 | } 40 | } 41 | dp[stat][city] = ans; 42 | return ans; 43 | } 44 | 45 | int main() 46 | { 47 | int t; 48 | cin >> t; 49 | while (t--) 50 | { 51 | cin >> n; 52 | for (int i = 0; i < n; i++) 53 | { 54 | for (int j = 0; j < n; j++) 55 | { 56 | cin >> map[i][j]; 57 | } 58 | } 59 | for (int i = 0; i < (1 << n); i++) 60 | { 61 | for (int j = 0; j < n; j++) 62 | { 63 | dp[i][j] = -1; 64 | } 65 | } 66 | int comp = (1 << n) - 1; 67 | cout << solve(1, 0, comp) << endl; 68 | for (int i = 0; i < (1 << n); i++) 69 | { 70 | cout << i << " "; 71 | for (int j = 0; j < n; j++) 72 | { 73 | cout << dp[i][j] << " "; 74 | } 75 | cout << endl; 76 | } 77 | } 78 | 79 | return 0; 80 | } -------------------------------------------------------------------------------- /maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | int maze[10][10] = {0}; 6 | int matans[10][10] = {0}; 7 | 8 | bool isvalid(int x, int y) 9 | { 10 | return ((maze[x][y] == 0 || maze[x][y] == 2) && x >= 0 && x < n && y >= 0 && y < n); 11 | } 12 | 13 | int ans = -1; 14 | 15 | int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; 16 | 17 | void solve(int r, int c, int jewel) 18 | { 19 | if (r == n - 1 && c == n - 1) 20 | { 21 | if (jewel > ans) 22 | { 23 | ans = jewel; 24 | for (int i = 0; i < n; i++) 25 | { 26 | for (int j = 0; j < n; j++) 27 | { 28 | matans[i][j] = maze[i][j]; 29 | } 30 | } 31 | } 32 | } 33 | 34 | for (int i = 0; i < 4; i++) 35 | { 36 | int x = r + dir[i][0]; 37 | int y = c + dir[i][1]; 38 | 39 | if (isvalid(x, y)) 40 | { 41 | int check; 42 | if (maze[x][y] == 2) 43 | { 44 | check = 1; 45 | } 46 | else if (maze[x][y] == 0) 47 | { 48 | check = 0; 49 | } 50 | maze[x][y] = 3; 51 | solve(x, y, jewel + check); 52 | if (check == 1) 53 | { 54 | maze[x][y] = 2; 55 | } 56 | else 57 | { 58 | maze[x][y] = 0; 59 | } 60 | } 61 | } 62 | } 63 | 64 | int main() 65 | { 66 | int t; 67 | cin >> t; 68 | while (t--) 69 | { 70 | cin >> n; 71 | for (int i = 0; i < n; i++) 72 | { 73 | for (int j = 0; j < n; j++) 74 | { 75 | cin >> maze[i][j]; 76 | // matans[i][j] = 0; 77 | } 78 | } 79 | ans = -1; 80 | maze[0][0] = 3; 81 | solve(0, 0, 0); 82 | cout << endl; 83 | cout << ans << endl; 84 | for (int i = 0; i < n; i++) 85 | { 86 | for (int j = 0; j < n; j++) 87 | { 88 | cout << matans[i][j] << " "; 89 | } 90 | cout << endl; 91 | } 92 | } 93 | 94 | return 0; 95 | } -------------------------------------------------------------------------------- /crowpots.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, k; 5 | int arr[1000]; 6 | int dp[1000][1000]; 7 | 8 | int ans = 100000; 9 | 10 | void merge(int arr[], int l, int m, int r) 11 | { 12 | int i, j, k; 13 | int n1 = m - l + 1; 14 | int n2 = r - m; 15 | int fh[n1], sh[n2]; 16 | 17 | for (i = 0; i < n1; i++) 18 | { 19 | fh[i] = arr[l + i]; 20 | } 21 | for (j = 0; j < n2; j++) 22 | { 23 | sh[j] = arr[m + 1 + j]; 24 | } 25 | 26 | i = 0, j = 0, k = l; 27 | 28 | while (i < n1 && j < n2) 29 | { 30 | if (fh[i] <= sh[j]) 31 | { 32 | arr[k] = fh[i]; 33 | i++; 34 | } 35 | else 36 | { 37 | arr[k] = sh[j]; 38 | j++; 39 | } 40 | k++; 41 | } 42 | while (i < n1) 43 | { 44 | arr[k] = fh[i]; 45 | i++; 46 | k++; 47 | } 48 | while (j < n2) 49 | { 50 | arr[k] = sh[j]; 51 | j++; 52 | k++; 53 | } 54 | } 55 | 56 | void mergesort(int arr[], int l, int r) 57 | { 58 | if (l < r) 59 | { 60 | int m = l + (r - l) / 2; 61 | mergesort(arr, l, m); 62 | mergesort(arr, m + 1, r); 63 | merge(arr, l, m, r); 64 | } 65 | } 66 | 67 | int solve() 68 | { 69 | for (int i = 1; i <= n; i++) 70 | { 71 | dp[i][1] = (n - i + 1) * arr[i]; 72 | } 73 | 74 | for (int i = 1; i <= n; i++) 75 | { 76 | for (int j = 2; j <= k; j++) 77 | { 78 | for (int p = i + 1; p <= n; p++) 79 | { 80 | dp[i][j] = min(dp[i][j], dp[p][j - 1] + (p - i) * arr[i]); 81 | } 82 | } 83 | } 84 | 85 | for (int i = 0; i < n; i++) 86 | { 87 | ans = min(ans, dp[i][k]); 88 | } 89 | return ans; 90 | } 91 | 92 | int main() 93 | { 94 | int t; 95 | cin >> t; 96 | while (t--) 97 | { 98 | cin >> n >> k; 99 | for (int i = 1; i <= n; i++) 100 | { 101 | cin >> arr[i]; 102 | } 103 | 104 | mergesort(arr, 1, n); 105 | 106 | for (int i = 0; i < 1000; i++) 107 | { 108 | for (int j = 0; j < 1000; j++) 109 | { 110 | dp[i][j] = 100000; 111 | } 112 | } 113 | cout << solve() << endl; 114 | } 115 | 116 | return 0; 117 | } -------------------------------------------------------------------------------- /bstinbinary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node 5 | { 6 | int data; 7 | node *left; 8 | node *right; 9 | }; 10 | 11 | struct info 12 | { 13 | int size; 14 | int min; 15 | int max; 16 | int ans; 17 | bool isbst; 18 | }; 19 | 20 | node *newnode(int x) 21 | { 22 | node *newnode = new node(); 23 | newnode->data = x; 24 | newnode->left = NULL; 25 | newnode->right = NULL; 26 | return newnode; 27 | } 28 | 29 | node *insert(node *head, int arr[], int i, int n) 30 | { 31 | if (i < n) 32 | { 33 | node *temp = newnode(arr[i]); 34 | head = temp; 35 | head->left = insert(head->left, arr, 2 * i + 1, n); 36 | head->right = insert(head->right, arr, 2 * i + 2, n); 37 | } 38 | 39 | return head; 40 | } 41 | 42 | void inorder(node *head) 43 | { 44 | if (head == NULL) 45 | { 46 | return; 47 | } 48 | inorder(head->left); 49 | cout << head->data << " "; 50 | inorder(head->right); 51 | } 52 | 53 | info findbst(node *head) 54 | { 55 | if (head == NULL) 56 | { 57 | info temp; 58 | temp.ans = 0; 59 | temp.isbst = true; 60 | temp.max = INT_MIN; 61 | temp.min = INT_MAX; 62 | temp.size = 0; 63 | return temp; 64 | } 65 | if (head->left == NULL && head->right == NULL) 66 | { 67 | info temp; 68 | temp.ans = 1; 69 | temp.isbst = true; 70 | temp.max = head->data; 71 | temp.min = head->data; 72 | temp.size = 1; 73 | return temp; 74 | } 75 | 76 | info l = findbst(head->left); 77 | info r = findbst(head->right); 78 | 79 | info res; 80 | res.size = 1 + l.size + r.size; 81 | if (l.isbst && r.isbst && l.max < head->data && r.min > head->data) 82 | { 83 | res.min = min(l.min, min(r.min, head->data)); 84 | res.max = max(r.max, max(l.max, head->data)); 85 | res.ans = res.size; 86 | res.isbst = true; 87 | // cout << head->data << " " << res.ans << endl; 88 | return res; 89 | } 90 | res.ans = max(l.ans, r.ans); 91 | res.isbst = false; 92 | return res; 93 | } 94 | 95 | int main() 96 | { 97 | int t, n; 98 | cin >> t; 99 | while (t--) 100 | { 101 | cin >> n; 102 | int nodearr[n]; 103 | node *head = NULL; 104 | for (int i = 0; i < n; i++) 105 | { 106 | cin >> nodearr[i]; 107 | } 108 | head = insert(head, nodearr, 0, n); 109 | inorder(head); 110 | cout << endl; 111 | info res = findbst(head); 112 | cout << res.ans; 113 | } 114 | 115 | return 0; 116 | } -------------------------------------------------------------------------------- /oldmobile.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node 5 | { 6 | int data; 7 | int touch; 8 | }; 9 | 10 | node queue[1000]; 11 | int vis[10000]; 12 | int digit[10]; 13 | int op[4]; 14 | 15 | int front, back, n, o, lim, num; 16 | 17 | void init() 18 | { 19 | front = back = 0; 20 | } 21 | 22 | void push(int data, int touch) 23 | { 24 | queue[back].data = data; 25 | queue[back].touch = touch; 26 | back = (back + 1) % 1000; 27 | return; 28 | } 29 | 30 | node pop() 31 | { 32 | node temp = queue[front]; 33 | front = (front + 1) % 1000; 34 | return temp; 35 | } 36 | 37 | bool isempty() 38 | { 39 | return front == back; 40 | } 41 | 42 | int solve() 43 | { 44 | node ans; 45 | while (!isempty()) 46 | { 47 | node temp = pop(); 48 | int data = temp.data; 49 | int touch = temp.touch; 50 | 51 | if (touch > lim) 52 | { 53 | continue; 54 | } 55 | 56 | if (data == num) 57 | { 58 | ans.data = data; 59 | ans.touch = touch; 60 | break; 61 | } 62 | 63 | if (!vis[data]) 64 | { 65 | for (int i = 0; i < n; i++) 66 | { 67 | push(10 * data + digit[i], touch + 1); 68 | } 69 | 70 | for (int i = 0; i < o; i++) 71 | { 72 | for (int j = 0; j < 1000; j++) 73 | { 74 | if (vis[j]) 75 | { 76 | 77 | if (op[i] == 1) 78 | { 79 | cout << data + j << endl; 80 | push(data + j, touch + 3); 81 | } 82 | else if (op[i] == 2 && data - j >= 0) 83 | { 84 | cout << data - j << endl; 85 | push(data - j, touch + 3); 86 | } 87 | else if (op[i] == 3) 88 | { 89 | cout << data * j << endl; 90 | push(data * j, touch + 3); 91 | } 92 | else if (op[i] == 4 && j != 0) 93 | { 94 | cout << data / j << endl; 95 | push(data / j, touch + 3); 96 | } 97 | } 98 | } 99 | } 100 | vis[data] = true; 101 | } 102 | } 103 | return ans.touch; 104 | } 105 | 106 | int main() 107 | { 108 | int t; 109 | cin >> t; 110 | while (t--) 111 | { 112 | cin >> n >> o >> lim; 113 | int x; 114 | init(); 115 | for (int i = 0; i < n; i++) 116 | { 117 | cin >> x; 118 | digit[i] = x; 119 | push(x, 1); 120 | } 121 | for (int i = 0; i < o; i++) 122 | { 123 | cin >> x; 124 | op[i] = x; 125 | } 126 | cin >> num; 127 | cout << solve(); 128 | } 129 | 130 | return 0; 131 | } -------------------------------------------------------------------------------- /rareElements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int map[20][20]; 5 | bool vis[20][20]; 6 | int rare[5][2]; 7 | int n, r; 8 | 9 | struct node 10 | { 11 | int x; 12 | int y; 13 | int l; 14 | }; 15 | 16 | node q[1000]; 17 | int front, back; 18 | 19 | void init() 20 | { 21 | front = back = 0; 22 | return; 23 | } 24 | void init2() 25 | { 26 | for (int i = 0; i < 20; i++) 27 | { 28 | for (int j = 0; j < 20; j++) 29 | { 30 | vis[i][j] = 0; 31 | } 32 | } 33 | return; 34 | } 35 | 36 | bool isempty() 37 | { 38 | return (front == back); 39 | } 40 | 41 | void push(int x, int y, int l) 42 | { 43 | q[back].x = x; 44 | q[back].y = y; 45 | q[back].l = l; 46 | back++; 47 | return; 48 | } 49 | 50 | node pop() 51 | { 52 | if (!isempty()) 53 | { 54 | return q[front++]; 55 | } 56 | exit(EXIT_FAILURE); 57 | } 58 | 59 | bool isvalid(int r, int c) 60 | { 61 | return (r >= 0 && r < n && c >= 0 && c < n); 62 | } 63 | 64 | int dir[4][2] = { 65 | {1, 0}, 66 | {0, 1}, 67 | {-1, 0}, 68 | {0, -1}}; 69 | 70 | int bfs(int sx, int sy, int dx, int dy) 71 | { 72 | push(sx, sy, 0); 73 | vis[sx][sy] = 1; 74 | while (!isempty()) 75 | { 76 | node n = pop(); 77 | // cout << n.x << " " << n.y << endl; 78 | if (n.x == dx && n.y == dy) 79 | { 80 | return n.l; 81 | } 82 | for (int i = 0; i < 4; i++) 83 | { 84 | int valx = n.x + dir[i][0]; 85 | int valy = n.y + dir[i][1]; 86 | int lvl = n.l + 1; 87 | if (isvalid(valx, valy) && map[valx][valy] == 1 && vis[valx][valy] == 0) 88 | { 89 | push(valx, valy, lvl); 90 | vis[valx][valy] = 1; 91 | } 92 | } 93 | } 94 | return -1; 95 | } 96 | 97 | int main() 98 | { 99 | int t, x, y; 100 | cin >> t; 101 | int m = t; 102 | while (t--) 103 | { 104 | cin >> n >> r; 105 | for (int j = 0; j < r; j++) 106 | { 107 | cin >> x >> y; 108 | x--; 109 | y--; 110 | rare[j][0] = x; 111 | rare[j][1] = y; 112 | } 113 | for (int j = 0; j < n; j++) 114 | { 115 | for (int k = 0; k < n; k++) 116 | { 117 | cin >> map[j][k]; 118 | vis[j][k] = 0; 119 | } 120 | } 121 | 122 | int ans = 10000; 123 | 124 | for (int j = 0; j < n; j++) 125 | { 126 | for (int k = 0; k < n; k++) 127 | { 128 | if (map[j][k] == 1) 129 | { 130 | int temp = 0; 131 | for (int l = 0; l < r; l++) 132 | { 133 | init(); 134 | init2(); 135 | int res = bfs(j, k, rare[l][0], rare[l][1]); 136 | // cout << "res = " << res << endl; 137 | temp = max(temp, res); 138 | } 139 | 140 | ans = min(temp, ans); 141 | } 142 | } 143 | } 144 | cout << "#" << m - t << " " << ans << endl; 145 | } 146 | return 0; 147 | } -------------------------------------------------------------------------------- /endoscope.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node 5 | { 6 | int x; 7 | int y; 8 | int l; 9 | }; 10 | 11 | node q[10000]; 12 | int front, rear; 13 | 14 | int map[50][50]; 15 | int vis[50][50] = {0}; 16 | int t, n, m, ans, srcx, srcy, len; 17 | 18 | void init() 19 | { 20 | front = 0; 21 | rear = 0; 22 | } 23 | 24 | bool isempty() 25 | { 26 | return rear == front; 27 | } 28 | 29 | void push(int x, int y, int l) 30 | { 31 | q[rear].x = x; 32 | q[rear].y = y; 33 | q[rear].l = l; 34 | rear++; 35 | } 36 | 37 | node pop() 38 | { 39 | // if (!isempty()) 40 | // { 41 | return q[front++]; 42 | // } 43 | // exit(EXIT_FAILURE); 44 | } 45 | 46 | bool isvalid(int x, int y) 47 | { 48 | return (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y]); 49 | } 50 | 51 | int solve() 52 | { 53 | if (map[srcx][srcy] == 0) 54 | { 55 | return -1; 56 | } 57 | ans = 1; 58 | vis[srcx][srcy] = 1; 59 | push(srcx, srcy, 1); 60 | while (!isempty()) 61 | { 62 | node newnode = pop(); 63 | int x = newnode.x; 64 | int y = newnode.y; 65 | int l = newnode.l; 66 | 67 | if (isvalid(x - 1, y) && l < len && 68 | (map[x - 1][y] == 1 || map[x - 1][y] == 2 || map[x - 1][y] == 5 || map[x - 1][y] == 6) && 69 | (map[x][y] == 1 || map[x][y] == 2 || map[x][y] == 4 || map[x][y] == 7)) 70 | { 71 | ans++; 72 | vis[x - 1][y] = 1; 73 | push(x - 1, y, l + 1); 74 | } 75 | 76 | if (isvalid(x + 1, y) && l < len && 77 | (map[x + 1][y] == 1 || map[x + 1][y] == 2 || map[x + 1][y] == 4 || map[x + 1][y] == 7) && 78 | (map[x][y] == 1 || map[x][y] == 2 || map[x][y] == 5 || map[x][y] == 6)) 79 | { 80 | ans++; 81 | vis[x + 1][y] = 1; 82 | push(x + 1, y, l + 1); 83 | } 84 | 85 | if (isvalid(x, y - 1) && l < len && 86 | (map[x][y - 1] == 1 || map[x][y - 1] == 3 || map[x][y - 1] == 4 || map[x][y - 1] == 5) && 87 | (map[x][y] == 1 || map[x][y] == 3 || map[x][y] == 6 || map[x][y] == 7)) 88 | { 89 | ans++; 90 | vis[x][y - 1] = 1; 91 | push(x, y - 1, l + 1); 92 | } 93 | 94 | if (isvalid(x, y + 1) && l < len && 95 | (map[x][y + 1] == 1 || map[x][y + 1] == 3 || map[x][y + 1] == 6 || map[x][y + 1] == 7) && 96 | (map[x][y] == 1 || map[x][y] == 3 || map[x][y] == 4 || map[x][y] == 5)) 97 | { 98 | ans++; 99 | vis[x][y + 1] = 1; 100 | push(x, y + 1, l + 1); 101 | } 102 | } 103 | return ans; 104 | } 105 | 106 | int main() 107 | { 108 | cin >> t; 109 | int res[t]; 110 | for (int i = 0; i < t; i++) 111 | { 112 | cin >> n >> m >> srcx >> srcy >> len; 113 | for (int j = 0; j < n; j++) 114 | { 115 | for (int k = 0; k < m; k++) 116 | { 117 | cin >> map[j][k]; 118 | vis[j][k] = 0; 119 | } 120 | } 121 | init(); 122 | res[i] = solve(); 123 | } 124 | 125 | for (int i = 0; i < t; i++) 126 | { 127 | cout << "#" << i + 1 << " " << res[i] << endl; 128 | } 129 | 130 | return 0; 131 | } -------------------------------------------------------------------------------- /distancetoleaf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | 6 | struct node 7 | { 8 | int data; 9 | node *left; 10 | node *right; 11 | }; 12 | 13 | node *newNode(int x) 14 | { 15 | node *newnode = new node(); 16 | newnode->data = x; 17 | newnode->left = NULL; 18 | newnode->right = NULL; 19 | return newnode; 20 | } 21 | 22 | node *insert(node *head, int arr[], int i) 23 | { 24 | if (i < n && arr[i] != 0) 25 | { 26 | node *temp = newNode(arr[i]); 27 | head = temp; 28 | head->left = insert(head->left, arr, 2 * i + 1); 29 | head->right = insert(head->right, arr, 2 * i + 2); 30 | } 31 | if (arr[i] == 0) 32 | { 33 | head = NULL; 34 | } 35 | 36 | return head; 37 | } 38 | 39 | node *search(node *head, int x) 40 | { 41 | if (head == NULL) 42 | { 43 | return NULL; 44 | } 45 | if (head->data == x) 46 | { 47 | return head; 48 | } 49 | node *l, *r; 50 | if (head->left) 51 | { 52 | l = search(head->left, x); 53 | if (l != NULL) 54 | { 55 | return l; 56 | } 57 | } 58 | if (head->right) 59 | { 60 | r = search(head->right, x); 61 | if (r != NULL) 62 | { 63 | return r; 64 | } 65 | } 66 | return NULL; 67 | } 68 | 69 | int searchnode(node *head) 70 | { 71 | if (head == NULL) 72 | { 73 | return 0; 74 | } 75 | 76 | if (head->left == NULL && head->right == NULL) 77 | { 78 | return 1; 79 | } 80 | if (!head->left) 81 | { 82 | return searchnode(head->right) + 1; 83 | } 84 | if (!head->right) 85 | { 86 | return searchnode(head->left) + 1; 87 | } 88 | 89 | int l = searchnode(head->left); 90 | int r = searchnode(head->right); 91 | return 1 + min(l, r); 92 | } 93 | 94 | int searchparent(node *head, node *x, int &mindist) 95 | { 96 | if (head == NULL) 97 | return -1; 98 | if (head == x) 99 | return 0; 100 | 101 | int h; 102 | int l = searchparent(head->left, x, mindist); 103 | if (l != -1) 104 | { 105 | h = searchnode(head->right); 106 | cout << h << " hr " << head->right->data << endl; 107 | mindist = min(mindist, h + l + 1); 108 | return h + l + 1; 109 | } 110 | int r = searchparent(head->right, x, mindist); 111 | if (r != -1) 112 | { 113 | h = searchnode(head->left); 114 | cout << h << " hl " << head->left->data << endl; 115 | mindist = min(mindist, h + r + 1); 116 | return h + r + 1; 117 | } 118 | return -1; 119 | } 120 | 121 | int solve(node *head, int x) 122 | { 123 | int mindist = INT_MAX; 124 | node *xloc = search(head, x); 125 | int dist = searchnode(xloc); 126 | searchparent(head, xloc, mindist); 127 | cout << dist << " " << mindist << endl; 128 | mindist = min(dist, mindist); 129 | return mindist; 130 | } 131 | 132 | void inorder(node *head) 133 | { 134 | if (head == NULL) 135 | { 136 | return; 137 | } 138 | inorder(head->left); 139 | cout << head->data << " "; 140 | inorder(head->right); 141 | } 142 | 143 | int main() 144 | { 145 | int t, x; 146 | cin >> t; 147 | while (t--) 148 | { 149 | cin >> n; 150 | int arr[n]; 151 | node *head = NULL; 152 | for (int i = 0; i < n; i++) 153 | { 154 | cin >> arr[i]; 155 | } 156 | head = insert(head, arr, 0); 157 | cin >> x; 158 | inorder(head); 159 | cout << endl; 160 | cout << solve(head, x); 161 | } 162 | return 0; 163 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Samsung Research Institute Interview Questions 2 | 3 | - [Coding Round Questions](#coding) 4 | - [Technical Interview Questions](#tech) 5 | - [Data Structures and Algorithms](#dsalg) 6 | - [DBMS](#dbms) 7 | - [Operating System](#os) 8 | - [Miscellaneous](#misc) 9 | 10 | --- 11 | 12 | ## Coding round questions
13 | 14 | #### Popular Tags 15 | 16 | dynamic-programming 17 | Graph 18 | backtracking 19 | DFS 20 | BFS 21 | Bipartite-Graph 22 | 23 | - Company S has developed an industrial endoscope available to explore inner part of the decrepit water pipes. 24 | It is possible to explore the inner part of the pipes putting the endoscope in a certain part of the pipe. 25 | The endoscope can be moved in the pipe only. Meanwhile, when the pipes are connected, if the length of the endoscope is long enough to explore, then it can inspect the connected pipes. However, we cannot observe every pipe because the length of the endoscope is limited. 26 | 27 | When the map of the ground water pipe, the location where the endoscope to out in, and the length of the endoscope is given, calculate the number of pipe which are available to explore. Length of endoscope means the range upto which endoscope can explore. There are seven kind of pipes, and description for each pipe are shown below: 28 | 29 | | S.No | Pipe | Connected to | 30 | | ---- | :--------------------------------------------------------------------------------: | --------------------: | 31 | | 1 | ![](https://github.com/rishabh115/InterviewQuestions/raw/master/Samsung/res/1.png) | Up, Down, Left, Right | 32 | | 2 | ![](https://github.com/rishabh115/InterviewQuestions/raw/master/Samsung/res/2.png) | Up, Down | 33 | | 3 | ![](https://github.com/rishabh115/InterviewQuestions/raw/master/Samsung/res/3.png) | Left, Right | 34 | | 4 | ![](https://github.com/rishabh115/InterviewQuestions/raw/master/Samsung/res/4.png) | Up, Right | 35 | | 5 | ![](https://github.com/rishabh115/InterviewQuestions/raw/master/Samsung/res/5.png) | Down, Right | 36 | | 6 | ![](https://github.com/rishabh115/InterviewQuestions/raw/master/Samsung/res/6.png) | Down, Left | 37 | | 7 | ![](https://github.com/rishabh115/InterviewQuestions/raw/master/Samsung/res/7.png) | Up, Left | 38 | 39 | When the map of the groundwater pipe, the location where the endoscope to out in, and the length of the endoscope is given, calculate the number of the pipe which are available to explore. Length of endoscope means the range up to which endoscope can explore. 40 | 41 | #### Input 42 | 43 | In the first line, T, the number of total test cases are given. From the second line, T test cases are given. In the first line of each test case, N, the height of the map of the groundwater pipes, M, the width, R, the vertical location of the water pipe where to put in the endoscope, C, the horizontal location of it, and the length of the endoscope L are given. In the following N lines, information about the map of the groundwater pipe is given. Each line has M numbers. Each number (from 1 to 7) means the type of water pipe for that point. 0 means there is no water pipe buried in that place. 44 | 45 | #### Output 46 | 47 | Print the respective answer for T test cases in total for T lines. The answer is the number of water pipes which is available to observe using the endoscope. 48 | 49 | #### Constraints 50 | 51 | 1≤ T ≤100 52 | 53 | 1≤ N, M ≤50 54 | 55 | 0≤ X < N 56 | 57 | 0≤ Y < M 58 | 59 | 1≤ L ≤ 20 60 | 61 | - Men's restroom problem: It is a well-researched fact that men in a restroom generally prefer to maximize their distance from already occupied stalls, by occupying the middle of the longest sequence of unoccupied places. For a detailed version, check the following link.
62 | Link: https://stackoverflow.com/questions/32645046/urinal-algorithm-a-simple-optimization 63 | 64 | - Given a graph print either of the set of the vertices that are colored with the same color. And if the graph is not bipartite print “-1”. Test cases also included cases when a graph is not connected. 65 | 66 | - You’ll be given a grid as below: 67 | `0 1 0 2 0 --> Non highlighted part 0 2 2 2 1 0 2 1 1 1 1 0 1 0 0 0 0 1 2 2 1 1 0 0 1 x x S x x -->highlighted yellow` 68 | In the grid above, 69 | 1: This cell has a coin. 70 | 2: This cell has an enemy. 71 | 0: It contains nothing. 72 | The highlighted(yellow) zone is the control zone. S is a spaceship that we need to control so that we can get maximum coins. 73 | Now, S’s initial position will be at the center and we can only move it right or left by one cell or do not move. 74 | At each time, the non-highlighted part of the grid will move down by one unit. 75 | We can also use a bomb but only once. If we use that, all the enemies in the 5×5 region above the control zone will be killed. 76 | If we use a bomb at the very beginning, the grid will look like this: 77 | ```` 78 | 0 1 0 2 0 --> Non highlighted part 79 | 0 0 0 0 1 80 | 0 0 1 1 1 81 | 1 0 1 0 0 82 | 0 0 1 0 0 83 | 1 1 0 0 1 84 | x x S x x --> highlighted yellow 85 | ``` 86 | As soon as, the spaceship encounters an enemy or the entire grid has come down, the game ends. 87 | For example, 88 | At the very first instance, if we want to collect a coin we should move left **( coins=1)**. This is because when the grid comes down by 1 unit we have a coin on the second position and by moving left we can collect that coin. Next, we should move right to collect another coin **( coins=2)** . 89 | After this, remain at the same position **( coins=4)**. 90 | This is the current situation after collecting 4 coins. 91 | ``` 92 | 0 1 0 2 0 0 1 0 0 0 93 | 0 2 2 2 1 -->after using 0 0 0 0 1 94 | x x S x x -->bomb x x S x x 95 | ```` 96 | Now, we can use the bomb to get out of this situation. After this, we can collect at most 1 coin. So maximum coins=5. 97 | - A Research team want to establish a research center in a region where they found some rare-elements.They want to make it closest to all the rare-elements as close as possible so that they can reduce overall cost of research over there. It is given that all the rare elements location is connected by roads. It is also given that Research Center can only be built on the road. The team decided to assign this task to a coder.If you feel you have that much potential. 98 | Here is the Task:- Find the shortest of the longest distance of the research centre from given locations of rare-elements. 99 | locations are given in the matrix cell form where 1 represents roads and 0 no road... 100 | number of rare-element and their location was also given(number<=5) 101 | and order of square matrix was less than equal to (20). 102 | - Given a Binary Tree where each node has positive and negative values. Convert this to a tree where each node contains the sum of the left and right subtrees in the original tree. The values of leaf nodes are changed to 0. 103 | - Write a function that calculates the day of the week for any particular date in the past or future. A typical application is to calculate the day of the week on which someone was born or some other special event occurred. 104 | - Given a Binary Tree, write a function that returns the size of the largest subtree which is also a Binary Search Tree (BST). If the complete Binary Tree is BST, then return the size of the whole tree. 105 | - You are given an array of integers which represents positions available and an integer c(cows). 106 | Now you have to choose c positions such that the minimum difference between cows is maximized. 107 | ``` 108 | For example, 109 | 1 3 5 8 10 110 | c=3 111 | output: 4 112 | 1 5 10 113 | ``` 114 | - Given a Binary Tree and a node x in it, find the distance of the closest leaf to x in Binary Tree. If given node itself is a leaf, then the distance is 0. 115 | - Given random points in a 2-D plane, construct a convex polygon with a minimum area of coverage and which encompasses all the given points. 116 | - Given a graph, find out if it can be colored using 2 colors. If Yes, print numbers of vertices with any one of the colour, followed by such vertices in sorted order. If No just print -1 117 | - Given a 2 D matrix where 1 represents the places where the frog can jump and 0 represent the empty spaces, the frog can move freely in the horizontal direction (on 1’s only) without incurring any cost (jump). A vertical jump from a given point of the matrix to another point on the matrix can be taken (on 1’s only) with cost as the number of jumps taken. 118 | Given a source and destination, the frog has to reach the destination minimizing the cost (jump) 119 | - Given a directed graph. Check whether a graph contain a cycle or not. 120 | - Given a level K , you have to find out the sum of data of all the nodes at level K in a binary tree. 121 | Input is given as: 122 | `(P(C()())(C()())) P is for Parent, C is for child. if parent has one child : (P(C()())()) if parent has no child : (P()())` 123 | - A company sells its products with a unique serial number on it. Company has has found that there are some products that don’t sell well which are identified to have ominous numbers in the serial number of the product. So if a serial number of the product contains atmost ’k’ ominous number then it won’t sell. 124 | Given a range form s to e, you need to find number of products that would sell, leaving out the products that contains atmost ’k’ ominous numbers. `Input: First line contains the number of test cases, followed by the range s to e, 1` 125 | 126 | - You are given N unique numbers a1 2 operations. (Each touch is considered an operation). 179 | If you have to type 5 -> '1+4=' that requires 4 operations. There could be other ways to make '5'. 180 | The goal is to find minimum operations. 181 | 182 | - There are N pots. Every pot has some water in it. They may be partially filled. Every pot is associated with overflow number O which tell how many minimum no. of stones required for that pot to overflow. The crow knows O1 to On(overflow no. for all the pots). Crow wants some K pots to be overflow. So the task is a minimum number of stones he can make K pots overflow in the worst case. 183 | 184 | Array of overflow no--. {1,...,On} 185 | Number of pots--n 186 | No of pots to overflow-- k 187 | 188 | Let say two pots are there with overflow no.s {5,58}, and the crow has to overflow one pot(k=1). So crow will put 5 stones in a pot with overflow no.(58), it will not overflow, then he will put in the pot with overflow no.(5), hence the total no. of stones to make overflow one-pot is=10. 189 | 190 | - You are given 2 convex hulls. Find all the common points that lie in the intersection of these 2 convex hulls. 191 | 192 | - There is one spaceship. X and Y co-ordinate of the source of spaceship and destination spaceship is given. There is N number of wormholes; each wormhole has 5 values. 193 | First 2 values are starting co-ordinate of the wormhole and after that value no. 3 and 4 represent ending co-ordinate of the wormhole and last 5th value is represents a cost to pass through this wormhole. Now, these warmholes are bi-directional. 194 | Now the to go from (x1,y1) to (x2,y2) is abs(x1-x2)+abs(y1-y2). 195 | The main problem here is to find the minimum distance to reach spaceship from source to destination co-ordinate using any number of warm-hole. It is ok if you won't uses any warmhole. 196 | - There is an island surrounded by oil mines. You will be given n companies and m oil mines having values. You have to distribute the mines to "n" companies in fair manner. Remember the companies can have oil mines adjacent to each other and not in between of each other.After distributing them compute the difference of oil mines from the company getting highest and company getting lowest. This number should be minimum.(then only the distribution can be termed as fair). 197 | 198 | Example 199 | Input 200 | 2 201 | 2 4 202 | 6 13 10 2 203 | 2 4 204 | 6 10 13 2 205 | 206 | output 207 | 5 208 | 1 209 | 210 | - There is a n x n matrix with only 0s & 1s. Letters are formed using 1 and 0. 211 | For eg.- 212 | U is 1 0 1, V is 1 0 1 213 | 1 0 1 1 0 1 214 | 1 1 1 0 1 0 215 | 216 | Likewise there are 6 letters and they can rotated in 90, 180, 270, 360 degree. And if there is a letter, the next column would be filled with 0. 217 | Eg. 218 | V- 1 0 1 0 219 | 1 0 1 0 220 | 0 1 0 0 221 | 222 | So we have to count the number of each letter in the matrix. 223 | 224 | - There is a maze that has one entrance and one exit. 225 | Jewels are placed in passages of the maze. 226 | You want to pick up the jewels after getting into the maze through the entrance and before getting out of it through the exit. 227 | You want to get as many jewels as possible, but you don’t want to take the same passage you used once. 228 | 229 | When locations of a maze and jewels are given, 230 | find out the greatest number of jewels you can get without taking the same passage twice, and the path taken in this case. 231 | 232 | ![](https://github.com/rishabh115/InterviewQuestions/blob/master/Samsung/res/jewel.jpg?raw=true) 233 | 234 | #### Input 235 | 236 | There can be more than one test case in the input file. The first line has T, the number of test cases. 237 | Then the totally T test cases are provided in the following lines (T ≤ 10 ). 238 | 239 | In each test case, 240 | In the first line, the size of the maze N (1 ≤ N ≤ 10) is given. The maze is N×N square-shaped. 241 | From the second line through N lines, information of the maze is given. 242 | “0” means a passage, “1” means a wall, and “2” means a location of a jewel. 243 | The entrance is located on the upper-most left passage and the exit is located on the lower-most right passage. 244 | There is no case where the path from the entrance to the exit doesn’t exist. 245 | 246 | #### Output 247 | 248 | From the first line through N lines, mark the path with 3 and output it. 249 | In N+1 line, output the greatest number of jewels that can be picked up. 250 | Each test case must be output separately as a empty. 251 | 252 | - Mr. Lee has to travel various offices abroad to assist branches of each place. But he has a problem. The airfare would be real high as all offices he has to visit are in foreign countries. He wants to visit every location only one time and return home with the lowest expense. 253 | Help this company-caring man calculate the lowest expense. 254 | 255 | Time limit : 1 second (java : 2 seconds) 256 | 257 | #### Input format 258 | 259 | Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 30) are given in a row. 260 | N, the number of offices to visit is given on the first row per each test case. At this moment, No. 1 office is regarded as his company (Departure point). (1 ≤ N ≤ 12) 261 | Airfares are given to move cities in which branches are located from the second row to N number rows. I.e. jth number of ith row is the airfare to move from ith city to jth city. If it is impossible to move between two cities, it is given as zero. 262 | 263 | #### Output format 264 | 265 | Output the minimum airfare used to depart from his company, visit all offices, and then return his company on the first row per each test case. 266 | 267 | Example of Input 268 | 269 | 2
270 | 5
271 | 0 14 4 10 20
272 | 14 0 7 8 7
273 | 4 5 0 7 16
274 | 11 7 9 0 2
275 | 18 7 17 4 0
276 | 5
277 | 9 9 2 9 5
278 | 6 3 5 1 5
279 | 1 8 3 3 3
280 | 6 0 9 6 8
281 | 6 6 9 4 8
282 | 283 | Example of Output 284 | 285 | 30
286 | 18 287 | 288 | - There is a mobile piece and a stationary piece on the N×M chessboard. The available moves of the mobile piece are the same as set out in the image below. You need to capture the stationary piece by moving the mobile piece with the minimum amount of moves. 289 | 290 | Write a program to find out the minimum number moves to catch a piece. 291 | 292 | Time limit:1 second (java: 2 seconds) 293 | 294 | #### Input 295 | 296 | Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 20) are given in a row. 297 | N, the numbers of the rows and M, the number of columns of the chessboard are given in the first row of each test case. 298 | R & C is the location information of the attacking piece and S & K is the location of the defending pieces and are given in the row at the second line. However, the location of the uppermost end of the left end is (1, 1) 299 | 300 | #### Output 301 | 302 | For each test case, you should print "Case #T" in the first line where T means the case number. 303 | For each test case, you should output the minimum number of movements to catch a defending piece at the first line of each test case. If not moveable, output equals ‘-1’. 304 | 305 | - You are busy to promote a newly released film in a movie theatre . the title is 'Biochemical Laughing Bomb' which is about terror. Guerillas drop a biochemical laughing bomb in the middle of a city. once exposed, you have to laugh all your life. The bomb will contaminate four people around it during t second, and another four around each of them during another one second. However, you won't be contaminated if you are not in the adjacent four directions. as the below shows the location of the bomb and affected people , and shows contamination process in seconds and you can figure out that the whole city is contaminated in 8 seconds. 306 | In order to protect the city from the epidemic, create a program that figures out when the city will be contaminated by the bomb for the last. 307 | 308 | #### Input 309 | 310 | Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 30) are given in a row. 311 | The row and column of the city, N and M are given by being separated with a blank on the first row of each test case. (1 ≤ N, M ≤ 100) 312 | The status within city is given by being separated with a blank from the second row to N number rows. 1 means people exist and 0 means people do not exist. 313 | The coordinate of the row and column on which the bomb fall is given by being separated with a blank on the last row. 314 | 315 | #### Output 316 | 317 | For each test case, you should print "Case #T" in the first line where T means the case number. For each test case, you should output how long does it take to contaminate al people on the first row of each test case. 318 | 319 | - Given number of pipes 1..n, Find two largest pipes of maximum length possible. 320 | Ex: input - 1,2,3,4,6 321 | Output - The maximum length possible is 8. 322 | Pipe1 - 2,6 323 | Pipe2 - 1,3,4 324 | 325 | - A Research team want to establish a research center in a region where they found some rare-elements. They want to make it closest to all the rare-elements as close as possible so that they can reduce overall cost of research over there. It is given that all the rare-element’s location is connected by roads. It is also given that Research Center can only be build on road. Team decided to assign this task to a coder. If you feel you have that much potential.. 326 | 327 | Here is the Task :- Find the shortest of the longest distance of research center from given locations of rare-elements. 328 | 329 | Locations are given in the matrix cell form where 1 represents roads and 0 no road.. 330 | Number of rare-element and their location was also given(number<=5) 331 | and order of square matrix was less than equal to (20). 332 | 333 | - There is a source (S) and destination (D) and a spacecraft has to go from S to D. There are N number of wormholes in between 334 | which has following properties: 335 | 336 | Each wormhole has an entry and an exit. 337 | Each wormhole is bi-directional i.e. one can enter and exit from any of the ends. 338 | The time to cross the wormhole is given and the space craft may or may not use the wormhole 339 | to reach D. 340 | The time taken to travel outside wormhole between two points (x1, y1) and (x2, y2) is given by a formula 341 | |x1 - x2| + |y1 - y2| 342 | 343 | where, (x1, y1) and (x2, y2) are the co-ordinates of two points. 344 | The co-ordinates of S and D are given and we have to find the minimum time to reach D from S. 345 | 346 | Note: It’s not mandatory to consider all the wormholes. 347 | 348 | - There are n balloons and n bullets and each balloon is assigned with a particular number (point). Whenever a particular balloon is shot the no of points increases by 349 | 1.the multiplication of point assigned to balloon on left and that of right side. 350 | 351 | 2.point assigned to left if no right exists 352 | 353 | 3.point assigned to right if no left exists. 354 | 355 | 4.the point assigned to itself if no other balloon exists. 356 | 357 | You have to output the maximum no of points possible. 358 | 359 | Input 360 | 361 | 1 2 3 4 362 | 363 | Output 364 | 365 | 20 366 | 367 | --- 368 | 369 | ## Technical Interview Questions 370 | 371 |
372 |

Data Structures and Algorithms

373 | 374 | - Write the code of heap sort and complexity in different cases. 375 | - Implement Kadane's Algorithm. 376 | - Kahn's algorithm for topological sorting. 377 | - Which DS is used to manage files and folder in your mobile? 378 | - Write code for Dijkstra Algorithm. 379 | - Find the kth minimum element into binary search. 380 | - What is memory leak? How to avoid it? 381 | - Write a program to create circular queue? 382 | - Detect and remove loop in linked list. 383 | - Design LRU data structure. 384 | - WAP for finding nth node from the end in the linked list. 385 | - Explain KMP Algorithm. 386 | - Zigzag traversal between two singly linked list. 387 | - There is a N\*M matrix where each row is sorted. Find the kth largest element in matrix? 388 | - Print sum of all prime numbers within a given range. 389 | - Find the largest and second largest element in array. Give 4 different approaches. 390 | - Given two polynomials as linked lists, return a linked list which represents the product of two polynomials. 391 | - What is the difference between dynamic programming and divide and conquer? 392 | - Given two polynomials represented by two arrays, write a function that multiplies given two polynomials. 393 | - What are the disadvantages of stack. 394 | - Write a program to find when we get stack overflow if we are using recursive functions. 395 | - How cycle detection is different in directed and undirected graphs? 396 | - Write a program to append a C-style string to the end of another C-style string. You were supposed to 397 | write a function like void concat(char* a , char* b) such that after calling this function a becomes 398 | a+b(concatenation of a and b). Assume that size of a is greater than length of a+length of b.No 399 | library functions were to be used. Not even strlen. 400 | - Why do we need trie data structure?Tell one another technique which does the same job as trie 401 | with same time complexity? Then why trie is preferred over that one?(The other technique which I 402 | told was hashing). 403 | - Given a binary tree remove nodes to make it 'Perfect'. Print all removed nodes. 404 |

405 |

DBMS

406 | 407 | - What is view in DBMS? 408 | - What is Indexing? 409 | - You have a student table having 3 attributes student_name, subject and marks. Find the subject-wise maximum mark of students. Arrange the subjects in ascending order and marks in descending order 410 | - Primary indexing vs Secondary indexing vs cluster indexing, multilevel indexing. 411 | - Sparse indexing vs dense indexing. 412 | - Lossless decomposition vs Lossy decomposition. 413 | - Describe join operation w.r.t databases. 414 |
415 |
416 |

Operating System

417 | 418 | - What is page fault and why it occurs? 419 | - What is virtual memory? 420 | - What is Demand Paging? 421 | - Explain Semaphore. 422 | - While context switching takes place what is stored on stack and heap. 423 | - Write code for Producer-Consumer Problem. 424 | - What is Swap In and Swap Out? 425 | - Differentiate between Starvation and Aging. 426 | - Explain the page replacement Algorithm LRU. 427 | - What is Inter process communication ,types and which one is fast and why? 428 | - What is fragmentation? Define external fragmentation. 429 | - Calculate the number of processes generated using N fork() statements? 430 | - Due to priority of process, what type of problem occurs in priority scheduling? 431 | - What is real time OS. 432 | - Difference between preemptive and non- preemptive algorithms. 433 | - If time slice is greater than the execution time of largest execution time process than round robin acts as...? 434 | - There is a file and 5 processes. How can you grant access so that 435 | only 2 process can write to file and 1 can read file at a time . 436 | - Stack pointer vs Frame pointer. 437 | - Explain spooling. 438 | - You are given a process A. The scheduler is using Priority Based Round Robin Technique for 439 | scheduling various process. Suppose at some instant A was being run and it entered its critical 440 | section. And there it acquired a resource X. Meanwhile another process B comes to the ready 441 | queue. B has more priority than A. Now the preemption occurs and scheduler decides to run B and it 442 | also wants t own resource X to complete some task. Now B won’t be able to take the resource as A 443 | is already having it but since it has higher priority it must run before A. So is this condition a 444 | deadlock? Explain with reasons. 445 | - What is starvation? Out of priority based round robin scheduling and priority based scheduling , which one is more likely 446 | to suffer from the problem of starvation? How to recover from starvation? 447 | - Static and dynamic memory allocation and followup questions on stack and heap. 448 |
449 |

Miscellaneous

450 | 451 | - Explain and give an example of a function pointer. Write function pointer for a function that takes an integer as a parameter and returns character. 452 | - Implement you own strcat() function. Dont use string header. 453 | - Difference between TCP and UDP. 454 | - What is Inline functions? 455 | - Make a utility function in C to detect memory leaks in any program given its source code,you are allowed to modify input program minimally for this purpose. 456 | - Balloon burst problem. 457 | - Write a program to allocate a 3D-array dynamically. 458 | - Write your own typedef operator. 459 | - What is meant by Early binding and late binding. 460 | - State various protocols in various layers of TCP/IP. 461 | - Create a pointer that can point to an array of integers. 462 | - Explain client server architecture. 463 | - What is L-value and R-value reference? 464 | - State various principles of OOPS. 465 | - Define MFC COM and DCOM. 466 | - Difference between NULL and NIL. 467 | - What is meant by Big and Small Endian? How would you know that Machine code is Big or Small Endian? 468 | - What is friend class and function in C++? 469 | - What is Virtual Function in C++? 470 | - Explain VTABLE and VPTR? 471 | - Difference between sizeOf(void) and sizeOf(void\*). 472 | - Full form of conio. 473 | - Describe Java's garbage collection. 474 | - Explain Abstract and pure Virtual function in detail. 475 | - What is the difference between a static and const variable? 476 | - Do namespaces interact? if yes, how? if no, why not? 477 | - Define Scheduling. Which data structure is used in scheduling? 478 | - Where are the local, global, static, auto, register, extern, const, volatile variables stored? 479 | - What is a virtual destructor? Explain the use of it - C++. 480 | - What is a void pointer, a smart pointer, a wild pointer, a null pointer, and a dangling pointer? In what cases are they used? 481 | - What is the difference between a deep copy and a shallow copy? 482 | - Explain Diamond problem in C++. 483 | - What is segmentation fault? 484 | - Implement 3 stacks using 1 array. 485 | - Explain MultiLevel inheritance. 486 | - Memory layout of C program. 487 | - What is structure padding ? 488 | - What are memset and memcopy? 489 | - What is function pointer? 490 | - There are some exceptions that cannot be caught by try catch. How to catch such exceptions? Can we prevent our program to crash if we are not able to catch such exceptions. 491 | - What is name mangling, and how does it work? 492 | - What does malloc(0) return? 493 | - There are 25 horses and 1 racing track. You can conduct a race of atmost 5 horses on that track. You 494 | need to find fastest 3 horses. What is the minimum number of races do you require? 495 | - You are given 8 batteries and 1 torch. 4 batteries are working and 4 are dead. The torch 496 | accommodates 2 batteries. If both the batteries are working then the torch will glow otherwise not. 497 | What is the minimum number of times you need to switch the torch to find out 2 working batteries? 498 | - How to prevent multiple object instantiations of a class ? 499 | - Explain the math behind gradient descent ? 500 | 501 | ## Feel free to show your love :heart: by putting a star :star: on this project :v: . 502 | --------------------------------------------------------------------------------