diff --git a/December 01/C++_Jeswindany_Cricmetric.cpp b/December 01/C++_Jeswindany_Cricmetric.cpp new file mode 100644 index 0000000..79da1fd --- /dev/null +++ b/December 01/C++_Jeswindany_Cricmetric.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main(){ + int n; + cin >> n; + int runs[n], total = 0, highest = 0; + for (int i = 0; i> runs[i]; + total += runs[i]; + if(runs[i]>runs[highest]){ + highest = i; + } + } + cout << total << endl; + cout << highest << endl; +} diff --git a/December 02/python3_Jeswindany_Shoppers_Choice.py b/December 02/python3_Jeswindany_Shoppers_Choice.py new file mode 100644 index 0000000..f0766fb --- /dev/null +++ b/December 02/python3_Jeswindany_Shoppers_Choice.py @@ -0,0 +1,8 @@ +sales = [int(i) for i in input().split()] +visited = [] +frequency = [] +for i in sales: + if i not in visited: + visited.append(i) + frequency.append(sales.count(i)) +print(frequency) diff --git a/December 03/C++_Jeswindany_Sunburnt.cpp b/December 03/C++_Jeswindany_Sunburnt.cpp new file mode 100644 index 0000000..36a3529 --- /dev/null +++ b/December 03/C++_Jeswindany_Sunburnt.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; +int main(){ + int n; + cin >> n; + int h[n]; + for(int i=0; i> h[i]; + } + int c = 0, f; + for(int i=0; i= h[i]){ + f = 0; + break; + } + } + if(f == 1) + c++; + } + cout << c; +} diff --git a/December 04/python3_Jeswindany_MirrorMagic.py b/December 04/python3_Jeswindany_MirrorMagic.py new file mode 100644 index 0000000..61339b3 --- /dev/null +++ b/December 04/python3_Jeswindany_MirrorMagic.py @@ -0,0 +1,22 @@ +def substrings(s): + substrings = [] + n = len(s) + + for i in range(n): + for j in range(i + 1, n + 1): + substrings.append(s[i:j]) + return substrings + +def palindrome(s): + return s == s[::-1] + +word = input() +sub = substrings(word) +sub.sort(key = str.__len__) +for i in range(len(word), len(sub)): + if(palindrome(sub[i].casefold())): + res = sub[i] + break +else: + res = "Error" +print(res) diff --git a/December 05/python3_Jeswindany_PeakyBlinders.py b/December 05/python3_Jeswindany_PeakyBlinders.py new file mode 100644 index 0000000..8f022cb --- /dev/null +++ b/December 05/python3_Jeswindany_PeakyBlinders.py @@ -0,0 +1,7 @@ +loot = [int(i) for i in input().split()] +avg = sum(loot)/len(loot) +res_sum = 0 +for i in loot: + if(i>=avg): + res_sum += i +print(res_sum) diff --git a/December 06/python3_Jeswindany_TheLostAlgorithmScrolls.py b/December 06/python3_Jeswindany_TheLostAlgorithmScrolls.py new file mode 100644 index 0000000..fc0904d --- /dev/null +++ b/December 06/python3_Jeswindany_TheLostAlgorithmScrolls.py @@ -0,0 +1,36 @@ +def link(a, b): + if(len(a) == len(b)): + c = 0 + for i in range(len(a)): + if(a[i] != b[i]): + c+=1 + if(c == 1): + return True + elif(abs(len(a) - len(b)) == 1): + if((a in b) or (b in a)): + return True + return False + +words = input().split() +start = "" +res = [] +for i in words: + for j in words: + if(i!=j and link(i,j)): + start = i + res.append(i) + break + if(start != ""): + break +if start == "": + print("No valid chain") +else: + while(1): + for i in words: + if(i!=start and link(start,i) and (i not in res)): + start = i + res.append(i) + break + else: + break + print(res) diff --git a/December 07/C++_Jeswindany_BabyBlocks.cpp b/December 07/C++_Jeswindany_BabyBlocks.cpp new file mode 100644 index 0000000..4fc301c --- /dev/null +++ b/December 07/C++_Jeswindany_BabyBlocks.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +void rectangleInCircle(double w,double h,double r){ + double diag = sqrt((w*w)+(h*h)); + if(diag>(2*r)) + cout << "false" << endl; + else + cout << "true" << endl; +} + +int main(){ + double w, h, r; + cin >> w >> h >> r; + rectangleInCircle(w,h,r); + return 0; +} diff --git a/December 08/C++_Jeswindany_TheEnchantedForest.cpp b/December 08/C++_Jeswindany_TheEnchantedForest.cpp new file mode 100644 index 0000000..753131e --- /dev/null +++ b/December 08/C++_Jeswindany_TheEnchantedForest.cpp @@ -0,0 +1,48 @@ +#include +#include +using namespace std; + + +void find_path(int n){ + int sqr[n][n]; + memset(sqr, 0, sizeof(sqr)); + int i = n/2; + int j = n-1; + + for(int num = 1; num <= n*n;){ + if(i == -1 && j == n){ + i = 0; + j = n-2; + } + else{ + if(j == n) + j = 0; + if(i < 0) + i = n-1; + } + if(sqr[i][j]){ + i += 1; + j -= 2; + continue; + } + else{ + sqr[i][j] = num++; + } + i--; + j++; + } + + for (i = 0; i> n; + find_path(n); + return 0; +} diff --git a/December 09/C++_Jeswindany_camelsOnAString.cpp b/December 09/C++_Jeswindany_camelsOnAString.cpp new file mode 100644 index 0000000..76ba373 --- /dev/null +++ b/December 09/C++_Jeswindany_camelsOnAString.cpp @@ -0,0 +1,16 @@ +#include +#include +using namespace std; + +int main(){ + string text; + cin >> text; + int c = 1; + for(int i = 0; i=65 && text[i] <= 90){ + c += 1; + } + } + cout << c; + return 0; +} diff --git a/December 10/python3_Jeswindany_ForgotPassword.py b/December 10/python3_Jeswindany_ForgotPassword.py new file mode 100644 index 0000000..7f30bb5 --- /dev/null +++ b/December 10/python3_Jeswindany_ForgotPassword.py @@ -0,0 +1,12 @@ +import re + +def substring(name, a, b): + return name[a-1:a+b-1] + +data = [[100, "Shivnash Kumar"],[110,"Ragul Gupta"]] + +ind = re.findall('\d+',input()) +a,b= [int(i) for i in ind] + +for i,j in data: + print(substring(j,a,b)) diff --git a/December 11/python3_Jeswindany_CoderOfConversions.py b/December 11/python3_Jeswindany_CoderOfConversions.py new file mode 100644 index 0000000..f533f8a --- /dev/null +++ b/December 11/python3_Jeswindany_CoderOfConversions.py @@ -0,0 +1,5 @@ +def binsum(a,b): + return bin(a+b)[2:] + +a,b = [int(i) for i in input().split()] +print(binsum(a,b)) \ No newline at end of file diff --git a/December 12/python3_Jeswindany_TheHeist.py b/December 12/python3_Jeswindany_TheHeist.py new file mode 100644 index 0000000..d5fab2d --- /dev/null +++ b/December 12/python3_Jeswindany_TheHeist.py @@ -0,0 +1,25 @@ +def binarySearch(box): + l = 0 + h = len(box) - 1 + while(l <= h): + m = (l+h)//2 + if(box[m].lower() == "gold"): + return 1 + elif(box[m].lower() < "gold"): + l = m+1 + elif(box[m].lower() > "gold"): + h = m-1 + return 0 + +box1 = sorted([i.lower() for i in input().split()]) +box2 = sorted([i.lower() for i in input().split()]) +box3 = sorted([i.lower() for i in input().split()]) + +if(binarySearch(box1)): + print("Box1 Contains the Gold") +elif(binarySearch(box2)): + print("Box2 Contains the Gold") +elif(binarySearch(box3)): + print("Box3 Contains the Gold") +else: + print("Gold not Found") \ No newline at end of file diff --git a/December 13/C++_Jeswindany_CallCipher.cpp b/December 13/C++_Jeswindany_CallCipher.cpp new file mode 100644 index 0000000..32766a9 --- /dev/null +++ b/December 13/C++_Jeswindany_CallCipher.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + +int textToNum(char c){ + switch(c){ + case 65: + case 66: + case 67: + return 2; + case 68: + case 69: + case 70: + return 3; + case 71: + case 72: + case 73: + return 4; + case 74: + case 75: + case 76: + return 5; + case 77: + case 78: + case 79: + return 6; + case 80: + case 81: + case 82: + case 83: + return 7; + case 84: + case 85: + case 86: + return 8; + case 87: + case 88: + case 89: + case 90: + return 9; + } +} + +int main(){ + string S; + cin >> S; + for (char i: S){ + if(i>=65 && i<=90) + cout << textToNum(i); + else if(i>=97 && i<=122) + cout << textToNum(toupper(i)); + else + cout << i; + } + cout << endl; + return 0; +} diff --git a/December 14/C++_Jeswindany_CallOfJustice.cpp b/December 14/C++_Jeswindany_CallOfJustice.cpp new file mode 100644 index 0000000..d4df719 --- /dev/null +++ b/December 14/C++_Jeswindany_CallOfJustice.cpp @@ -0,0 +1,74 @@ +#include +#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; + } + + findKDistanceNodes(root->left, target, k - 1, result); + 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(17); + root->left = new TreeNode(8); + root->right = new TreeNode(27); + root->left->left = new TreeNode(4); + root->left->right = new TreeNode(14); + root->left->right->left = new TreeNode(10); + root->left->right->right = new TreeNode(16); + int targetNode = 8; + int kDistance = 2; + vector output; + findTarget(root, targetNode, kDistance, output); + sort(output.begin(),output.end()); + for (int node : output) { + cout << node << " "; + } + cout << endl; + return 0; +} diff --git a/December 15/python3_Jeswindany_SubsequenceSorcery.py b/December 15/python3_Jeswindany_SubsequenceSorcery.py new file mode 100644 index 0000000..7169bc2 --- /dev/null +++ b/December 15/python3_Jeswindany_SubsequenceSorcery.py @@ -0,0 +1,13 @@ +def subsequence(s, sub, substr = ""): + if(len(s) == 0): + sub.append(substr) + return + subsequence(s[:-1], sub, substr + s[-1]) + subsequence(s[:-1], sub, substr) + return + +s = input() +sub = [] +subsequence(s, sub) + +print(len(set(sub))) diff --git a/December 16/Python3_Jeswindany_OutbreakDynamics.py b/December 16/Python3_Jeswindany_OutbreakDynamics.py new file mode 100644 index 0000000..052b672 --- /dev/null +++ b/December 16/Python3_Jeswindany_OutbreakDynamics.py @@ -0,0 +1,37 @@ +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) \ No newline at end of file diff --git a/December 17/C++_JeswinDany_BookshelfDilemma.cpp b/December 17/C++_JeswinDany_BookshelfDilemma.cpp new file mode 100644 index 0000000..9515b45 --- /dev/null +++ b/December 17/C++_JeswinDany_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/C++_Jeswindany_ItsChristmasSeason.cpp b/December 18/C++_Jeswindany_ItsChristmasSeason.cpp new file mode 100644 index 0000000..1d6f079 --- /dev/null +++ b/December 18/C++_Jeswindany_ItsChristmasSeason.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +using namespace std; + +const int MOD = 1e9 + 7; + +struct Node { + int value; + vector children; +}; + +void dfs(int node, int currSum, int X, vector& tree, vector>& dp) { + currSum = (currSum + tree[node].value) % X; + dp[node][currSum]++; + + for (int child : tree[node].children) { + dfs(child, currSum, X, tree, dp); + + for (auto it = dp[child].begin(); it != dp[child].end(); ++it) { + int remainder = (it->first + currSum) % X; + dp[node][remainder] = (dp[node][remainder] + it->second) % MOD; + } + } +} + +vector countGoodCuttings(int N, int X, vector& values, vector>& edges) { + vector tree(N); + vector> dp(N); + + for (int i = 0; i < N; ++i) { + tree[i].value = values[i]; + } + + for (const auto& edge : edges) { + int u = edge[0], v = edge[1]; + tree[u].children.push_back(v); + } + + dfs(0, 0, X, tree, dp); + vector result(N); + for (int i = 0; i < N; ++i) { + result[i] = dp[0][i % X]; + } + + return result; +} + +int main() { + int N, X; + cin >> N >> X; + + vector values(N); + for (int i = 0; i < N; ++i) { + cin >> values[i]; + } + + vector> edges(N - 1, vector(2)); + for (int i = 0; i < N - 1; ++i) { + cin >> edges[i][0] >> edges[i][1]; + } + + vector counts = countGoodCuttings(N, X, values, edges); + + for (int i = 0; i < N; ++i) { + cout << counts[i] << " "; + } + + return 0; +} \ No newline at end of file diff --git a/December 19/C++_Jeswindany_SymbolicSum.cpp b/December 19/C++_Jeswindany_SymbolicSum.cpp new file mode 100644 index 0000000..c09f445 --- /dev/null +++ b/December 19/C++_Jeswindany_SymbolicSum.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +using namespace std; + +int symbolicSum(const string& sequence) { + stack> stk; + + int multiplier = 1; + long long sum = 0; + + for (char ch : sequence) { + if (isdigit(ch)) { + sum += ch - '0'; + } else if (ch == 'X') { + stk.push({sum, multiplier}); + sum = 0; + multiplier = 1; + } else if (ch == ' ') { + continue; + } else { + multiplier = ch - '0'; + } + } + + long long symbolicSum = 0; + + while (!stk.empty()) { + auto top = stk.top(); + stk.pop(); + + symbolicSum += top.first * top.second; + } + + return symbolicSum; +} + +int main() { + string sequence = "1 X 2 X 3 X2"; + + int result = symbolicSum(sequence); + cout << result << endl; + + return 0; +} \ No newline at end of file diff --git a/December 20/Python_Jeswindany_TreasureHuntInTheIsles.py b/December 20/Python_Jeswindany_TreasureHuntInTheIsles.py new file mode 100644 index 0000000..8ee28a1 --- /dev/null +++ b/December 20/Python_Jeswindany_TreasureHuntInTheIsles.py @@ -0,0 +1,22 @@ +import heapq +def hunt(graph, start, end): + l = [(0, start, [start])] + while l: + a, b, c = heapq.heappop(l) + if b == end: + return c + for i, j in graph.get(b, {}).items(): + t = a + j + heapq.heappush(l, (t, i, c + [i])) + 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_cave = 'Cave_A' +end_cave = 'Cave_E' +print(hunt(graph, start_cave, end_cave)) \ No newline at end of file diff --git a/December 21/C++_Jeswindany_RiddleMeThis.c++ b/December 21/C++_Jeswindany_RiddleMeThis.c++ new file mode 100644 index 0000000..e2addc3 --- /dev/null +++ b/December 21/C++_Jeswindany_RiddleMeThis.c++ @@ -0,0 +1,21 @@ +#include +using namespace std; + +#include + +int main(){ + string S; + cout << "Code: "; + cin >> S; + cout << "Shift: "; + int shift; + cin >> shift; + for (int i=0; S[i]!='\0'; i++){ + char tmp = S[i]-(shift%26); + if(tmp < 65) + tmp = 90-(64-tmp); + cout << tmp; + } + cout << endl; + return 0; +} \ No newline at end of file diff --git a/December 22/C++_Jeswindany_RottenOranges.cpp b/December 22/C++_Jeswindany_RottenOranges.cpp new file mode 100644 index 0000000..c7640b1 --- /dev/null +++ b/December 22/C++_Jeswindany_RottenOranges.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace std; + +int orangesRotting(vector>& grid) { + int rows = grid.size(); + int cols = grid[0].size(); + + queue> rotten; + int freshOranges = 0; + int time = 0; + + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + if (grid[i][j] == 1) { + ++freshOranges; + } else if (grid[i][j] == 2) { + rotten.push({i, j}); + } + } + } + + vector> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + + while (!rotten.empty() && freshOranges > 0) { + int size = rotten.size(); + for (int i = 0; i < size; ++i) { + int x = rotten.front().first; + int y = rotten.front().second; + rotten.pop(); + + for (const auto& dir : directions) { + int newX = x + dir.first; + int newY = y + dir.second; + + if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && + grid[newX][newY] == 1) { + grid[newX][newY] = 2; + rotten.push({newX, newY}); + --freshOranges; + } + } + } + if (!rotten.empty()) { + ++time; + } + } + + return (freshOranges == 0) ? time : -1; +} + +int main() { + int n, m; + cin >> n >> m; + + vector> grid(n, vector(m)); + + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + cin >> grid[i][j]; + } + } + + cout << orangesRotting(grid); + return 0; +} \ No newline at end of file diff --git a/December 23/Python_Jeswindany_Dominoes.py b/December 23/Python_Jeswindany_Dominoes.py new file mode 100644 index 0000000..2a398c5 --- /dev/null +++ b/December 23/Python_Jeswindany_Dominoes.py @@ -0,0 +1,35 @@ +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 = int(input()) +dominos = [] +for i in range(n): + dominos.append(tuple(int(i) for i in input().split())) + +result = min_rotation_time(n, dominos) +print(result) \ No newline at end of file diff --git a/December 24/C++_Jeswindany_GoldenRuleViolation.c++ b/December 24/C++_Jeswindany_GoldenRuleViolation.c++ new file mode 100644 index 0000000..518c0b9 --- /dev/null +++ b/December 24/C++_Jeswindany_GoldenRuleViolation.c++ @@ -0,0 +1,32 @@ +#include +using namespace std; + +void swap(int &a, int &b){ + a = a+b; + b = a-b; + a = a-b; +} + +int bubbleSort(int arr[], int n){ + int c = 0; + for (int i = 0; i arr[j+1]){ + swap(arr[j],arr[j+1]); + c++; + } + } + } + return c; +} + +int main(){ + int n; + cin >> n; + int arr[n]; + for (int i = 0; i> arr[i]; + int count = bubbleSort(arr, n); + cout << count << endl; + return 0; +} diff --git a/December 25/Python_Jeswindany_HarmonyHurdle.py b/December 25/Python_Jeswindany_HarmonyHurdle.py new file mode 100644 index 0000000..e878fff --- /dev/null +++ b/December 25/Python_Jeswindany_HarmonyHurdle.py @@ -0,0 +1,23 @@ +tasks = [1, 2, 3, 4, 5] +dependancies = [[], [1], [2], [3], [4,1]] +d = {} +c = 0 +t = 0 + +for i in range(len(tasks)): + d[tasks[i]] = dependancies[i] + +while(d): + t += 1 + l = [] + for i in d: + if d[i] == []: + l.append(i) + for i in l: + d.pop(i) + for i in d: + for j in d[i]: + if j in l: + d[i].remove(j) +print(t) + diff --git a/December 26/C++_Jeswindany_ThePhantomCycle.c++ b/December 26/C++_Jeswindany_ThePhantomCycle.c++ new file mode 100644 index 0000000..66ceecd --- /dev/null +++ b/December 26/C++_Jeswindany_ThePhantomCycle.c++ @@ -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->next->next = head; + if (detectLoop(head)) { + cout << "Cycle Found"; + } else { + cout << "No Cycle Found"; + } + + return 0; +} diff --git a/December 27/C++_Jeswindany_CircleOfEndurance.c++ b/December 27/C++_Jeswindany_CircleOfEndurance.c++ new file mode 100644 index 0000000..b47137f --- /dev/null +++ b/December 27/C++_Jeswindany_CircleOfEndurance.c++ @@ -0,0 +1,36 @@ +#include +using namespace std; + +int main(){ + int n; + cout << "N = "; + cin >> n; + int petrol[n]; + cout << "Petrol = "; + for (int i = 0; i < n; i++) + cin >> petrol[i]; + int distance[n]; + cout << "Distance = "; + for (int i = 0; i < n; i++) + cin >> distance[i]; + for (int i = 0; i < n; i++){ + if(petrol[i] >= distance[i]){ + int pos = i, tank = petrol[i]-distance[i]; + pos = (pos+1)%n; + while(pos != i){ + tank += petrol[pos]; + if (distance[pos] > tank){ + break; + } + tank -= distance[pos]; + pos = (pos+1)%n; + } + if (pos == i){ + cout << i+1; + return 0; + } + } + } + cout << -1; + return 0; +} \ No newline at end of file diff --git a/December 28/Python_Jeswindany_TheSellingGame.py b/December 28/Python_Jeswindany_TheSellingGame.py new file mode 100644 index 0000000..67dc358 --- /dev/null +++ b/December 28/Python_Jeswindany_TheSellingGame.py @@ -0,0 +1,32 @@ +def max_gadgets_sold(x, z, items, clients): + items.sort(key=lambda item: item['m'], 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 + +x = 3 +z = 3 +items = [ + {'k': 10, 'r': 100, 'm': 5, 'n': 110}, + {'k': 9, 'r': 200, 'm': 2, 'n': 200}, + {'k': 20, 'r': 300, 'm': 30, 'n': 300} +] + +clients = [ + {'k': 5, 'r': 110}, + {'k': 9, 'r': 500}, + {'k': 20, 'r': 400}, +] + +result = max_gadgets_sold(x, z, items, clients) +print(result) diff --git a/December 29/Python_JeswinDany_CartesianWalkValidator.py b/December 29/Python_JeswinDany_CartesianWalkValidator.py new file mode 100644 index 0000000..03e39c4 --- /dev/null +++ b/December 29/Python_JeswinDany_CartesianWalkValidator.py @@ -0,0 +1,24 @@ +directions = input().split() + +def CartesianWalkValidator(directions): + + if(len(directions) == 10): + initial = [0,0] + for i in directions: + if i == 'n': + initial[1] += 1 + elif i == 'e': + initial[0] += 1 + elif i == 'w': + initial[0] -= 1 + elif i == 's': + initial[1] -= 1 + if initial == [0,0]: + return True + else: + return False + + else: + return False + +print(CartesianWalkValidator(directions)) \ No newline at end of file diff --git a/December 30/Python_Jeswindany_TreeInversions.py b/December 30/Python_Jeswindany_TreeInversions.py new file mode 100644 index 0000000..e87d13e --- /dev/null +++ b/December 30/Python_Jeswindany_TreeInversions.py @@ -0,0 +1,51 @@ +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) # Divide by 2 to avoid double counting + 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/Python_Jeswindany_NQueens.py b/December 31/Python_Jeswindany_NQueens.py new file mode 100644 index 0000000..133d1e1 --- /dev/null +++ b/December 31/Python_Jeswindany_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)