diff --git a/December 01/C_HariVignesh18_Cricmetrics.c b/December 01/C_HariVignesh18_Cricmetrics.c new file mode 100644 index 0000000..f841b37 --- /dev/null +++ b/December 01/C_HariVignesh18_Cricmetrics.c @@ -0,0 +1,20 @@ +#include +int main(){ + int n,s,a[100]; + int max; + int i; + scanf("%d",&n); + for(i = 0;i < n;i++){ + scanf("%d",&a[i]); + s = s + a[i]; + } + max = a[0]; + int ind = 0; + for(i = 1;imax){ + max = a[i]; + ind = i; + } + } + printf("%d\n%d",s,ind); +} diff --git a/December 02 /python3_HariVignesh18_Shopper's Choice.py b/December 02 /python3_HariVignesh18_Shopper's Choice.py new file mode 100644 index 0000000..a55fb96 --- /dev/null +++ b/December 02 /python3_HariVignesh18_Shopper's Choice.py @@ -0,0 +1,13 @@ +a = list(map(int,input().split())) +b = [] +for i in a: + if i not in b: + b.append(i) +c = [] +for i in b: + count = 0 + for j in a: + if i == j: + count += 1 + c.append(count) +print(c) diff --git a/December 02/.gitignore b/December 02/.gitignore deleted file mode 100644 index 8e0ca25..0000000 --- a/December 02/.gitignore +++ /dev/null @@ -1,231 +0,0 @@ -# gitignore file for "A December of Algorithms 2023" -# visit https://github.com/SVCE-ACM/A-December-of-Algorithms -# Written individually to adjust for the needs of the problem - -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# node.js -/node_modules -package-lock.json - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -pip-wheel-metadata/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -.python-version - -# pipenv -Pipfile.lock -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# Compiled class file -*.class - -# Log file -*.log - -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -# Files and directories created by pub -.dart_tool/ -.packages -build/ -# If you're building an application, you may want to check-in your pubspec.lock -pubspec.lock - -# Directory created by dartdoc -# If you don't generate documentation locally you can remove this line. -doc/api/ - -# Avoid committing generated Javascript files: -*.dart.js -*.info.json # Produced by the --dump-info flag. -*.js # When generated by dart2js. Don't specify *.js if your - # project includes source files written in JavaScript. -*.js_ -*.js.deps -*.js.map - -# Prerequisites -*.d - -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod -*.smod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex - -# Debug files -*.dSYM/ -*.su -*.idb -*.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf diff --git a/December 03/C_HariVignesh18_Sunburnt.c b/December 03/C_HariVignesh18_Sunburnt.c new file mode 100644 index 0000000..4471bd9 --- /dev/null +++ b/December 03/C_HariVignesh18_Sunburnt.c @@ -0,0 +1,23 @@ +#include +int main() +{ + int n,a[100]; + scanf("%d",&n); + for(int i = 0;i < n; i++){ + scanf("%d",&a[i]); + } + int b = 0; + for(int i = 0;i < n; i++){ + int s = 0; + for(int j = 0;j < i;j++){ + if(a[j]>=a[i]){ + s = 1; + break; + } + } + if(s == 0){ + b+=1; + } + } + printf("%d",b); +} diff --git a/December 04/python3_HariVignesh18_Mirror Magic.py b/December 04/python3_HariVignesh18_Mirror Magic.py new file mode 100644 index 0000000..d3a1aae --- /dev/null +++ b/December 04/python3_HariVignesh18_Mirror Magic.py @@ -0,0 +1,12 @@ +a = input() +b = "" +for i in range(0,len(a)-1): + for j in range(i+2,len(a)+1): + sub = a[i:j] + if sub == sub[::-1]: + if len(b)!= "" or len(b) > len(sub): + b = sub +if b!="": + print(b) +else: + print("Error") diff --git a/December 05/python3_HariVignesh18_PeakyBlinders.py b/December 05/python3_HariVignesh18_PeakyBlinders.py new file mode 100644 index 0000000..cb3055d --- /dev/null +++ b/December 05/python3_HariVignesh18_PeakyBlinders.py @@ -0,0 +1,12 @@ +a = [int(i) for i in input().split()] +c = 0 +s = 0 +for i in a: + s += i + c += 1 +b = s/c +s = 0 +for i in a: + if i >= b: + s += i +print(s) diff --git a/December 06/python3_HariVignesh18_TheLostAlgorithmScrolls.py b/December 06/python3_HariVignesh18_TheLostAlgorithmScrolls.py new file mode 100644 index 0000000..641aab4 --- /dev/null +++ b/December 06/python3_HariVignesh18_TheLostAlgorithmScrolls.py @@ -0,0 +1,18 @@ +s = input().split() +ans = [] +for i in range(0,len(s)-1): + if len(s[i]) == len(s[i+1]): + f = 0 + a = s[i] + b = s[i+1] + for i in range(0,len(s[i])): + if a[i] != b[i]: + f += 1 + if f == 1: + if a not in ans: + ans.append(a) + ans.append(b) +if ans != []: + print(ans) +else: + print("No Valid Chain.") diff --git a/December 07/python3_HariVignesh18_BabyBlocks.py b/December 07/python3_HariVignesh18_BabyBlocks.py new file mode 100644 index 0000000..e124d9f --- /dev/null +++ b/December 07/python3_HariVignesh18_BabyBlocks.py @@ -0,0 +1,13 @@ +def rectangleInCircle(width ,height ,radius): + diagSquare = (width**2 + height**2) + diagonal = diagSquare**0.5 + if diagonal > radius*2: + return False + else: + return True +a = [int(i) for i in input().split()] +ans = rectangleInCircle(a[0] , a[1], a[2]) +if ans == True: + print("True") +else: + print("False") diff --git a/December 08/python3_HariVignesh18_TheEnchantedForest.py b/December 08/python3_HariVignesh18_TheEnchantedForest.py new file mode 100644 index 0000000..6064d93 --- /dev/null +++ b/December 08/python3_HariVignesh18_TheEnchantedForest.py @@ -0,0 +1,23 @@ +def find_path(n): + ans = [[0]*n for _ in range(n)] + c = 0 + i = 0 + j = n//2 + for k in range(1,(n*n)+1): + ans [i][j] = k + i -= 1 + j += 1 + c += 1 + if c == n: + i += 2 + j -= 1 + c = 0 + else: + if j == n: + j -= n + elif i < 0: + i += n + for row in ans: + print(*row) +n = int(input()) +find_path(n) diff --git a/December 09/python3_HariVignesh18_Camels_In_A_String.py b/December 09/python3_HariVignesh18_Camels_In_A_String.py new file mode 100644 index 0000000..7113711 --- /dev/null +++ b/December 09/python3_HariVignesh18_Camels_In_A_String.py @@ -0,0 +1,6 @@ +s = input() +c = 1 +for i in s: + if ord(i)>64 and ord(i)<91: + c += 1 +print(c) diff --git a/December 10/python3_HariVignesh18_forgotPassword.py b/December 10/python3_HariVignesh18_forgotPassword.py new file mode 100644 index 0000000..dc0daa9 --- /dev/null +++ b/December 10/python3_HariVignesh18_forgotPassword.py @@ -0,0 +1,19 @@ +a = ['Shivansh Kumar','Ragul Gupta'] +query = input().split() +b = ['1','2','3','4','5','6','7','8','9','0'] +c = [0]*2 +j = 0 +cl = 0 +for i in query[1]: + if i in b: + if cl == 0: + c[j] += int(i) + else: + c[j] = c[j]*10 + int(i) + cl += 1 + if i not in b: + if cl > 0: + j += 1 + cl = 0 +for i in a: + print(i[c[0]-1:c[0]+c[1]-1]) diff --git a/December 11/python3_HariVignesh18_coderOfConversions.py b/December 11/python3_HariVignesh18_coderOfConversions.py new file mode 100644 index 0000000..e5dc65a --- /dev/null +++ b/December 11/python3_HariVignesh18_coderOfConversions.py @@ -0,0 +1,12 @@ +def decToBinary(n): + s = n[0] + n[1] + binaryNum = [0] * s; + i = 0; + while (s > 0): + binaryNum[i] = s % 2; + s = int(s / 2); + i += 1; + for j in range(i - 1, -1, -1): + print(binaryNum[j], end = ""); +n = [int(i) for i in input().split()] +decToBinary(n); diff --git a/December 12/python3_HariVignesh18_theHeist.py b/December 12/python3_HariVignesh18_theHeist.py new file mode 100644 index 0000000..1e6198b --- /dev/null +++ b/December 12/python3_HariVignesh18_theHeist.py @@ -0,0 +1,26 @@ +def find(box): + target = 'Gold' + l = 0 + r = len(box)-1 + while l<=r: + mid = (l+r)//2 + if box[mid] == target: + return True + elif box[mid] < target: + l = mid+1 + else: + r = mid-1 + return False + +box1 = input().split() +box2 = input().split() +box3 = input().split() +box1.sort() +box2.sort() +box3.sort() +if find(box1): + print("Box1 contains the Gold") +elif find(box2): + print("Box2 contains the Gold") +else: + print("Box3 contains the Gold") diff --git a/December 13/python3_HariVignesh18_CallCipher.py b/December 13/python3_HariVignesh18_CallCipher.py new file mode 100644 index 0000000..2cba944 --- /dev/null +++ b/December 13/python3_HariVignesh18_CallCipher.py @@ -0,0 +1,11 @@ +def textToNum(s): + my_dict = {"A": 2, "B": 2, "C": 2,"D": 3, "E": 3, "F": 3,"G": 4, "H": 4, "I": 4,"J": 5, "K": 5, "L": 5,"M": 6, "N": 6, "O": 6,"P": 7, "Q": 7, "R": 7, "S": 7,"T": 8, "U": 8, "V": 8,"W": 9, "X": 9, "Y": 9, "Z": 9} + ans = "" + for i in s: + if i in my_dict: + ans += str(my_dict[i]) + else: + ans += i + return ans +s = input() +print(textToNum(s)) diff --git a/December 14/c++_HariVignesh18_CallOfJustice.cpp b/December 14/c++_HariVignesh18_CallOfJustice.cpp new file mode 100644 index 0000000..333e611 --- /dev/null +++ b/December 14/c++_HariVignesh18_CallOfJustice.cpp @@ -0,0 +1,82 @@ +#include +#include + +using namespace std; + +class TreeNode { +public: + int value; + TreeNode* left; + TreeNode* right; + + TreeNode(int val) : value(val), left(nullptr), right(nullptr) {} +}; + +void findKDistanceNodes(TreeNode* root, int target, int k, vector& result) { + if (!root) + return; + + if (k == 0) { + result.push_back(root->value); + return; + } + + // Recur on the left subtree + findKDistanceNodes(root->left, target, k - 1, result); + + // Recur on the right subtree + findKDistanceNodes(root->right, target, k - 1, result); +} + +int findTarget(TreeNode* root, int target, int k, vector& result) { + if (!root) + return -1; + + if (root->value == target) { + findKDistanceNodes(root, target, k, result); + return 0; + } + + int leftDistance = findTarget(root->left, target, k, result); + if (leftDistance >= 0) { + if (leftDistance + 1 == k) + result.push_back(root->value); + + findKDistanceNodes(root->right, target, k - leftDistance - 2, result); + return leftDistance + 1; + } + + int rightDistance = findTarget(root->right, target, k, result); + if (rightDistance >= 0) { + if (rightDistance + 1 == k) + result.push_back(root->value); + + findKDistanceNodes(root->left, target, k - rightDistance - 2, result); + return rightDistance + 1; + } + + return -1; +} + +int main() { + TreeNode* root = new TreeNode(14); + root->left = new TreeNode(7); + root->right = new TreeNode(20); + root->right->right = new TreeNode(30); + root->left->left = new TreeNode(4); + root->left->right = new TreeNode(3); + root->left->right->left = new TreeNode(2); + root->left->right->left->left = new TreeNode(1); + root->left->right->left->right = new TreeNode(4); + + int targetNode = 7; + int kDistance = 3; + + vector output; + findTarget(root, targetNode, kDistance, output); + sort(output.begin(),output.end()); + for (int node : output) { + cout << " " << node; + } + return 0; +} diff --git a/December 15/python3_HariVignesh18_SubsequenceSorcery.py b/December 15/python3_HariVignesh18_SubsequenceSorcery.py new file mode 100644 index 0000000..d032ec8 --- /dev/null +++ b/December 15/python3_HariVignesh18_SubsequenceSorcery.py @@ -0,0 +1,9 @@ +from itertools import combinations +a = input() +ans = [] +for i in range(1,len(a)+1): + b = combinations(a,i) + for i in b: + if i not in ans: + ans.append(i) +print(len(ans)+1) diff --git a/December 16/python3_HariVignesh18_OutbreakDynamics.py b/December 16/python3_HariVignesh18_OutbreakDynamics.py new file mode 100644 index 0000000..f1b2e7f --- /dev/null +++ b/December 16/python3_HariVignesh18_OutbreakDynamics.py @@ -0,0 +1,35 @@ +from collections import deque +def min_infection_time(grid): + rows, cols = len(grid), len(grid[0]) + humans = 0 + zombies = deque() + for i in range(rows): + for j in range(cols): + if grid[i][j] == 1: + zombies.append((i, j)) + elif grid[i][j] == 0: + humans += 1 + time = 0 + directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] + while humans > 0 and zombies: + time += 1 + for _ in range(len(zombies)): + x, y = zombies.popleft() + for dx, dy in directions: + nx, ny = x + dx, y + dy + if 0 <= nx < rows and 0 <= ny < cols: + if grid[nx][ny] == 0: + humans -= 1 + grid[nx][ny] = 1 + zombies.append((nx, ny)) + + return time if humans == 0 else -1 +grid = [ + [1, 0, 1, 1, 0], + [0, 0, 0, 1, 1], + [1, 0, 1, 1, 1], + [1, 0, -1, 0, 0], + [1, 1, 0, 0, 1] +] +a = min_infection_time(grid) +print(a) diff --git a/December 17/c++_HariVignesh18_BookshelfDilemma.cpp b/December 17/c++_HariVignesh18_BookshelfDilemma.cpp new file mode 100644 index 0000000..43a7a79 --- /dev/null +++ b/December 17/c++_HariVignesh18_BookshelfDilemma.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + +struct Node { + int data; + struct Node* next; +}; + +void removeLoop(struct Node*, struct Node*); + +int detectAndRemoveLoop(struct Node* list) +{ + struct Node *slow_p = list, *fast_p = list; + + while (slow_p && fast_p && fast_p->next) { + slow_p = slow_p->next; + fast_p = fast_p->next->next; + + if (slow_p == fast_p) { + removeLoop(slow_p, list); + + return 1; + } + } + + return 0; +} + +void removeLoop(struct Node* loop_node, struct Node* head) +{ + struct Node* ptr1 = loop_node; + struct Node* ptr2 = loop_node; + + unsigned int k = 1, i; + while (ptr1->next != ptr2) { + ptr1 = ptr1->next; + k++; + } + + ptr1 = head; + + ptr2 = head; + for (i = 0; i < k; i++) + ptr2 = ptr2->next; + + while (ptr2 != ptr1) { + ptr1 = ptr1->next; + ptr2 = ptr2->next; + } + + while (ptr2->next != ptr1) + ptr2 = ptr2->next; + + ptr2->next = NULL; +} + +void printList(struct Node* node) +{ + + while (node != NULL) { + cout << node->data << " "; + node = node->next; + } +} + +struct Node* newNode(int key) +{ + struct Node* temp = new Node(); + temp->data = key; + temp->next = NULL; + return temp; +} + +int main() +{ + struct Node* head = newNode(1); + head->next = newNode(2); + head->next->next = newNode(3); + head->next->next->next = newNode(4); + head->next->next->next->next = newNode(5); + + head->next->next->next->next->next = head->next; + + detectAndRemoveLoop(head); + + printList(head); + return 0; +} diff --git a/December 18/python3_HariVignesh18_ It'sChristmasSeason,py b/December 18/python3_HariVignesh18_ It'sChristmasSeason,py new file mode 100644 index 0000000..a1c28db --- /dev/null +++ b/December 18/python3_HariVignesh18_ It'sChristmasSeason,py @@ -0,0 +1,87 @@ +import sys +sys.setrecursionlimit(10 ** 6) +N = 100000 +l=[] +MOD = 10 ** 9 + 7 +f = [0] * (N + 1) +invf = [0] * (N + 1) + +def dfs(node, p, g, a, x): + for c in g[node]: + if c != p: + dfs(c, node, g, a, x) + a[node] += a[c] + if a[node] >= x: + a[node] -= x + +def powmod(a, b, mod): + res = 1 + a %= mod + while b: + if b & 1: + res = res * a % mod + a = a * a % mod + b >>= 1 + return res + +def cn(u, v, MOD): + if u > v: + return 0 + if u == v: + return 1 + if u == 1 or u + 1 == v: + return v + return f[v] * invf[u] % MOD * invf[v - u] % MOD + +def dfs_count(node, p, g, a): + cnt = 1 if a[node] == 0 else 0 + for c in g[node]: + if c != p: + cnt += dfs_count(c, node, g, a) + return cnt + +def solve(): + global N, MOD, f, invf + n, x = map(int, input().split()) + a = list(map(int, input().split())) + a = [ai % x for ai in a] + g = [[] for _ in range(n)] + for _ in range(n - 1): + u, v = map(int, input().split()) + u -= 1 + v -= 1 + g[u].append(v) + g[v].append(u) + + dfs(0, -1, g, a, x) + + if a[0] != 0: + print(' '.join(['0'] * n)) + return + + result = [1] + cnt = dfs_count(0, -1, g, a) + + for i in range(2, n + 1): + result.append(cn(i - 1, cnt - 1, MOD)) + + l.append(list(''.join(map(str,result)))) + +f[0] = 1 + +for i in range(1, N + 1): + f[i] = f[i - 1] * i % MOD + +invf[N] = powmod(f[N], MOD - 2, MOD) + +for i in range(N, 0, -1): + invf[i - 1] = invf[i] * i % MOD + +t = int(input()) + +for _ in range(t): + solve() +for i in l: + for j in i: + print(int(j),end=" ") + print() diff --git a/December 19/C++_HariVignesh18_SymbolicSum.cpp b/December 19/C++_HariVignesh18_SymbolicSum.cpp new file mode 100644 index 0000000..d2e6a0d --- /dev/null +++ b/December 19/C++_HariVignesh18_SymbolicSum.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +using namespace std; + +int symbolicSum(const string& sequence) { + stack> st; + + int mul = 1; + long long sum = 0; + + for (char ch : sequence) { + if (isdigit(ch)) { + sum += ch - '0'; + } else if (ch == 'X') { + st.push({sum, mul}); + sum = 0; + mul = 1; + } else if (ch == ' ') { + continue; + } else { + mul = ch - '0'; + } + } + + long long symbolicSum = 0; + + while (!st.empty()) { + auto top = st.top(); + st.pop(); + + symbolicSum += top.first * top.second; + } + + return symbolicSum; +} + +int main() { + string s_seq = "X5 2 X3 7 X2 5 6"; + + int r = symbolicSum(s_seq); + cout << "Symbolic Sum: " << r << endl; + + return 0; +} diff --git a/December 20/python3_HariVignesh_TreasureHuntInTheIsles,py b/December 20/python3_HariVignesh_TreasureHuntInTheIsles,py new file mode 100644 index 0000000..116fb91 --- /dev/null +++ b/December 20/python3_HariVignesh_TreasureHuntInTheIsles,py @@ -0,0 +1,22 @@ +import heapq +def hunt(g, s, e): + q = [(0, s, [s])] + while q: + tw, cc, pf = heapq.heappop(q) + if cc == e: + return pf + for n, w in g.get(cc, {}).items(): + tnw = tw + w + heapq.heappush(q, (tnw, n, pf + [n])) + return [] + +graph = { + 'Cave_A': {'Cave_B': 3, 'Cave_C': 5}, + 'Cave_B': {'Cave_D': 7, 'Cave_E': 1}, + 'Cave_C': {'Cave_D': 3}, + 'Cave_D': {'Cave_E': 5}, + 'Cave_E': {} +} +start_cave2 = 'Cave_A' +end_cave2 = 'Cave_E' +print(hunt(graph, start_cave2, end_cave2)) diff --git a/December 21/python3_HariVignesh18_RiddleMeThis.py b/December 21/python3_HariVignesh18_RiddleMeThis.py new file mode 100644 index 0000000..021d0ea --- /dev/null +++ b/December 21/python3_HariVignesh18_RiddleMeThis.py @@ -0,0 +1,9 @@ +s = "RQHODQTLATCTQ" +ans = "" +for i in s: + a = ord(i) + a = a-25 + if a < 65: + a +=26 + ans += chr(a) +print(ans) diff --git a/December 22/python3_HariVignesh18_RottenOranges.py b/December 22/python3_HariVignesh18_RottenOranges.py new file mode 100644 index 0000000..a485e72 --- /dev/null +++ b/December 22/python3_HariVignesh18_RottenOranges.py @@ -0,0 +1,32 @@ +from collections import deque +def rottime(grid): + if not grid or not grid[0]: + return 0 + rows, cols = len(grid), len(grid[0]) + f, q = 0, deque() + for i in range(rows): + for j in range(cols): + if grid[i][j] == 1: + f+=1 + elif grid[i][j] == 2: + q.append((i, j, 0)) + if f==0: + return 0 + + d=[(-1, 0), (1, 0), (0, -1), (0, 1)] + t=0 + + while q: + i,j,t=q.popleft() + + for di,dj in d: + ni,nj=i+di,j+dj + + if 0 <= ni < rows and 0 <= nj < cols and grid[ni][nj] == 1: + grid[ni][nj] = 2 + f -= 1 + q.append((ni, nj, t + 1)) + + return t if f == 0 else -1 +grid=[[2, 2, 0, 1]] +print(rottime(grid)) diff --git a/December 23/python3_HariVignesh18_Dominoes.py b/December 23/python3_HariVignesh18_Dominoes.py new file mode 100644 index 0000000..270ccc4 --- /dev/null +++ b/December 23/python3_HariVignesh18_Dominoes.py @@ -0,0 +1,32 @@ +def min_rotation_time(n, dominos): + upper_sum = 0 + lower_sum = 0 + rotation_count = 0 + + for domino in dominos: + upper_sum += domino[0] + lower_sum += domino[1] + + if upper_sum % 2 == 0 and lower_sum % 2 == 0: + return 0 + + if upper_sum % 2 != lower_sum % 2: + return -1 + + for i in range(n): + if dominos[i][0] % 2 != dominos[i][1] % 2: + if upper_sum % 2 == 1 and dominos[i][0] % 2 == 1: + rotation_count += 1 + upper_sum -= 1 + lower_sum += 1 + elif lower_sum % 2 == 1 and dominos[i][1] % 2 == 1: + rotation_count += 1 + upper_sum += 1 + lower_sum -= 1 + + return rotation_count + +n = 3 +dominos = [(4,2),(6,4)] +result = min_rotation_time(n, dominos) +print(result) diff --git a/December 24/python3_HariVignesh18_GoldenRuleViolation b/December 24/python3_HariVignesh18_GoldenRuleViolation new file mode 100644 index 0000000..5c236a9 --- /dev/null +++ b/December 24/python3_HariVignesh18_GoldenRuleViolation @@ -0,0 +1,9 @@ +a = [int(i) for i in input().split()] +b = a.copy() +count = 0 +for i in range(0,len(a)): + c = b[:i+1] + for j in c: + if j > a[i]: + count += 1 +print(count) diff --git a/December 25/python3_HariVignesh18_HarmonyHurdle.py b/December 25/python3_HariVignesh18_HarmonyHurdle.py new file mode 100644 index 0000000..2e5b0b6 --- /dev/null +++ b/December 25/python3_HariVignesh18_HarmonyHurdle.py @@ -0,0 +1,38 @@ +from collections import defaultdict, deque + +def min_time_to_complete_tasks(tasks, dependencies): + graph = defaultdict(list) + in_degree = {task: 0 for task in tasks} + + for i, deps in enumerate(dependencies): + for dep in deps: + graph[dep].append(i + 1) + in_degree[i + 1] += 1 + + top_order = [] + queue = deque([task for task, degree in in_degree.items() if degree == 0]) + + while queue: + current_task = queue.popleft() + top_order.append(current_task) + + for neighbor in graph[current_task]: + in_degree[neighbor] -= 1 + if in_degree[neighbor] == 0: + queue.append(neighbor) + + min_time = 0 + time_to_complete = {task: 0 for task in tasks} + + for task in top_order: + for neighbor in graph[task]: + time_to_complete[neighbor] = max(time_to_complete[neighbor], time_to_complete[task] + 1) + min_time = max(min_time, time_to_complete[neighbor]) + + return min_time + +tasks = [1, 2, 3, 4, 5] +dependencies = [[], [1], [2], [3], [4, 1]] + +result = min_time_to_complete_tasks(tasks, dependencies) +print(result) diff --git a/December 26/C++_HariVignesh18_PhantomCycle.cpp b/December 26/C++_HariVignesh18_PhantomCycle.cpp new file mode 100644 index 0000000..b2fd282 --- /dev/null +++ b/December 26/C++_HariVignesh18_PhantomCycle.cpp @@ -0,0 +1,60 @@ +#include +#include +using namespace std; + +struct node +{ + int val; + struct node *next; +}; +struct node *head = NULL; + +bool detectLoop(struct node* head) +{ + struct node* temp = head; + map v; + while(temp!=NULL){ + if(v[temp]!=true){ + v[temp] = true; + temp = temp->next; + } + else{ + return true; + } + } + return false; +} + +void insert(int val) +{ + struct node *nn = new struct node; + nn->val = val; + nn->next = NULL; + if (head == NULL) { + head = nn; + } else { + struct node *t = head; + while (t->next != NULL) { + t = t->next; + } + t->next = nn; + } +} + +int main() { + int n, e; + cin >> n; + for (int i = 0; i < n; i++) { + cin >> e; + insert(e); + } + // Adding a Loop + head->next->next->next = head; + if (detectLoop(head)) { + cout << "Cycle Found"; + } else { + cout << "No Cycle Found"; + } + + return 0; +} diff --git a/December 27/python3_HarVignesh_CircleOfEndurance.py b/December 27/python3_HarVignesh_CircleOfEndurance.py new file mode 100644 index 0000000..2c60f6e --- /dev/null +++ b/December 27/python3_HarVignesh_CircleOfEndurance.py @@ -0,0 +1,25 @@ +def find_starting_point(N, petrol, distance): + start = 0 + petrol_in_tank = 0 + total_petrol = 0 + total_distance = 0 + + for i in range(N): + petrol_in_tank = petrol[i] - distance[i] + total_petrol += petrol[i] + total_distance += distance[i] + + if petrol_in_tank < 0: + start_point += 1 + petrol_in_tank = 0 + return start + +N1 = 4 +petrol1 = [4, 6, 7, 4] +distance1 = [6, 5, 3, 5] +print(find_starting_point(N1, petrol1, distance1)) + +N2 = 5 +petrol2 = [2, 3, 7, 4, 5] +distance2 = [6, 5, 3, 5, 3] +print(find_starting_point(N2, petrol2, distance2)) diff --git a/December 28/python3_HariVignesh8_TheSellingGame.py b/December 28/python3_HariVignesh8_TheSellingGame.py new file mode 100644 index 0000000..c591e0b --- /dev/null +++ b/December 28/python3_HariVignesh8_TheSellingGame.py @@ -0,0 +1,33 @@ +def max_gadgets_sold(x, z, items, clients): + items.sort(key=lambda item: (item['m'], -item['n']), reverse=True) + clients.sort(key=lambda client: client['k']) + + gadgets_sold = 0 + sold_items = set() + + for client in clients: + for i, item in enumerate(items): + if i not in sold_items and item['m'] > client['k'] and item['n'] <= client['r']: + gadgets_sold += 1 + sold_items.add(i) + break + + return gadgets_sold + +# Example usage: +x = 3 +z = 3 +items = [ + {'k': 10, 'r': 100, 'm': 5, 'n': 110}, # Item 0 + {'k': 9, 'r': 200, 'm': 2, 'n': 200}, # Item 1 + {'k': 20, 'r': 300, 'm': 30, 'n': 300} # Item 2 +] + +clients = [ + {'k': 5, 'r': 110}, # Client 0 + {'k': 9, 'r': 500}, # Client 1 + {'k': 20, 'r': 400}, # Client 2 +] + +result = max_gadgets_sold(x, z, items, clients) +print(result) diff --git a/December 29/python3_HariVignesh18_CartesianWalkValidator.py b/December 29/python3_HariVignesh18_CartesianWalkValidator.py new file mode 100644 index 0000000..ad028d8 --- /dev/null +++ b/December 29/python3_HariVignesh18_CartesianWalkValidator.py @@ -0,0 +1,23 @@ +def find_path(path): + b = [0, 0] + c = 0 + for i in path: + c += 1 + if i == 'n': + b = [b[0], b[1] + 1] + elif i == 'e': + b = [b[0] + 1, b[1]] + elif i == 's': + b = [b[0], b[1] - 1] + else: + b = [b[0] - 1, b[1]] + if c == 10: + if b == [0,0]: + print("True") + else: + print("False") + else: + print("False") + +path = input().split() +find_path(path) diff --git a/December 30/python3_HariVignesh18_TreeInversions.py b/December 30/python3_HariVignesh18_TreeInversions.py new file mode 100644 index 0000000..b120c6a --- /dev/null +++ b/December 30/python3_HariVignesh18_TreeInversions.py @@ -0,0 +1,43 @@ +def dfs(node, parent, colors, tree, in_count, out_count, depth): + in_count[node] = [0] * depth + out_count[node] = [0] * depth + in_count[node][colors[node] - 1] = 1 + + for child in tree[node]: + if child == parent: + continue + dfs(child, node, colors, tree, in_count, out_count, depth) + for i in range(depth): + in_count[node][i] += in_count[child][i] + out_count[node][i] += out_count[child][i] + out_count[node][colors[node] - 1] += 1 + +def solve_queries(tree, in_count, out_count, depth, queries): + result = [] + for query in queries: + x, y = query + x -= 1 + y -= 1 + total_inversions = 0 + for i in range(depth): + total_inversions += abs(in_count[x][i] - in_count[y][i]) + abs(out_count[x][i] - out_count[y][i]) + result.append(total_inversions // 2) + return result + +T = int(input()) +for _ in range(T): + N, Q = map(int, input().split()) + colors = list(map(int, input().split())) + tree = {i: [] for i in range(N)} + for _ in range(N - 1): + X, Y = map(int, input().split()) + tree[X - 1].append(Y - 1) + tree[Y - 1].append(X - 1) + in_count = {} + out_count = {} + dfs(0, -1, colors, tree, in_count, out_count, max(colors)) + queries = [tuple(map(int, input().split())) for _ in range(Q)] + result = solve_queries(tree, in_count, out_count, max(colors), queries) + + for res in result: + print(res) diff --git a/December 31/python3_HariVignesh18_NQueens.py b/December 31/python3_HariVignesh18_NQueens.py new file mode 100644 index 0000000..88f3061 --- /dev/null +++ b/December 31/python3_HariVignesh18_NQueens.py @@ -0,0 +1,25 @@ +def is_safe(board, row, col, N): + for i in range(row): + if board[i] == col or board[i] - i == col - row or board[i] + i == col + row: + return False + return True + +def solve_nqueens(board, row, N, solutions): + if row == N: + solutions.append(tuple(board[:])) + else: + for col in range(N): + if is_safe(board, row, col, N): + board[row] = col + solve_nqueens(board, row + 1, N, solutions) + +def print_solutions(N): + board = [-1] * N + solutions = [] + solve_nqueens(board, 0, N, solutions) + + for solution in solutions: + print(" ".join(f"({i + 1}, {solution[i] + 1})" for i in range(N))) + +N = int(input()) +print_solutions(N)